blob: 37dbacf84849add3a31b410ee06d26ec08c56a07 [file] [log] [blame]
Thomas Gleixner8b094cd2014-07-16 21:04:02 +00001#ifndef _LINUX_TIMEKEEPING_H
2#define _LINUX_TIMEKEEPING_H
3
Baolin Wang86d34732016-04-08 14:02:12 +08004#include <asm-generic/errno-base.h>
5
Thomas Gleixner8b094cd2014-07-16 21:04:02 +00006/* Included from linux/ktime.h */
7
8void timekeeping_init(void);
9extern int timekeeping_suspended;
10
11/*
12 * Get and set timeofday
13 */
14extern void do_gettimeofday(struct timeval *tv);
pang.xunlei21f7eca2014-11-18 19:15:16 +080015extern int do_settimeofday64(const struct timespec64 *ts);
Baolin Wang86d34732016-04-08 14:02:12 +080016extern int do_sys_settimeofday64(const struct timespec64 *tv,
17 const struct timezone *tz);
18static inline int do_sys_settimeofday(const struct timespec *tv,
19 const struct timezone *tz)
20{
21 struct timespec64 ts64;
22
23 if (!tv)
24 return -EINVAL;
25
26 ts64 = timespec_to_timespec64(*tv);
27 return do_sys_settimeofday64(&ts64, tz);
28}
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000029
30/*
31 * Kernel time accessors
32 */
33unsigned long get_seconds(void);
Baolin Wang8758a242015-07-29 20:09:43 +080034struct timespec64 current_kernel_time64(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000035/* does not take xtime_lock */
36struct timespec __current_kernel_time(void);
37
Baolin Wang8758a242015-07-29 20:09:43 +080038static inline struct timespec current_kernel_time(void)
39{
40 struct timespec64 now = current_kernel_time64();
41
42 return timespec64_to_timespec(now);
43}
44
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000045/*
46 * timespec based interfaces
47 */
John Stultz334334b2014-11-07 11:20:40 -080048struct timespec64 get_monotonic_coarse64(void);
John Stultzcdba2ec2014-11-07 11:03:20 -080049extern void getrawmonotonic64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000050extern void ktime_get_ts64(struct timespec64 *ts);
Heena Sirwani9e3680b2014-10-29 16:01:16 +053051extern time64_t ktime_get_seconds(void);
Heena Sirwanidbe7aa62014-10-29 16:01:50 +053052extern time64_t ktime_get_real_seconds(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000053
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000054extern int __getnstimeofday64(struct timespec64 *tv);
55extern void getnstimeofday64(struct timespec64 *tv);
John Stultzd08c0cd2014-12-08 12:00:09 -080056extern void getboottime64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000057
58#if BITS_PER_LONG == 64
pang.xunlei21f7eca2014-11-18 19:15:16 +080059/**
60 * Deprecated. Use do_settimeofday64().
61 */
62static inline int do_settimeofday(const struct timespec *ts)
63{
64 return do_settimeofday64(ts);
65}
66
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000067static inline int __getnstimeofday(struct timespec *ts)
68{
69 return __getnstimeofday64(ts);
70}
71
72static inline void getnstimeofday(struct timespec *ts)
73{
74 getnstimeofday64(ts);
75}
76
77static inline void ktime_get_ts(struct timespec *ts)
78{
79 ktime_get_ts64(ts);
80}
81
82static inline void ktime_get_real_ts(struct timespec *ts)
83{
84 getnstimeofday64(ts);
85}
86
John Stultzcdba2ec2014-11-07 11:03:20 -080087static inline void getrawmonotonic(struct timespec *ts)
88{
89 getrawmonotonic64(ts);
90}
91
John Stultz334334b2014-11-07 11:20:40 -080092static inline struct timespec get_monotonic_coarse(void)
93{
94 return get_monotonic_coarse64();
95}
John Stultzd08c0cd2014-12-08 12:00:09 -080096
97static inline void getboottime(struct timespec *ts)
98{
99 return getboottime64(ts);
100}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000101#else
pang.xunlei21f7eca2014-11-18 19:15:16 +0800102/**
103 * Deprecated. Use do_settimeofday64().
104 */
105static inline int do_settimeofday(const struct timespec *ts)
106{
107 struct timespec64 ts64;
108
109 ts64 = timespec_to_timespec64(*ts);
110 return do_settimeofday64(&ts64);
111}
112
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000113static inline int __getnstimeofday(struct timespec *ts)
114{
115 struct timespec64 ts64;
116 int ret = __getnstimeofday64(&ts64);
117
118 *ts = timespec64_to_timespec(ts64);
119 return ret;
120}
121
122static inline void getnstimeofday(struct timespec *ts)
123{
124 struct timespec64 ts64;
125
126 getnstimeofday64(&ts64);
127 *ts = timespec64_to_timespec(ts64);
128}
129
130static inline void ktime_get_ts(struct timespec *ts)
131{
132 struct timespec64 ts64;
133
134 ktime_get_ts64(&ts64);
135 *ts = timespec64_to_timespec(ts64);
136}
137
138static inline void ktime_get_real_ts(struct timespec *ts)
139{
140 struct timespec64 ts64;
141
142 getnstimeofday64(&ts64);
143 *ts = timespec64_to_timespec(ts64);
144}
John Stultzcdba2ec2014-11-07 11:03:20 -0800145
146static inline void getrawmonotonic(struct timespec *ts)
147{
148 struct timespec64 ts64;
149
150 getrawmonotonic64(&ts64);
151 *ts = timespec64_to_timespec(ts64);
152}
John Stultz334334b2014-11-07 11:20:40 -0800153
154static inline struct timespec get_monotonic_coarse(void)
155{
156 return timespec64_to_timespec(get_monotonic_coarse64());
157}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000158
John Stultzd08c0cd2014-12-08 12:00:09 -0800159static inline void getboottime(struct timespec *ts)
160{
161 struct timespec64 ts64;
162
163 getboottime64(&ts64);
164 *ts = timespec64_to_timespec(ts64);
165}
166#endif
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000167
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000168#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000169
170/*
171 * ktime_t based interfaces
172 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000173
174enum tk_offsets {
175 TK_OFFS_REAL,
176 TK_OFFS_BOOT,
177 TK_OFFS_TAI,
178 TK_OFFS_MAX,
179};
180
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000181extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000182extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000183extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000184extern ktime_t ktime_get_raw(void);
Harald Geyer6374f912015-04-07 11:12:35 +0000185extern u32 ktime_get_resolution_ns(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000186
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000187/**
188 * ktime_get_real - get the real (wall-) time in ktime_t format
189 */
190static inline ktime_t ktime_get_real(void)
191{
192 return ktime_get_with_offset(TK_OFFS_REAL);
193}
194
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000195/**
196 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
197 *
198 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
199 * time spent in suspend.
200 */
201static inline ktime_t ktime_get_boottime(void)
202{
203 return ktime_get_with_offset(TK_OFFS_BOOT);
204}
205
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000206/**
207 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
208 */
209static inline ktime_t ktime_get_clocktai(void)
210{
211 return ktime_get_with_offset(TK_OFFS_TAI);
212}
213
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000214/**
215 * ktime_mono_to_real - Convert monotonic time to clock realtime
216 */
217static inline ktime_t ktime_mono_to_real(ktime_t mono)
218{
219 return ktime_mono_to_any(mono, TK_OFFS_REAL);
220}
221
Thomas Gleixner897994e2014-07-16 21:04:29 +0000222static inline u64 ktime_get_ns(void)
223{
224 return ktime_to_ns(ktime_get());
225}
226
227static inline u64 ktime_get_real_ns(void)
228{
229 return ktime_to_ns(ktime_get_real());
230}
231
232static inline u64 ktime_get_boot_ns(void)
233{
234 return ktime_to_ns(ktime_get_boottime());
235}
236
Peter Zijlstrafe5fba02015-03-17 12:39:10 +0100237static inline u64 ktime_get_tai_ns(void)
238{
239 return ktime_to_ns(ktime_get_clocktai());
240}
241
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000242static inline u64 ktime_get_raw_ns(void)
243{
244 return ktime_to_ns(ktime_get_raw());
245}
246
Thomas Gleixner4396e052014-07-16 21:05:23 +0000247extern u64 ktime_get_mono_fast_ns(void);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100248extern u64 ktime_get_raw_fast_ns(void);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000249
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000250/*
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000251 * Timespec interfaces utilizing the ktime based ones
252 */
253static inline void get_monotonic_boottime(struct timespec *ts)
254{
255 *ts = ktime_to_timespec(ktime_get_boottime());
256}
257
John Stultz2e0c78e2014-12-18 18:04:34 -0800258static inline void get_monotonic_boottime64(struct timespec64 *ts)
259{
260 *ts = ktime_to_timespec64(ktime_get_boottime());
261}
262
Thomas Gleixner61edec82014-07-16 21:05:01 +0000263static inline void timekeeping_clocktai(struct timespec *ts)
264{
265 *ts = ktime_to_timespec(ktime_get_clocktai());
266}
267
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000268/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000269 * RTC specific
270 */
Xunlei Pang0fa88cb2015-04-01 20:34:38 -0700271extern bool timekeeping_rtc_skipsuspend(void);
272extern bool timekeeping_rtc_skipresume(void);
273
pang.xunlei04d90892014-11-18 19:15:17 +0800274extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
275
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000276/*
277 * PPS accessor
278 */
Arnd Bergmann071eee42015-09-28 22:21:29 +0200279extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
280 struct timespec64 *ts_real);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000281
282/*
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800283 * struct system_time_snapshot - simultaneous raw/real time capture with
284 * counter value
285 * @cycles: Clocksource counter value to produce the system times
286 * @real: Realtime system time
287 * @raw: Monotonic raw system time
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800288 * @clock_was_set_seq: The sequence number of clock was set events
289 * @cs_was_changed_seq: The sequence number of clocksource change events
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800290 */
291struct system_time_snapshot {
292 cycle_t cycles;
293 ktime_t real;
294 ktime_t raw;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800295 unsigned int clock_was_set_seq;
296 u8 cs_was_changed_seq;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800297};
298
299/*
Christopher S. Hall8006c242016-02-22 03:15:22 -0800300 * 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
305 */
306struct system_device_crosststamp {
307 ktime_t device;
308 ktime_t sys_realtime;
309 ktime_t sys_monoraw;
310};
311
312/*
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
318 */
319struct system_counterval_t {
320 cycle_t cycles;
321 struct clocksource *cs;
322};
323
324/*
325 * Get cross timestamp between system clock and device clock
326 */
327extern int get_device_system_crosststamp(
328 int (*get_time_fn)(ktime_t *device_time,
329 struct system_counterval_t *system_counterval,
330 void *ctx),
331 void *ctx,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800332 struct system_time_snapshot *history,
Christopher S. Hall8006c242016-02-22 03:15:22 -0800333 struct system_device_crosststamp *xtstamp);
334
335/*
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800336 * Simultaneously snapshot realtime and monotonic raw clocks
337 */
338extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
339
340/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000341 * Persistent clock related interfaces
342 */
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000343extern int persistent_clock_is_local;
344
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000345extern void read_persistent_clock(struct timespec *ts);
Xunlei Pang2ee96632015-04-01 20:34:22 -0700346extern void read_persistent_clock64(struct timespec64 *ts);
Xunlei Pang9a806dd2015-04-01 20:34:21 -0700347extern void read_boot_clock64(struct timespec64 *ts);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000348extern int update_persistent_clock(struct timespec now);
Xunlei Pang3c00a1f2015-04-01 20:34:23 -0700349extern int update_persistent_clock64(struct timespec64 now);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000350
351
352#endif