blob: ddc229ff6d1ee1dbd3e535d7c0a2f16225c29f47 [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
Ingo Molnar93b5a9a2017-02-05 14:53:36 +010011/* Architecture timer tick functions: */
12extern void update_process_times(int user);
13extern void xtime_update(unsigned long ticks);
14
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000015/*
16 * Get and set timeofday
17 */
18extern void do_gettimeofday(struct timeval *tv);
pang.xunlei21f7eca2014-11-18 19:15:16 +080019extern int do_settimeofday64(const struct timespec64 *ts);
Baolin Wang86d34732016-04-08 14:02:12 +080020extern int do_sys_settimeofday64(const struct timespec64 *tv,
21 const struct timezone *tz);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000022/*
23 * Kernel time accessors
24 */
25unsigned long get_seconds(void);
Baolin Wang8758a242015-07-29 20:09:43 +080026struct timespec64 current_kernel_time64(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000027/* does not take xtime_lock */
28struct timespec __current_kernel_time(void);
29
Baolin Wang8758a242015-07-29 20:09:43 +080030static inline struct timespec current_kernel_time(void)
31{
32 struct timespec64 now = current_kernel_time64();
33
34 return timespec64_to_timespec(now);
35}
36
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000037/*
38 * timespec based interfaces
39 */
John Stultz334334b2014-11-07 11:20:40 -080040struct timespec64 get_monotonic_coarse64(void);
John Stultzcdba2ec2014-11-07 11:03:20 -080041extern void getrawmonotonic64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000042extern void ktime_get_ts64(struct timespec64 *ts);
Heena Sirwani9e3680b2014-10-29 16:01:16 +053043extern time64_t ktime_get_seconds(void);
Heena Sirwanidbe7aa62014-10-29 16:01:50 +053044extern time64_t ktime_get_real_seconds(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000045
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000046extern int __getnstimeofday64(struct timespec64 *tv);
47extern void getnstimeofday64(struct timespec64 *tv);
John Stultzd08c0cd2014-12-08 12:00:09 -080048extern void getboottime64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000049
50#if BITS_PER_LONG == 64
pang.xunlei21f7eca2014-11-18 19:15:16 +080051/**
52 * Deprecated. Use do_settimeofday64().
53 */
54static inline int do_settimeofday(const struct timespec *ts)
55{
56 return do_settimeofday64(ts);
57}
58
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000059static inline int __getnstimeofday(struct timespec *ts)
60{
61 return __getnstimeofday64(ts);
62}
63
64static inline void getnstimeofday(struct timespec *ts)
65{
66 getnstimeofday64(ts);
67}
68
69static inline void ktime_get_ts(struct timespec *ts)
70{
71 ktime_get_ts64(ts);
72}
73
74static inline void ktime_get_real_ts(struct timespec *ts)
75{
76 getnstimeofday64(ts);
77}
78
John Stultzcdba2ec2014-11-07 11:03:20 -080079static inline void getrawmonotonic(struct timespec *ts)
80{
81 getrawmonotonic64(ts);
82}
83
John Stultz334334b2014-11-07 11:20:40 -080084static inline struct timespec get_monotonic_coarse(void)
85{
86 return get_monotonic_coarse64();
87}
John Stultzd08c0cd2014-12-08 12:00:09 -080088
89static inline void getboottime(struct timespec *ts)
90{
91 return getboottime64(ts);
92}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000093#else
pang.xunlei21f7eca2014-11-18 19:15:16 +080094/**
95 * Deprecated. Use do_settimeofday64().
96 */
97static inline int do_settimeofday(const struct timespec *ts)
98{
99 struct timespec64 ts64;
100
101 ts64 = timespec_to_timespec64(*ts);
102 return do_settimeofday64(&ts64);
103}
104
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000105static inline int __getnstimeofday(struct timespec *ts)
106{
107 struct timespec64 ts64;
108 int ret = __getnstimeofday64(&ts64);
109
110 *ts = timespec64_to_timespec(ts64);
111 return ret;
112}
113
114static inline void getnstimeofday(struct timespec *ts)
115{
116 struct timespec64 ts64;
117
118 getnstimeofday64(&ts64);
119 *ts = timespec64_to_timespec(ts64);
120}
121
122static inline void ktime_get_ts(struct timespec *ts)
123{
124 struct timespec64 ts64;
125
126 ktime_get_ts64(&ts64);
127 *ts = timespec64_to_timespec(ts64);
128}
129
130static inline void ktime_get_real_ts(struct timespec *ts)
131{
132 struct timespec64 ts64;
133
134 getnstimeofday64(&ts64);
135 *ts = timespec64_to_timespec(ts64);
136}
John Stultzcdba2ec2014-11-07 11:03:20 -0800137
138static inline void getrawmonotonic(struct timespec *ts)
139{
140 struct timespec64 ts64;
141
142 getrawmonotonic64(&ts64);
143 *ts = timespec64_to_timespec(ts64);
144}
John Stultz334334b2014-11-07 11:20:40 -0800145
146static inline struct timespec get_monotonic_coarse(void)
147{
148 return timespec64_to_timespec(get_monotonic_coarse64());
149}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000150
John Stultzd08c0cd2014-12-08 12:00:09 -0800151static inline void getboottime(struct timespec *ts)
152{
153 struct timespec64 ts64;
154
155 getboottime64(&ts64);
156 *ts = timespec64_to_timespec(ts64);
157}
158#endif
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000159
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000160#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000161
162/*
163 * ktime_t based interfaces
164 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000165
166enum tk_offsets {
167 TK_OFFS_REAL,
168 TK_OFFS_BOOT,
169 TK_OFFS_TAI,
170 TK_OFFS_MAX,
171};
172
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000173extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000174extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000175extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000176extern ktime_t ktime_get_raw(void);
Harald Geyer6374f912015-04-07 11:12:35 +0000177extern u32 ktime_get_resolution_ns(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000178
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000179/**
180 * ktime_get_real - get the real (wall-) time in ktime_t format
181 */
182static inline ktime_t ktime_get_real(void)
183{
184 return ktime_get_with_offset(TK_OFFS_REAL);
185}
186
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000187/**
188 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
189 *
190 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
191 * time spent in suspend.
192 */
193static inline ktime_t ktime_get_boottime(void)
194{
195 return ktime_get_with_offset(TK_OFFS_BOOT);
196}
197
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000198/**
199 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
200 */
201static inline ktime_t ktime_get_clocktai(void)
202{
203 return ktime_get_with_offset(TK_OFFS_TAI);
204}
205
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000206/**
207 * ktime_mono_to_real - Convert monotonic time to clock realtime
208 */
209static inline ktime_t ktime_mono_to_real(ktime_t mono)
210{
211 return ktime_mono_to_any(mono, TK_OFFS_REAL);
212}
213
Thomas Gleixner897994e2014-07-16 21:04:29 +0000214static inline u64 ktime_get_ns(void)
215{
216 return ktime_to_ns(ktime_get());
217}
218
219static inline u64 ktime_get_real_ns(void)
220{
221 return ktime_to_ns(ktime_get_real());
222}
223
224static inline u64 ktime_get_boot_ns(void)
225{
226 return ktime_to_ns(ktime_get_boottime());
227}
228
Peter Zijlstrafe5fba02015-03-17 12:39:10 +0100229static inline u64 ktime_get_tai_ns(void)
230{
231 return ktime_to_ns(ktime_get_clocktai());
232}
233
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000234static inline u64 ktime_get_raw_ns(void)
235{
236 return ktime_to_ns(ktime_get_raw());
237}
238
Thomas Gleixner4396e052014-07-16 21:05:23 +0000239extern u64 ktime_get_mono_fast_ns(void);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100240extern u64 ktime_get_raw_fast_ns(void);
Joel Fernandes948a5312016-11-28 14:35:22 -0800241extern u64 ktime_get_boot_fast_ns(void);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000242
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000243/*
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000244 * Timespec interfaces utilizing the ktime based ones
245 */
246static inline void get_monotonic_boottime(struct timespec *ts)
247{
248 *ts = ktime_to_timespec(ktime_get_boottime());
249}
250
John Stultz2e0c78e2014-12-18 18:04:34 -0800251static inline void get_monotonic_boottime64(struct timespec64 *ts)
252{
253 *ts = ktime_to_timespec64(ktime_get_boottime());
254}
255
Thomas Gleixner61edec82014-07-16 21:05:01 +0000256static inline void timekeeping_clocktai(struct timespec *ts)
257{
258 *ts = ktime_to_timespec(ktime_get_clocktai());
259}
260
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700261static inline void timekeeping_clocktai64(struct timespec64 *ts)
262{
263 *ts = ktime_to_timespec64(ktime_get_clocktai());
264}
265
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000266/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000267 * RTC specific
268 */
Xunlei Pang0fa88cb2015-04-01 20:34:38 -0700269extern bool timekeeping_rtc_skipsuspend(void);
270extern bool timekeeping_rtc_skipresume(void);
271
pang.xunlei04d90892014-11-18 19:15:17 +0800272extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
273
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000274/*
275 * PPS accessor
276 */
Arnd Bergmann071eee42015-09-28 22:21:29 +0200277extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
278 struct timespec64 *ts_real);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000279
280/*
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800281 * struct system_time_snapshot - simultaneous raw/real time capture with
282 * counter value
283 * @cycles: Clocksource counter value to produce the system times
284 * @real: Realtime system time
285 * @raw: Monotonic raw system time
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800286 * @clock_was_set_seq: The sequence number of clock was set events
287 * @cs_was_changed_seq: The sequence number of clocksource change events
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800288 */
289struct system_time_snapshot {
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100290 u64 cycles;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800291 ktime_t real;
292 ktime_t raw;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800293 unsigned int clock_was_set_seq;
294 u8 cs_was_changed_seq;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800295};
296
297/*
Christopher S. Hall8006c242016-02-22 03:15:22 -0800298 * 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
303 */
304struct system_device_crosststamp {
305 ktime_t device;
306 ktime_t sys_realtime;
307 ktime_t sys_monoraw;
308};
309
310/*
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
316 */
317struct system_counterval_t {
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100318 u64 cycles;
Christopher S. Hall8006c242016-02-22 03:15:22 -0800319 struct clocksource *cs;
320};
321
322/*
323 * Get cross timestamp between system clock and device clock
324 */
325extern int get_device_system_crosststamp(
326 int (*get_time_fn)(ktime_t *device_time,
327 struct system_counterval_t *system_counterval,
328 void *ctx),
329 void *ctx,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800330 struct system_time_snapshot *history,
Christopher S. Hall8006c242016-02-22 03:15:22 -0800331 struct system_device_crosststamp *xtstamp);
332
333/*
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800334 * Simultaneously snapshot realtime and monotonic raw clocks
335 */
336extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
337
338/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000339 * Persistent clock related interfaces
340 */
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000341extern int persistent_clock_is_local;
342
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000343extern void read_persistent_clock(struct timespec *ts);
Xunlei Pang2ee96632015-04-01 20:34:22 -0700344extern void read_persistent_clock64(struct timespec64 *ts);
Xunlei Pang9a806dd2015-04-01 20:34:21 -0700345extern void read_boot_clock64(struct timespec64 *ts);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000346extern int update_persistent_clock(struct timespec now);
Xunlei Pang3c00a1f2015-04-01 20:34:23 -0700347extern int update_persistent_clock64(struct timespec64 now);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000348
349
350#endif