blob: 3aa72e64865021ef0efd15c0511fee55a520c19e [file] [log] [blame]
Thomas Gleixner8b094cd2014-07-16 21:04:02 +00001#ifndef _LINUX_TIMEKEEPING_H
2#define _LINUX_TIMEKEEPING_H
3
4/* Included from linux/ktime.h */
5
6void timekeeping_init(void);
7extern int timekeeping_suspended;
8
9/*
10 * Get and set timeofday
11 */
12extern void do_gettimeofday(struct timeval *tv);
pang.xunlei21f7eca2014-11-18 19:15:16 +080013extern int do_settimeofday64(const struct timespec64 *ts);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000014extern int do_sys_settimeofday(const struct timespec *tv,
15 const struct timezone *tz);
16
17/*
18 * Kernel time accessors
19 */
20unsigned long get_seconds(void);
21struct timespec current_kernel_time(void);
22/* does not take xtime_lock */
23struct timespec __current_kernel_time(void);
24
25/*
26 * timespec based interfaces
27 */
John Stultz334334b2014-11-07 11:20:40 -080028struct timespec64 get_monotonic_coarse64(void);
John Stultzcdba2ec2014-11-07 11:03:20 -080029extern void getrawmonotonic64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000030extern void ktime_get_ts64(struct timespec64 *ts);
Heena Sirwani9e3680b2014-10-29 16:01:16 +053031extern time64_t ktime_get_seconds(void);
Heena Sirwanidbe7aa62014-10-29 16:01:50 +053032extern time64_t ktime_get_real_seconds(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000033
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000034extern int __getnstimeofday64(struct timespec64 *tv);
35extern void getnstimeofday64(struct timespec64 *tv);
John Stultzd08c0cd2014-12-08 12:00:09 -080036extern void getboottime64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000037
38#if BITS_PER_LONG == 64
pang.xunlei21f7eca2014-11-18 19:15:16 +080039/**
40 * Deprecated. Use do_settimeofday64().
41 */
42static inline int do_settimeofday(const struct timespec *ts)
43{
44 return do_settimeofday64(ts);
45}
46
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000047static inline int __getnstimeofday(struct timespec *ts)
48{
49 return __getnstimeofday64(ts);
50}
51
52static inline void getnstimeofday(struct timespec *ts)
53{
54 getnstimeofday64(ts);
55}
56
57static inline void ktime_get_ts(struct timespec *ts)
58{
59 ktime_get_ts64(ts);
60}
61
62static inline void ktime_get_real_ts(struct timespec *ts)
63{
64 getnstimeofday64(ts);
65}
66
John Stultzcdba2ec2014-11-07 11:03:20 -080067static inline void getrawmonotonic(struct timespec *ts)
68{
69 getrawmonotonic64(ts);
70}
71
John Stultz334334b2014-11-07 11:20:40 -080072static inline struct timespec get_monotonic_coarse(void)
73{
74 return get_monotonic_coarse64();
75}
John Stultzd08c0cd2014-12-08 12:00:09 -080076
77static inline void getboottime(struct timespec *ts)
78{
79 return getboottime64(ts);
80}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000081#else
pang.xunlei21f7eca2014-11-18 19:15:16 +080082/**
83 * Deprecated. Use do_settimeofday64().
84 */
85static inline int do_settimeofday(const struct timespec *ts)
86{
87 struct timespec64 ts64;
88
89 ts64 = timespec_to_timespec64(*ts);
90 return do_settimeofday64(&ts64);
91}
92
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000093static inline int __getnstimeofday(struct timespec *ts)
94{
95 struct timespec64 ts64;
96 int ret = __getnstimeofday64(&ts64);
97
98 *ts = timespec64_to_timespec(ts64);
99 return ret;
100}
101
102static inline void getnstimeofday(struct timespec *ts)
103{
104 struct timespec64 ts64;
105
106 getnstimeofday64(&ts64);
107 *ts = timespec64_to_timespec(ts64);
108}
109
110static inline void ktime_get_ts(struct timespec *ts)
111{
112 struct timespec64 ts64;
113
114 ktime_get_ts64(&ts64);
115 *ts = timespec64_to_timespec(ts64);
116}
117
118static inline void ktime_get_real_ts(struct timespec *ts)
119{
120 struct timespec64 ts64;
121
122 getnstimeofday64(&ts64);
123 *ts = timespec64_to_timespec(ts64);
124}
John Stultzcdba2ec2014-11-07 11:03:20 -0800125
126static inline void getrawmonotonic(struct timespec *ts)
127{
128 struct timespec64 ts64;
129
130 getrawmonotonic64(&ts64);
131 *ts = timespec64_to_timespec(ts64);
132}
John Stultz334334b2014-11-07 11:20:40 -0800133
134static inline struct timespec get_monotonic_coarse(void)
135{
136 return timespec64_to_timespec(get_monotonic_coarse64());
137}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000138
John Stultzd08c0cd2014-12-08 12:00:09 -0800139static inline void getboottime(struct timespec *ts)
140{
141 struct timespec64 ts64;
142
143 getboottime64(&ts64);
144 *ts = timespec64_to_timespec(ts64);
145}
146#endif
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000147
148#define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000149#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000150
151/*
152 * ktime_t based interfaces
153 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000154
155enum tk_offsets {
156 TK_OFFS_REAL,
157 TK_OFFS_BOOT,
158 TK_OFFS_TAI,
159 TK_OFFS_MAX,
160};
161
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000162extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000163extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000164extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000165extern ktime_t ktime_get_raw(void);
Harald Geyer6374f912015-04-07 11:12:35 +0000166extern u32 ktime_get_resolution_ns(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000167
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000168/**
169 * ktime_get_real - get the real (wall-) time in ktime_t format
170 */
171static inline ktime_t ktime_get_real(void)
172{
173 return ktime_get_with_offset(TK_OFFS_REAL);
174}
175
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000176/**
177 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
178 *
179 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
180 * time spent in suspend.
181 */
182static inline ktime_t ktime_get_boottime(void)
183{
184 return ktime_get_with_offset(TK_OFFS_BOOT);
185}
186
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000187/**
188 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
189 */
190static inline ktime_t ktime_get_clocktai(void)
191{
192 return ktime_get_with_offset(TK_OFFS_TAI);
193}
194
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000195/**
196 * ktime_mono_to_real - Convert monotonic time to clock realtime
197 */
198static inline ktime_t ktime_mono_to_real(ktime_t mono)
199{
200 return ktime_mono_to_any(mono, TK_OFFS_REAL);
201}
202
Thomas Gleixner897994e2014-07-16 21:04:29 +0000203static inline u64 ktime_get_ns(void)
204{
205 return ktime_to_ns(ktime_get());
206}
207
208static inline u64 ktime_get_real_ns(void)
209{
210 return ktime_to_ns(ktime_get_real());
211}
212
213static inline u64 ktime_get_boot_ns(void)
214{
215 return ktime_to_ns(ktime_get_boottime());
216}
217
Peter Zijlstrafe5fba02015-03-17 12:39:10 +0100218static inline u64 ktime_get_tai_ns(void)
219{
220 return ktime_to_ns(ktime_get_clocktai());
221}
222
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000223static inline u64 ktime_get_raw_ns(void)
224{
225 return ktime_to_ns(ktime_get_raw());
226}
227
Thomas Gleixner4396e052014-07-16 21:05:23 +0000228extern u64 ktime_get_mono_fast_ns(void);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100229extern u64 ktime_get_raw_fast_ns(void);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000230
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000231/*
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000232 * Timespec interfaces utilizing the ktime based ones
233 */
234static inline void get_monotonic_boottime(struct timespec *ts)
235{
236 *ts = ktime_to_timespec(ktime_get_boottime());
237}
238
John Stultz2e0c78e2014-12-18 18:04:34 -0800239static inline void get_monotonic_boottime64(struct timespec64 *ts)
240{
241 *ts = ktime_to_timespec64(ktime_get_boottime());
242}
243
Thomas Gleixner61edec82014-07-16 21:05:01 +0000244static inline void timekeeping_clocktai(struct timespec *ts)
245{
246 *ts = ktime_to_timespec(ktime_get_clocktai());
247}
248
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000249/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000250 * RTC specific
251 */
Xunlei Pang0fa88cb2015-04-01 20:34:38 -0700252extern bool timekeeping_rtc_skipsuspend(void);
253extern bool timekeeping_rtc_skipresume(void);
254
pang.xunlei04d90892014-11-18 19:15:17 +0800255extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
256
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000257/*
258 * PPS accessor
259 */
260extern void getnstime_raw_and_real(struct timespec *ts_raw,
261 struct timespec *ts_real);
262
263/*
264 * Persistent clock related interfaces
265 */
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000266extern int persistent_clock_is_local;
267
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000268extern void read_persistent_clock(struct timespec *ts);
Xunlei Pang2ee96632015-04-01 20:34:22 -0700269extern void read_persistent_clock64(struct timespec64 *ts);
Xunlei Pang9a806dd2015-04-01 20:34:21 -0700270extern void read_boot_clock64(struct timespec64 *ts);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000271extern int update_persistent_clock(struct timespec now);
Xunlei Pang3c00a1f2015-04-01 20:34:23 -0700272extern int update_persistent_clock64(struct timespec64 now);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000273
274
275#endif