1 #ifndef _LINUX_TIMEKEEPING_H
2 #define _LINUX_TIMEKEEPING_H
4 #include <asm-generic/errno-base.h>
6 /* Included from linux/ktime.h */
8 void timekeeping_init(void);
9 extern int timekeeping_suspended
;
12 * Get and set timeofday
14 extern void do_gettimeofday(struct timeval
*tv
);
15 extern int do_settimeofday64(const struct timespec64
*ts
);
16 extern int do_sys_settimeofday64(const struct timespec64
*tv
,
17 const struct timezone
*tz
);
18 static inline int do_sys_settimeofday(const struct timespec
*tv
,
19 const struct timezone
*tz
)
21 struct timespec64 ts64
;
26 ts64
= timespec_to_timespec64(*tv
);
27 return do_sys_settimeofday64(&ts64
, tz
);
31 * Kernel time accessors
33 unsigned long get_seconds(void);
34 struct timespec64
current_kernel_time64(void);
35 /* does not take xtime_lock */
36 struct timespec
__current_kernel_time(void);
38 static inline struct timespec
current_kernel_time(void)
40 struct timespec64 now
= current_kernel_time64();
42 return timespec64_to_timespec(now
);
46 * timespec based interfaces
48 struct timespec64
get_monotonic_coarse64(void);
49 extern void getrawmonotonic64(struct timespec64
*ts
);
50 extern void ktime_get_ts64(struct timespec64
*ts
);
51 extern time64_t
ktime_get_seconds(void);
52 extern time64_t
ktime_get_real_seconds(void);
54 extern int __getnstimeofday64(struct timespec64
*tv
);
55 extern void getnstimeofday64(struct timespec64
*tv
);
56 extern void getboottime64(struct timespec64
*ts
);
58 #if BITS_PER_LONG == 64
60 * Deprecated. Use do_settimeofday64().
62 static inline int do_settimeofday(const struct timespec
*ts
)
64 return do_settimeofday64(ts
);
67 static inline int __getnstimeofday(struct timespec
*ts
)
69 return __getnstimeofday64(ts
);
72 static inline void getnstimeofday(struct timespec
*ts
)
77 static inline void ktime_get_ts(struct timespec
*ts
)
82 static inline void ktime_get_real_ts(struct timespec
*ts
)
87 static inline void getrawmonotonic(struct timespec
*ts
)
89 getrawmonotonic64(ts
);
92 static inline struct timespec
get_monotonic_coarse(void)
94 return get_monotonic_coarse64();
97 static inline void getboottime(struct timespec
*ts
)
99 return getboottime64(ts
);
103 * Deprecated. Use do_settimeofday64().
105 static inline int do_settimeofday(const struct timespec
*ts
)
107 struct timespec64 ts64
;
109 ts64
= timespec_to_timespec64(*ts
);
110 return do_settimeofday64(&ts64
);
113 static inline int __getnstimeofday(struct timespec
*ts
)
115 struct timespec64 ts64
;
116 int ret
= __getnstimeofday64(&ts64
);
118 *ts
= timespec64_to_timespec(ts64
);
122 static inline void getnstimeofday(struct timespec
*ts
)
124 struct timespec64 ts64
;
126 getnstimeofday64(&ts64
);
127 *ts
= timespec64_to_timespec(ts64
);
130 static inline void ktime_get_ts(struct timespec
*ts
)
132 struct timespec64 ts64
;
134 ktime_get_ts64(&ts64
);
135 *ts
= timespec64_to_timespec(ts64
);
138 static inline void ktime_get_real_ts(struct timespec
*ts
)
140 struct timespec64 ts64
;
142 getnstimeofday64(&ts64
);
143 *ts
= timespec64_to_timespec(ts64
);
146 static inline void getrawmonotonic(struct timespec
*ts
)
148 struct timespec64 ts64
;
150 getrawmonotonic64(&ts64
);
151 *ts
= timespec64_to_timespec(ts64
);
154 static inline struct timespec
get_monotonic_coarse(void)
156 return timespec64_to_timespec(get_monotonic_coarse64());
159 static inline void getboottime(struct timespec
*ts
)
161 struct timespec64 ts64
;
163 getboottime64(&ts64
);
164 *ts
= timespec64_to_timespec(ts64
);
168 #define ktime_get_real_ts64(ts) getnstimeofday64(ts)
171 * ktime_t based interfaces
181 extern ktime_t
ktime_get(void);
182 extern ktime_t
ktime_get_with_offset(enum tk_offsets offs
);
183 extern ktime_t
ktime_mono_to_any(ktime_t tmono
, enum tk_offsets offs
);
184 extern ktime_t
ktime_get_raw(void);
185 extern u32
ktime_get_resolution_ns(void);
188 * ktime_get_real - get the real (wall-) time in ktime_t format
190 static inline ktime_t
ktime_get_real(void)
192 return ktime_get_with_offset(TK_OFFS_REAL
);
196 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
198 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
199 * time spent in suspend.
201 static inline ktime_t
ktime_get_boottime(void)
203 return ktime_get_with_offset(TK_OFFS_BOOT
);
207 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
209 static inline ktime_t
ktime_get_clocktai(void)
211 return ktime_get_with_offset(TK_OFFS_TAI
);
215 * ktime_mono_to_real - Convert monotonic time to clock realtime
217 static inline ktime_t
ktime_mono_to_real(ktime_t mono
)
219 return ktime_mono_to_any(mono
, TK_OFFS_REAL
);
222 static inline u64
ktime_get_ns(void)
224 return ktime_to_ns(ktime_get());
227 static inline u64
ktime_get_real_ns(void)
229 return ktime_to_ns(ktime_get_real());
232 static inline u64
ktime_get_boot_ns(void)
234 return ktime_to_ns(ktime_get_boottime());
237 static inline u64
ktime_get_tai_ns(void)
239 return ktime_to_ns(ktime_get_clocktai());
242 static inline u64
ktime_get_raw_ns(void)
244 return ktime_to_ns(ktime_get_raw());
247 extern u64
ktime_get_mono_fast_ns(void);
248 extern u64
ktime_get_raw_fast_ns(void);
251 * Timespec interfaces utilizing the ktime based ones
253 static inline void get_monotonic_boottime(struct timespec
*ts
)
255 *ts
= ktime_to_timespec(ktime_get_boottime());
258 static inline void get_monotonic_boottime64(struct timespec64
*ts
)
260 *ts
= ktime_to_timespec64(ktime_get_boottime());
263 static inline void timekeeping_clocktai(struct timespec
*ts
)
265 *ts
= ktime_to_timespec(ktime_get_clocktai());
271 extern bool timekeeping_rtc_skipsuspend(void);
272 extern bool timekeeping_rtc_skipresume(void);
274 extern void timekeeping_inject_sleeptime64(struct timespec64
*delta
);
279 extern void ktime_get_raw_and_real_ts64(struct timespec64
*ts_raw
,
280 struct timespec64
*ts_real
);
283 * struct system_time_snapshot - simultaneous raw/real time capture with
285 * @cycles: Clocksource counter value to produce the system times
286 * @real: Realtime system time
287 * @raw: Monotonic raw system time
288 * @clock_was_set_seq: The sequence number of clock was set events
289 * @cs_was_changed_seq: The sequence number of clocksource change events
291 struct system_time_snapshot
{
295 unsigned int clock_was_set_seq
;
296 u8 cs_was_changed_seq
;
300 * struct system_device_crosststamp - system/device cross-timestamp
301 * (syncronized capture)
302 * @device: Device time
303 * @sys_realtime: Realtime simultaneous with device time
304 * @sys_monoraw: Monotonic raw simultaneous with device time
306 struct system_device_crosststamp
{
308 ktime_t sys_realtime
;
313 * struct system_counterval_t - system counter value with the pointer to the
314 * corresponding clocksource
315 * @cycles: System counter value
316 * @cs: Clocksource corresponding to system counter value. Used by
317 * timekeeping code to verify comparibility of two cycle values
319 struct system_counterval_t
{
321 struct clocksource
*cs
;
325 * Get cross timestamp between system clock and device clock
327 extern int get_device_system_crosststamp(
328 int (*get_time_fn
)(ktime_t
*device_time
,
329 struct system_counterval_t
*system_counterval
,
332 struct system_time_snapshot
*history
,
333 struct system_device_crosststamp
*xtstamp
);
336 * Simultaneously snapshot realtime and monotonic raw clocks
338 extern void ktime_get_snapshot(struct system_time_snapshot
*systime_snapshot
);
341 * Persistent clock related interfaces
343 extern int persistent_clock_is_local
;
345 extern void read_persistent_clock(struct timespec
*ts
);
346 extern void read_persistent_clock64(struct timespec64
*ts
);
347 extern void read_boot_clock64(struct timespec64
*ts
);
348 extern int update_persistent_clock(struct timespec now
);
349 extern int update_persistent_clock64(struct timespec64 now
);