blob: 361f8bf1429dfe28e63ad32d12561fab51c9e959 [file] [log] [blame]
Thomas Gleixner8b094cd2014-07-16 21:04:02 +00001#ifndef _LINUX_TIMEKEEPING_H
2#define _LINUX_TIMEKEEPING_H
3
Christoph Hellwigb536fd52016-09-22 07:48:17 -07004#include <linux/errno.h>
Baolin Wang86d34732016-04-08 14:02:12 +08005
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)
John Stultzdfc25072016-06-01 11:53:26 -070024 return do_sys_settimeofday64(NULL, tz);
25
26 if (!timespec_valid(tv))
Baolin Wang86d34732016-04-08 14:02:12 +080027 return -EINVAL;
28
29 ts64 = timespec_to_timespec64(*tv);
30 return do_sys_settimeofday64(&ts64, tz);
31}
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000032
33/*
34 * Kernel time accessors
35 */
36unsigned long get_seconds(void);
Baolin Wang8758a242015-07-29 20:09:43 +080037struct timespec64 current_kernel_time64(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000038/* does not take xtime_lock */
39struct timespec __current_kernel_time(void);
40
Baolin Wang8758a242015-07-29 20:09:43 +080041static inline struct timespec current_kernel_time(void)
42{
43 struct timespec64 now = current_kernel_time64();
44
45 return timespec64_to_timespec(now);
46}
47
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000048/*
49 * timespec based interfaces
50 */
John Stultz334334b2014-11-07 11:20:40 -080051struct timespec64 get_monotonic_coarse64(void);
John Stultzcdba2ec2014-11-07 11:03:20 -080052extern void getrawmonotonic64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000053extern void ktime_get_ts64(struct timespec64 *ts);
Heena Sirwani9e3680b2014-10-29 16:01:16 +053054extern time64_t ktime_get_seconds(void);
Heena Sirwanidbe7aa62014-10-29 16:01:50 +053055extern time64_t ktime_get_real_seconds(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000056
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000057extern int __getnstimeofday64(struct timespec64 *tv);
58extern void getnstimeofday64(struct timespec64 *tv);
John Stultzd08c0cd2014-12-08 12:00:09 -080059extern void getboottime64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000060
61#if BITS_PER_LONG == 64
pang.xunlei21f7eca2014-11-18 19:15:16 +080062/**
63 * Deprecated. Use do_settimeofday64().
64 */
65static inline int do_settimeofday(const struct timespec *ts)
66{
67 return do_settimeofday64(ts);
68}
69
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000070static inline int __getnstimeofday(struct timespec *ts)
71{
72 return __getnstimeofday64(ts);
73}
74
75static inline void getnstimeofday(struct timespec *ts)
76{
77 getnstimeofday64(ts);
78}
79
80static inline void ktime_get_ts(struct timespec *ts)
81{
82 ktime_get_ts64(ts);
83}
84
85static inline void ktime_get_real_ts(struct timespec *ts)
86{
87 getnstimeofday64(ts);
88}
89
John Stultzcdba2ec2014-11-07 11:03:20 -080090static inline void getrawmonotonic(struct timespec *ts)
91{
92 getrawmonotonic64(ts);
93}
94
John Stultz334334b2014-11-07 11:20:40 -080095static inline struct timespec get_monotonic_coarse(void)
96{
97 return get_monotonic_coarse64();
98}
John Stultzd08c0cd2014-12-08 12:00:09 -080099
100static inline void getboottime(struct timespec *ts)
101{
102 return getboottime64(ts);
103}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000104#else
pang.xunlei21f7eca2014-11-18 19:15:16 +0800105/**
106 * Deprecated. Use do_settimeofday64().
107 */
108static inline int do_settimeofday(const struct timespec *ts)
109{
110 struct timespec64 ts64;
111
112 ts64 = timespec_to_timespec64(*ts);
113 return do_settimeofday64(&ts64);
114}
115
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000116static inline int __getnstimeofday(struct timespec *ts)
117{
118 struct timespec64 ts64;
119 int ret = __getnstimeofday64(&ts64);
120
121 *ts = timespec64_to_timespec(ts64);
122 return ret;
123}
124
125static inline void getnstimeofday(struct timespec *ts)
126{
127 struct timespec64 ts64;
128
129 getnstimeofday64(&ts64);
130 *ts = timespec64_to_timespec(ts64);
131}
132
133static inline void ktime_get_ts(struct timespec *ts)
134{
135 struct timespec64 ts64;
136
137 ktime_get_ts64(&ts64);
138 *ts = timespec64_to_timespec(ts64);
139}
140
141static inline void ktime_get_real_ts(struct timespec *ts)
142{
143 struct timespec64 ts64;
144
145 getnstimeofday64(&ts64);
146 *ts = timespec64_to_timespec(ts64);
147}
John Stultzcdba2ec2014-11-07 11:03:20 -0800148
149static inline void getrawmonotonic(struct timespec *ts)
150{
151 struct timespec64 ts64;
152
153 getrawmonotonic64(&ts64);
154 *ts = timespec64_to_timespec(ts64);
155}
John Stultz334334b2014-11-07 11:20:40 -0800156
157static inline struct timespec get_monotonic_coarse(void)
158{
159 return timespec64_to_timespec(get_monotonic_coarse64());
160}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000161
John Stultzd08c0cd2014-12-08 12:00:09 -0800162static inline void getboottime(struct timespec *ts)
163{
164 struct timespec64 ts64;
165
166 getboottime64(&ts64);
167 *ts = timespec64_to_timespec(ts64);
168}
169#endif
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000170
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000171#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000172
173/*
174 * ktime_t based interfaces
175 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000176
177enum tk_offsets {
178 TK_OFFS_REAL,
179 TK_OFFS_BOOT,
180 TK_OFFS_TAI,
181 TK_OFFS_MAX,
182};
183
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000184extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000185extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000186extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000187extern ktime_t ktime_get_raw(void);
Harald Geyer6374f912015-04-07 11:12:35 +0000188extern u32 ktime_get_resolution_ns(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000189
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000190/**
191 * ktime_get_real - get the real (wall-) time in ktime_t format
192 */
193static inline ktime_t ktime_get_real(void)
194{
195 return ktime_get_with_offset(TK_OFFS_REAL);
196}
197
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000198/**
199 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
200 *
201 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
202 * time spent in suspend.
203 */
204static inline ktime_t ktime_get_boottime(void)
205{
206 return ktime_get_with_offset(TK_OFFS_BOOT);
207}
208
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000209/**
210 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
211 */
212static inline ktime_t ktime_get_clocktai(void)
213{
214 return ktime_get_with_offset(TK_OFFS_TAI);
215}
216
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000217/**
218 * ktime_mono_to_real - Convert monotonic time to clock realtime
219 */
220static inline ktime_t ktime_mono_to_real(ktime_t mono)
221{
222 return ktime_mono_to_any(mono, TK_OFFS_REAL);
223}
224
Thomas Gleixner897994e2014-07-16 21:04:29 +0000225static inline u64 ktime_get_ns(void)
226{
227 return ktime_to_ns(ktime_get());
228}
229
230static inline u64 ktime_get_real_ns(void)
231{
232 return ktime_to_ns(ktime_get_real());
233}
234
235static inline u64 ktime_get_boot_ns(void)
236{
237 return ktime_to_ns(ktime_get_boottime());
238}
239
Peter Zijlstrafe5fba02015-03-17 12:39:10 +0100240static inline u64 ktime_get_tai_ns(void)
241{
242 return ktime_to_ns(ktime_get_clocktai());
243}
244
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000245static inline u64 ktime_get_raw_ns(void)
246{
247 return ktime_to_ns(ktime_get_raw());
248}
249
Thomas Gleixner4396e052014-07-16 21:05:23 +0000250extern u64 ktime_get_mono_fast_ns(void);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100251extern u64 ktime_get_raw_fast_ns(void);
Joel Fernandes72408a62016-11-28 14:35:22 -0800252extern u64 ktime_get_boot_fast_ns(void);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000253
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000254/*
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000255 * Timespec interfaces utilizing the ktime based ones
256 */
257static inline void get_monotonic_boottime(struct timespec *ts)
258{
259 *ts = ktime_to_timespec(ktime_get_boottime());
260}
261
John Stultz2e0c78e2014-12-18 18:04:34 -0800262static inline void get_monotonic_boottime64(struct timespec64 *ts)
263{
264 *ts = ktime_to_timespec64(ktime_get_boottime());
265}
266
Thomas Gleixner61edec82014-07-16 21:05:01 +0000267static inline void timekeeping_clocktai(struct timespec *ts)
268{
269 *ts = ktime_to_timespec(ktime_get_clocktai());
270}
271
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000272/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000273 * RTC specific
274 */
Xunlei Pang0fa88cb2015-04-01 20:34:38 -0700275extern bool timekeeping_rtc_skipsuspend(void);
276extern bool timekeeping_rtc_skipresume(void);
277
pang.xunlei04d90892014-11-18 19:15:17 +0800278extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
279
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000280/*
281 * PPS accessor
282 */
Arnd Bergmann071eee42015-09-28 22:21:29 +0200283extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
284 struct timespec64 *ts_real);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000285
286/*
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800287 * struct system_time_snapshot - simultaneous raw/real time capture with
288 * counter value
289 * @cycles: Clocksource counter value to produce the system times
290 * @real: Realtime system time
291 * @raw: Monotonic raw system time
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800292 * @clock_was_set_seq: The sequence number of clock was set events
293 * @cs_was_changed_seq: The sequence number of clocksource change events
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800294 */
295struct system_time_snapshot {
296 cycle_t cycles;
297 ktime_t real;
298 ktime_t raw;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800299 unsigned int clock_was_set_seq;
300 u8 cs_was_changed_seq;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800301};
302
303/*
Christopher S. Hall8006c242016-02-22 03:15:22 -0800304 * struct system_device_crosststamp - system/device cross-timestamp
305 * (syncronized capture)
306 * @device: Device time
307 * @sys_realtime: Realtime simultaneous with device time
308 * @sys_monoraw: Monotonic raw simultaneous with device time
309 */
310struct system_device_crosststamp {
311 ktime_t device;
312 ktime_t sys_realtime;
313 ktime_t sys_monoraw;
314};
315
316/*
317 * struct system_counterval_t - system counter value with the pointer to the
318 * corresponding clocksource
319 * @cycles: System counter value
320 * @cs: Clocksource corresponding to system counter value. Used by
321 * timekeeping code to verify comparibility of two cycle values
322 */
323struct system_counterval_t {
324 cycle_t cycles;
325 struct clocksource *cs;
326};
327
328/*
329 * Get cross timestamp between system clock and device clock
330 */
331extern int get_device_system_crosststamp(
332 int (*get_time_fn)(ktime_t *device_time,
333 struct system_counterval_t *system_counterval,
334 void *ctx),
335 void *ctx,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800336 struct system_time_snapshot *history,
Christopher S. Hall8006c242016-02-22 03:15:22 -0800337 struct system_device_crosststamp *xtstamp);
338
339/*
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800340 * Simultaneously snapshot realtime and monotonic raw clocks
341 */
342extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
343
344/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000345 * Persistent clock related interfaces
346 */
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000347extern int persistent_clock_is_local;
348
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000349extern void read_persistent_clock(struct timespec *ts);
Xunlei Pang2ee96632015-04-01 20:34:22 -0700350extern void read_persistent_clock64(struct timespec64 *ts);
Xunlei Pang9a806dd2015-04-01 20:34:21 -0700351extern void read_boot_clock64(struct timespec64 *ts);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000352extern int update_persistent_clock(struct timespec now);
Xunlei Pang3c00a1f2015-04-01 20:34:23 -0700353extern int update_persistent_clock64(struct timespec64 now);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000354
355
356#endif