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>
8 /* Included from linux/ktime.h */
10 void timekeeping_init(void);
11 extern int timekeeping_suspended;
13 /* Architecture timer tick functions: */
14 extern void legacy_timer_tick(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 * ktime_get() family: read the current time in a multitude of ways,
26 * The default time reference is CLOCK_MONOTONIC, starting at
27 * boot time but not counting the time spent in suspend.
28 * For other references, use the functions with "real", "clocktai",
29 * "boottime" and "raw" suffixes.
31 * To get the time in a different format, use the ones wit
32 * "ns", "ts64" and "seconds" suffix.
34 * See Documentation/core-api/timekeeping.rst for more details.
39 * timespec64 based interfaces
41 extern void ktime_get_raw_ts64(struct timespec64 *ts);
42 extern void ktime_get_ts64(struct timespec64 *ts);
43 extern void ktime_get_real_ts64(struct timespec64 *tv);
44 extern void ktime_get_coarse_ts64(struct timespec64 *ts);
45 extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
47 void getboottime64(struct timespec64 *ts);
50 * time64_t base interfaces
52 extern time64_t ktime_get_seconds(void);
53 extern time64_t __ktime_get_real_seconds(void);
54 extern time64_t ktime_get_real_seconds(void);
57 * ktime_t based interfaces
67 extern ktime_t ktime_get(void);
68 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
69 extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs);
70 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
71 extern ktime_t ktime_get_raw(void);
72 extern u32 ktime_get_resolution_ns(void);
75 * ktime_get_real - get the real (wall-) time in ktime_t format
77 static inline ktime_t ktime_get_real(void)
79 return ktime_get_with_offset(TK_OFFS_REAL);
82 static inline ktime_t ktime_get_coarse_real(void)
84 return ktime_get_coarse_with_offset(TK_OFFS_REAL);
88 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
90 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
91 * time spent in suspend.
93 static inline ktime_t ktime_get_boottime(void)
95 return ktime_get_with_offset(TK_OFFS_BOOT);
98 static inline ktime_t ktime_get_coarse_boottime(void)
100 return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
104 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
106 static inline ktime_t ktime_get_clocktai(void)
108 return ktime_get_with_offset(TK_OFFS_TAI);
111 static inline ktime_t ktime_get_coarse_clocktai(void)
113 return ktime_get_coarse_with_offset(TK_OFFS_TAI);
116 static inline ktime_t ktime_get_coarse(void)
118 struct timespec64 ts;
120 ktime_get_coarse_ts64(&ts);
121 return timespec64_to_ktime(ts);
124 static inline u64 ktime_get_coarse_ns(void)
126 return ktime_to_ns(ktime_get_coarse());
129 static inline u64 ktime_get_coarse_real_ns(void)
131 return ktime_to_ns(ktime_get_coarse_real());
134 static inline u64 ktime_get_coarse_boottime_ns(void)
136 return ktime_to_ns(ktime_get_coarse_boottime());
139 static inline u64 ktime_get_coarse_clocktai_ns(void)
141 return ktime_to_ns(ktime_get_coarse_clocktai());
145 * ktime_mono_to_real - Convert monotonic time to clock realtime
147 static inline ktime_t ktime_mono_to_real(ktime_t mono)
149 return ktime_mono_to_any(mono, TK_OFFS_REAL);
152 static inline u64 ktime_get_ns(void)
154 return ktime_to_ns(ktime_get());
157 static inline u64 ktime_get_real_ns(void)
159 return ktime_to_ns(ktime_get_real());
162 static inline u64 ktime_get_boottime_ns(void)
164 return ktime_to_ns(ktime_get_boottime());
167 static inline u64 ktime_get_clocktai_ns(void)
169 return ktime_to_ns(ktime_get_clocktai());
172 static inline u64 ktime_get_raw_ns(void)
174 return ktime_to_ns(ktime_get_raw());
177 extern u64 ktime_get_mono_fast_ns(void);
178 extern u64 ktime_get_raw_fast_ns(void);
179 extern u64 ktime_get_boot_fast_ns(void);
180 extern u64 ktime_get_tai_fast_ns(void);
181 extern u64 ktime_get_real_fast_ns(void);
184 * timespec64/time64_t interfaces utilizing the ktime based ones
185 * for API completeness, these could be implemented more efficiently
188 static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
190 *ts = ktime_to_timespec64(ktime_get_boottime());
193 static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
195 *ts = ktime_to_timespec64(ktime_get_coarse_boottime());
198 static inline time64_t ktime_get_boottime_seconds(void)
200 return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
203 static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
205 *ts = ktime_to_timespec64(ktime_get_clocktai());
208 static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
210 *ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
213 static inline time64_t ktime_get_clocktai_seconds(void)
215 return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
221 extern bool timekeeping_rtc_skipsuspend(void);
222 extern bool timekeeping_rtc_skipresume(void);
224 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
227 * struct ktime_timestanps - Simultaneous mono/boot/real timestamps
228 * @mono: Monotonic timestamp
229 * @boot: Boottime timestamp
230 * @real: Realtime timestamp
232 struct ktime_timestamps {
239 * struct system_time_snapshot - simultaneous raw/real time capture with
241 * @cycles: Clocksource counter value to produce the system times
242 * @real: Realtime system time
243 * @raw: Monotonic raw system time
244 * @clock_was_set_seq: The sequence number of clock was set events
245 * @cs_was_changed_seq: The sequence number of clocksource change events
247 struct system_time_snapshot {
251 enum clocksource_ids cs_id;
252 unsigned int clock_was_set_seq;
253 u8 cs_was_changed_seq;
257 * struct system_device_crosststamp - system/device cross-timestamp
258 * (synchronized capture)
259 * @device: Device time
260 * @sys_realtime: Realtime simultaneous with device time
261 * @sys_monoraw: Monotonic raw simultaneous with device time
263 struct system_device_crosststamp {
265 ktime_t sys_realtime;
270 * struct system_counterval_t - system counter value with the pointer to the
271 * corresponding clocksource
272 * @cycles: System counter value
273 * @cs: Clocksource corresponding to system counter value. Used by
274 * timekeeping code to verify comparibility of two cycle values
276 struct system_counterval_t {
278 struct clocksource *cs;
282 * Get cross timestamp between system clock and device clock
284 extern int get_device_system_crosststamp(
285 int (*get_time_fn)(ktime_t *device_time,
286 struct system_counterval_t *system_counterval,
289 struct system_time_snapshot *history,
290 struct system_device_crosststamp *xtstamp);
293 * Simultaneously snapshot realtime and monotonic raw clocks
295 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
297 /* NMI safe mono/boot/realtime timestamps */
298 extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap);
301 * Persistent clock related interfaces
303 extern int persistent_clock_is_local;
305 extern void read_persistent_clock64(struct timespec64 *ts);
306 void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
307 struct timespec64 *boot_offset);
308 #ifdef CONFIG_GENERIC_CMOS_UPDATE
309 extern int update_persistent_clock64(struct timespec64 now);