1 #ifndef _LINUX_TIMEKEEPING_H
2 #define _LINUX_TIMEKEEPING_H
4 /* Included from linux/ktime.h */
6 void timekeeping_init(void);
7 extern int timekeeping_suspended
;
10 * Get and set timeofday
12 extern void do_gettimeofday(struct timeval
*tv
);
13 extern int do_settimeofday64(const struct timespec64
*ts
);
14 extern int do_sys_settimeofday(const struct timespec
*tv
,
15 const struct timezone
*tz
);
18 * Kernel time accessors
20 unsigned long get_seconds(void);
21 struct timespec64
current_kernel_time64(void);
22 /* does not take xtime_lock */
23 struct timespec
__current_kernel_time(void);
25 static inline struct timespec
current_kernel_time(void)
27 struct timespec64 now
= current_kernel_time64();
29 return timespec64_to_timespec(now
);
33 * timespec based interfaces
35 struct timespec64
get_monotonic_coarse64(void);
36 extern void getrawmonotonic64(struct timespec64
*ts
);
37 extern void ktime_get_ts64(struct timespec64
*ts
);
38 extern time64_t
ktime_get_seconds(void);
39 extern time64_t
ktime_get_real_seconds(void);
41 extern int __getnstimeofday64(struct timespec64
*tv
);
42 extern void getnstimeofday64(struct timespec64
*tv
);
43 extern void getboottime64(struct timespec64
*ts
);
45 #if BITS_PER_LONG == 64
47 * Deprecated. Use do_settimeofday64().
49 static inline int do_settimeofday(const struct timespec
*ts
)
51 return do_settimeofday64(ts
);
54 static inline int __getnstimeofday(struct timespec
*ts
)
56 return __getnstimeofday64(ts
);
59 static inline void getnstimeofday(struct timespec
*ts
)
64 static inline void ktime_get_ts(struct timespec
*ts
)
69 static inline void ktime_get_real_ts(struct timespec
*ts
)
74 static inline void getrawmonotonic(struct timespec
*ts
)
76 getrawmonotonic64(ts
);
79 static inline struct timespec
get_monotonic_coarse(void)
81 return get_monotonic_coarse64();
84 static inline void getboottime(struct timespec
*ts
)
86 return getboottime64(ts
);
90 * Deprecated. Use do_settimeofday64().
92 static inline int do_settimeofday(const struct timespec
*ts
)
94 struct timespec64 ts64
;
96 ts64
= timespec_to_timespec64(*ts
);
97 return do_settimeofday64(&ts64
);
100 static inline int __getnstimeofday(struct timespec
*ts
)
102 struct timespec64 ts64
;
103 int ret
= __getnstimeofday64(&ts64
);
105 *ts
= timespec64_to_timespec(ts64
);
109 static inline void getnstimeofday(struct timespec
*ts
)
111 struct timespec64 ts64
;
113 getnstimeofday64(&ts64
);
114 *ts
= timespec64_to_timespec(ts64
);
117 static inline void ktime_get_ts(struct timespec
*ts
)
119 struct timespec64 ts64
;
121 ktime_get_ts64(&ts64
);
122 *ts
= timespec64_to_timespec(ts64
);
125 static inline void ktime_get_real_ts(struct timespec
*ts
)
127 struct timespec64 ts64
;
129 getnstimeofday64(&ts64
);
130 *ts
= timespec64_to_timespec(ts64
);
133 static inline void getrawmonotonic(struct timespec
*ts
)
135 struct timespec64 ts64
;
137 getrawmonotonic64(&ts64
);
138 *ts
= timespec64_to_timespec(ts64
);
141 static inline struct timespec
get_monotonic_coarse(void)
143 return timespec64_to_timespec(get_monotonic_coarse64());
146 static inline void getboottime(struct timespec
*ts
)
148 struct timespec64 ts64
;
150 getboottime64(&ts64
);
151 *ts
= timespec64_to_timespec(ts64
);
155 #define ktime_get_real_ts64(ts) getnstimeofday64(ts)
158 * ktime_t based interfaces
168 extern ktime_t
ktime_get(void);
169 extern ktime_t
ktime_get_with_offset(enum tk_offsets offs
);
170 extern ktime_t
ktime_mono_to_any(ktime_t tmono
, enum tk_offsets offs
);
171 extern ktime_t
ktime_get_raw(void);
172 extern u32
ktime_get_resolution_ns(void);
175 * ktime_get_real - get the real (wall-) time in ktime_t format
177 static inline ktime_t
ktime_get_real(void)
179 return ktime_get_with_offset(TK_OFFS_REAL
);
183 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
185 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
186 * time spent in suspend.
188 static inline ktime_t
ktime_get_boottime(void)
190 return ktime_get_with_offset(TK_OFFS_BOOT
);
194 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
196 static inline ktime_t
ktime_get_clocktai(void)
198 return ktime_get_with_offset(TK_OFFS_TAI
);
202 * ktime_mono_to_real - Convert monotonic time to clock realtime
204 static inline ktime_t
ktime_mono_to_real(ktime_t mono
)
206 return ktime_mono_to_any(mono
, TK_OFFS_REAL
);
209 static inline u64
ktime_get_ns(void)
211 return ktime_to_ns(ktime_get());
214 static inline u64
ktime_get_real_ns(void)
216 return ktime_to_ns(ktime_get_real());
219 static inline u64
ktime_get_boot_ns(void)
221 return ktime_to_ns(ktime_get_boottime());
224 static inline u64
ktime_get_tai_ns(void)
226 return ktime_to_ns(ktime_get_clocktai());
229 static inline u64
ktime_get_raw_ns(void)
231 return ktime_to_ns(ktime_get_raw());
234 extern u64
ktime_get_mono_fast_ns(void);
235 extern u64
ktime_get_raw_fast_ns(void);
238 * Timespec interfaces utilizing the ktime based ones
240 static inline void get_monotonic_boottime(struct timespec
*ts
)
242 *ts
= ktime_to_timespec(ktime_get_boottime());
245 static inline void get_monotonic_boottime64(struct timespec64
*ts
)
247 *ts
= ktime_to_timespec64(ktime_get_boottime());
250 static inline void timekeeping_clocktai(struct timespec
*ts
)
252 *ts
= ktime_to_timespec(ktime_get_clocktai());
258 extern bool timekeeping_rtc_skipsuspend(void);
259 extern bool timekeeping_rtc_skipresume(void);
261 extern void timekeeping_inject_sleeptime64(struct timespec64
*delta
);
266 extern void ktime_get_raw_and_real_ts64(struct timespec64
*ts_raw
,
267 struct timespec64
*ts_real
);
270 * struct system_time_snapshot - simultaneous raw/real time capture with
272 * @cycles: Clocksource counter value to produce the system times
273 * @real: Realtime system time
274 * @raw: Monotonic raw system time
275 * @clock_was_set_seq: The sequence number of clock was set events
276 * @cs_was_changed_seq: The sequence number of clocksource change events
278 struct system_time_snapshot
{
282 unsigned int clock_was_set_seq
;
283 u8 cs_was_changed_seq
;
287 * struct system_device_crosststamp - system/device cross-timestamp
288 * (syncronized capture)
289 * @device: Device time
290 * @sys_realtime: Realtime simultaneous with device time
291 * @sys_monoraw: Monotonic raw simultaneous with device time
293 struct system_device_crosststamp
{
295 ktime_t sys_realtime
;
300 * struct system_counterval_t - system counter value with the pointer to the
301 * corresponding clocksource
302 * @cycles: System counter value
303 * @cs: Clocksource corresponding to system counter value. Used by
304 * timekeeping code to verify comparibility of two cycle values
306 struct system_counterval_t
{
308 struct clocksource
*cs
;
312 * Get cross timestamp between system clock and device clock
314 extern int get_device_system_crosststamp(
315 int (*get_time_fn
)(ktime_t
*device_time
,
316 struct system_counterval_t
*system_counterval
,
319 struct system_time_snapshot
*history
,
320 struct system_device_crosststamp
*xtstamp
);
323 * Simultaneously snapshot realtime and monotonic raw clocks
325 extern void ktime_get_snapshot(struct system_time_snapshot
*systime_snapshot
);
328 * Persistent clock related interfaces
330 extern int persistent_clock_is_local
;
332 extern void read_persistent_clock(struct timespec
*ts
);
333 extern void read_persistent_clock64(struct timespec64
*ts
);
334 extern void read_boot_clock64(struct timespec64
*ts
);
335 extern int update_persistent_clock(struct timespec now
);
336 extern int update_persistent_clock64(struct timespec64 now
);