1 #ifndef _LINUX_TIMEKEEPING_H
2 #define _LINUX_TIMEKEEPING_H
4 #include <linux/errno.h>
6 /* Included from linux/ktime.h */
8 void timekeeping_init(void);
9 extern int timekeeping_suspended;
11 /* Architecture timer tick functions: */
12 extern void update_process_times(int user);
13 extern void xtime_update(unsigned long ticks);
16 * Get and set timeofday
18 extern void do_gettimeofday(struct timeval *tv);
19 extern int do_settimeofday64(const struct timespec64 *ts);
20 extern int do_sys_settimeofday64(const struct timespec64 *tv,
21 const struct timezone *tz);
22 static inline int do_sys_settimeofday(const struct timespec *tv,
23 const struct timezone *tz)
25 struct timespec64 ts64;
28 return do_sys_settimeofday64(NULL, tz);
30 if (!timespec_valid(tv))
33 ts64 = timespec_to_timespec64(*tv);
34 return do_sys_settimeofday64(&ts64, tz);
38 * Kernel time accessors
40 unsigned long get_seconds(void);
41 struct timespec64 current_kernel_time64(void);
42 /* does not take xtime_lock */
43 struct timespec __current_kernel_time(void);
45 static inline struct timespec current_kernel_time(void)
47 struct timespec64 now = current_kernel_time64();
49 return timespec64_to_timespec(now);
53 * timespec based interfaces
55 struct timespec64 get_monotonic_coarse64(void);
56 extern void getrawmonotonic64(struct timespec64 *ts);
57 extern void ktime_get_ts64(struct timespec64 *ts);
58 extern time64_t ktime_get_seconds(void);
59 extern time64_t ktime_get_real_seconds(void);
61 extern int __getnstimeofday64(struct timespec64 *tv);
62 extern void getnstimeofday64(struct timespec64 *tv);
63 extern void getboottime64(struct timespec64 *ts);
65 #if BITS_PER_LONG == 64
67 * Deprecated. Use do_settimeofday64().
69 static inline int do_settimeofday(const struct timespec *ts)
71 return do_settimeofday64(ts);
74 static inline int __getnstimeofday(struct timespec *ts)
76 return __getnstimeofday64(ts);
79 static inline void getnstimeofday(struct timespec *ts)
84 static inline void ktime_get_ts(struct timespec *ts)
89 static inline void ktime_get_real_ts(struct timespec *ts)
94 static inline void getrawmonotonic(struct timespec *ts)
96 getrawmonotonic64(ts);
99 static inline struct timespec get_monotonic_coarse(void)
101 return get_monotonic_coarse64();
104 static inline void getboottime(struct timespec *ts)
106 return getboottime64(ts);
110 * Deprecated. Use do_settimeofday64().
112 static inline int do_settimeofday(const struct timespec *ts)
114 struct timespec64 ts64;
116 ts64 = timespec_to_timespec64(*ts);
117 return do_settimeofday64(&ts64);
120 static inline int __getnstimeofday(struct timespec *ts)
122 struct timespec64 ts64;
123 int ret = __getnstimeofday64(&ts64);
125 *ts = timespec64_to_timespec(ts64);
129 static inline void getnstimeofday(struct timespec *ts)
131 struct timespec64 ts64;
133 getnstimeofday64(&ts64);
134 *ts = timespec64_to_timespec(ts64);
137 static inline void ktime_get_ts(struct timespec *ts)
139 struct timespec64 ts64;
141 ktime_get_ts64(&ts64);
142 *ts = timespec64_to_timespec(ts64);
145 static inline void ktime_get_real_ts(struct timespec *ts)
147 struct timespec64 ts64;
149 getnstimeofday64(&ts64);
150 *ts = timespec64_to_timespec(ts64);
153 static inline void getrawmonotonic(struct timespec *ts)
155 struct timespec64 ts64;
157 getrawmonotonic64(&ts64);
158 *ts = timespec64_to_timespec(ts64);
161 static inline struct timespec get_monotonic_coarse(void)
163 return timespec64_to_timespec(get_monotonic_coarse64());
166 static inline void getboottime(struct timespec *ts)
168 struct timespec64 ts64;
170 getboottime64(&ts64);
171 *ts = timespec64_to_timespec(ts64);
175 #define ktime_get_real_ts64(ts) getnstimeofday64(ts)
178 * ktime_t based interfaces
188 extern ktime_t ktime_get(void);
189 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
190 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
191 extern ktime_t ktime_get_raw(void);
192 extern u32 ktime_get_resolution_ns(void);
195 * ktime_get_real - get the real (wall-) time in ktime_t format
197 static inline ktime_t ktime_get_real(void)
199 return ktime_get_with_offset(TK_OFFS_REAL);
203 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
205 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
206 * time spent in suspend.
208 static inline ktime_t ktime_get_boottime(void)
210 return ktime_get_with_offset(TK_OFFS_BOOT);
214 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
216 static inline ktime_t ktime_get_clocktai(void)
218 return ktime_get_with_offset(TK_OFFS_TAI);
222 * ktime_mono_to_real - Convert monotonic time to clock realtime
224 static inline ktime_t ktime_mono_to_real(ktime_t mono)
226 return ktime_mono_to_any(mono, TK_OFFS_REAL);
229 static inline u64 ktime_get_ns(void)
231 return ktime_to_ns(ktime_get());
234 static inline u64 ktime_get_real_ns(void)
236 return ktime_to_ns(ktime_get_real());
239 static inline u64 ktime_get_boot_ns(void)
241 return ktime_to_ns(ktime_get_boottime());
244 static inline u64 ktime_get_tai_ns(void)
246 return ktime_to_ns(ktime_get_clocktai());
249 static inline u64 ktime_get_raw_ns(void)
251 return ktime_to_ns(ktime_get_raw());
254 extern u64 ktime_get_mono_fast_ns(void);
255 extern u64 ktime_get_raw_fast_ns(void);
256 extern u64 ktime_get_boot_fast_ns(void);
259 * Timespec interfaces utilizing the ktime based ones
261 static inline void get_monotonic_boottime(struct timespec *ts)
263 *ts = ktime_to_timespec(ktime_get_boottime());
266 static inline void get_monotonic_boottime64(struct timespec64 *ts)
268 *ts = ktime_to_timespec64(ktime_get_boottime());
271 static inline void timekeeping_clocktai(struct timespec *ts)
273 *ts = ktime_to_timespec(ktime_get_clocktai());
279 extern bool timekeeping_rtc_skipsuspend(void);
280 extern bool timekeeping_rtc_skipresume(void);
282 extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
287 extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
288 struct timespec64 *ts_real);
291 * struct system_time_snapshot - simultaneous raw/real time capture with
293 * @cycles: Clocksource counter value to produce the system times
294 * @real: Realtime system time
295 * @raw: Monotonic raw system time
296 * @clock_was_set_seq: The sequence number of clock was set events
297 * @cs_was_changed_seq: The sequence number of clocksource change events
299 struct system_time_snapshot {
303 unsigned int clock_was_set_seq;
304 u8 cs_was_changed_seq;
308 * struct system_device_crosststamp - system/device cross-timestamp
309 * (syncronized capture)
310 * @device: Device time
311 * @sys_realtime: Realtime simultaneous with device time
312 * @sys_monoraw: Monotonic raw simultaneous with device time
314 struct system_device_crosststamp {
316 ktime_t sys_realtime;
321 * struct system_counterval_t - system counter value with the pointer to the
322 * corresponding clocksource
323 * @cycles: System counter value
324 * @cs: Clocksource corresponding to system counter value. Used by
325 * timekeeping code to verify comparibility of two cycle values
327 struct system_counterval_t {
329 struct clocksource *cs;
333 * Get cross timestamp between system clock and device clock
335 extern int get_device_system_crosststamp(
336 int (*get_time_fn)(ktime_t *device_time,
337 struct system_counterval_t *system_counterval,
340 struct system_time_snapshot *history,
341 struct system_device_crosststamp *xtstamp);
344 * Simultaneously snapshot realtime and monotonic raw clocks
346 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
349 * Persistent clock related interfaces
351 extern int persistent_clock_is_local;
353 extern void read_persistent_clock(struct timespec *ts);
354 extern void read_persistent_clock64(struct timespec64 *ts);
355 extern void read_boot_clock64(struct timespec64 *ts);
356 extern int update_persistent_clock(struct timespec now);
357 extern int update_persistent_clock64(struct timespec64 now);