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
);
23 * Kernel time accessors
25 unsigned long get_seconds(void);
26 struct timespec64
current_kernel_time64(void);
27 /* does not take xtime_lock */
28 struct timespec
__current_kernel_time(void);
30 static inline struct timespec
current_kernel_time(void)
32 struct timespec64 now
= current_kernel_time64();
34 return timespec64_to_timespec(now
);
38 * timespec based interfaces
40 struct timespec64
get_monotonic_coarse64(void);
41 extern void getrawmonotonic64(struct timespec64
*ts
);
42 extern void ktime_get_ts64(struct timespec64
*ts
);
43 extern time64_t
ktime_get_seconds(void);
44 extern time64_t
ktime_get_real_seconds(void);
46 extern int __getnstimeofday64(struct timespec64
*tv
);
47 extern void getnstimeofday64(struct timespec64
*tv
);
48 extern void getboottime64(struct timespec64
*ts
);
50 #if BITS_PER_LONG == 64
52 * Deprecated. Use do_settimeofday64().
54 static inline int do_settimeofday(const struct timespec
*ts
)
56 return do_settimeofday64(ts
);
59 static inline int __getnstimeofday(struct timespec
*ts
)
61 return __getnstimeofday64(ts
);
64 static inline void getnstimeofday(struct timespec
*ts
)
69 static inline void ktime_get_ts(struct timespec
*ts
)
74 static inline void ktime_get_real_ts(struct timespec
*ts
)
79 static inline void getrawmonotonic(struct timespec
*ts
)
81 getrawmonotonic64(ts
);
84 static inline struct timespec
get_monotonic_coarse(void)
86 return get_monotonic_coarse64();
89 static inline void getboottime(struct timespec
*ts
)
91 return getboottime64(ts
);
95 * Deprecated. Use do_settimeofday64().
97 static inline int do_settimeofday(const struct timespec
*ts
)
99 struct timespec64 ts64
;
101 ts64
= timespec_to_timespec64(*ts
);
102 return do_settimeofday64(&ts64
);
105 static inline int __getnstimeofday(struct timespec
*ts
)
107 struct timespec64 ts64
;
108 int ret
= __getnstimeofday64(&ts64
);
110 *ts
= timespec64_to_timespec(ts64
);
114 static inline void getnstimeofday(struct timespec
*ts
)
116 struct timespec64 ts64
;
118 getnstimeofday64(&ts64
);
119 *ts
= timespec64_to_timespec(ts64
);
122 static inline void ktime_get_ts(struct timespec
*ts
)
124 struct timespec64 ts64
;
126 ktime_get_ts64(&ts64
);
127 *ts
= timespec64_to_timespec(ts64
);
130 static inline void ktime_get_real_ts(struct timespec
*ts
)
132 struct timespec64 ts64
;
134 getnstimeofday64(&ts64
);
135 *ts
= timespec64_to_timespec(ts64
);
138 static inline void getrawmonotonic(struct timespec
*ts
)
140 struct timespec64 ts64
;
142 getrawmonotonic64(&ts64
);
143 *ts
= timespec64_to_timespec(ts64
);
146 static inline struct timespec
get_monotonic_coarse(void)
148 return timespec64_to_timespec(get_monotonic_coarse64());
151 static inline void getboottime(struct timespec
*ts
)
153 struct timespec64 ts64
;
155 getboottime64(&ts64
);
156 *ts
= timespec64_to_timespec(ts64
);
160 #define ktime_get_real_ts64(ts) getnstimeofday64(ts)
163 * ktime_t based interfaces
173 extern ktime_t
ktime_get(void);
174 extern ktime_t
ktime_get_with_offset(enum tk_offsets offs
);
175 extern ktime_t
ktime_mono_to_any(ktime_t tmono
, enum tk_offsets offs
);
176 extern ktime_t
ktime_get_raw(void);
177 extern u32
ktime_get_resolution_ns(void);
180 * ktime_get_real - get the real (wall-) time in ktime_t format
182 static inline ktime_t
ktime_get_real(void)
184 return ktime_get_with_offset(TK_OFFS_REAL
);
188 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
190 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
191 * time spent in suspend.
193 static inline ktime_t
ktime_get_boottime(void)
195 return ktime_get_with_offset(TK_OFFS_BOOT
);
199 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
201 static inline ktime_t
ktime_get_clocktai(void)
203 return ktime_get_with_offset(TK_OFFS_TAI
);
207 * ktime_mono_to_real - Convert monotonic time to clock realtime
209 static inline ktime_t
ktime_mono_to_real(ktime_t mono
)
211 return ktime_mono_to_any(mono
, TK_OFFS_REAL
);
214 static inline u64
ktime_get_ns(void)
216 return ktime_to_ns(ktime_get());
219 static inline u64
ktime_get_real_ns(void)
221 return ktime_to_ns(ktime_get_real());
224 static inline u64
ktime_get_boot_ns(void)
226 return ktime_to_ns(ktime_get_boottime());
229 static inline u64
ktime_get_tai_ns(void)
231 return ktime_to_ns(ktime_get_clocktai());
234 static inline u64
ktime_get_raw_ns(void)
236 return ktime_to_ns(ktime_get_raw());
239 extern u64
ktime_get_mono_fast_ns(void);
240 extern u64
ktime_get_raw_fast_ns(void);
241 extern u64
ktime_get_boot_fast_ns(void);
244 * Timespec interfaces utilizing the ktime based ones
246 static inline void get_monotonic_boottime(struct timespec
*ts
)
248 *ts
= ktime_to_timespec(ktime_get_boottime());
251 static inline void get_monotonic_boottime64(struct timespec64
*ts
)
253 *ts
= ktime_to_timespec64(ktime_get_boottime());
256 static inline void timekeeping_clocktai(struct timespec
*ts
)
258 *ts
= ktime_to_timespec(ktime_get_clocktai());
261 static inline void timekeeping_clocktai64(struct timespec64
*ts
)
263 *ts
= ktime_to_timespec64(ktime_get_clocktai());
269 extern bool timekeeping_rtc_skipsuspend(void);
270 extern bool timekeeping_rtc_skipresume(void);
272 extern void timekeeping_inject_sleeptime64(struct timespec64
*delta
);
277 extern void ktime_get_raw_and_real_ts64(struct timespec64
*ts_raw
,
278 struct timespec64
*ts_real
);
281 * struct system_time_snapshot - simultaneous raw/real time capture with
283 * @cycles: Clocksource counter value to produce the system times
284 * @real: Realtime system time
285 * @raw: Monotonic raw system time
286 * @clock_was_set_seq: The sequence number of clock was set events
287 * @cs_was_changed_seq: The sequence number of clocksource change events
289 struct system_time_snapshot
{
293 unsigned int clock_was_set_seq
;
294 u8 cs_was_changed_seq
;
298 * struct system_device_crosststamp - system/device cross-timestamp
299 * (syncronized capture)
300 * @device: Device time
301 * @sys_realtime: Realtime simultaneous with device time
302 * @sys_monoraw: Monotonic raw simultaneous with device time
304 struct system_device_crosststamp
{
306 ktime_t sys_realtime
;
311 * struct system_counterval_t - system counter value with the pointer to the
312 * corresponding clocksource
313 * @cycles: System counter value
314 * @cs: Clocksource corresponding to system counter value. Used by
315 * timekeeping code to verify comparibility of two cycle values
317 struct system_counterval_t
{
319 struct clocksource
*cs
;
323 * Get cross timestamp between system clock and device clock
325 extern int get_device_system_crosststamp(
326 int (*get_time_fn
)(ktime_t
*device_time
,
327 struct system_counterval_t
*system_counterval
,
330 struct system_time_snapshot
*history
,
331 struct system_device_crosststamp
*xtstamp
);
334 * Simultaneously snapshot realtime and monotonic raw clocks
336 extern void ktime_get_snapshot(struct system_time_snapshot
*systime_snapshot
);
339 * Persistent clock related interfaces
341 extern int persistent_clock_is_local
;
343 extern void read_persistent_clock(struct timespec
*ts
);
344 extern void read_persistent_clock64(struct timespec64
*ts
);
345 extern void read_boot_clock64(struct timespec64
*ts
);
346 extern int update_persistent_clock(struct timespec now
);
347 extern int update_persistent_clock64(struct timespec64 now
);