blob: 9b63d13ba82b3748cc2fd5b61992fde76d8859a2 [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);
36
37#if BITS_PER_LONG == 64
pang.xunlei21f7eca2014-11-18 19:15:16 +080038/**
39 * Deprecated. Use do_settimeofday64().
40 */
41static inline int do_settimeofday(const struct timespec *ts)
42{
43 return do_settimeofday64(ts);
44}
45
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000046static inline int __getnstimeofday(struct timespec *ts)
47{
48 return __getnstimeofday64(ts);
49}
50
51static inline void getnstimeofday(struct timespec *ts)
52{
53 getnstimeofday64(ts);
54}
55
56static inline void ktime_get_ts(struct timespec *ts)
57{
58 ktime_get_ts64(ts);
59}
60
61static inline void ktime_get_real_ts(struct timespec *ts)
62{
63 getnstimeofday64(ts);
64}
65
John Stultzcdba2ec2014-11-07 11:03:20 -080066static inline void getrawmonotonic(struct timespec *ts)
67{
68 getrawmonotonic64(ts);
69}
70
John Stultz334334b2014-11-07 11:20:40 -080071static inline struct timespec get_monotonic_coarse(void)
72{
73 return get_monotonic_coarse64();
74}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000075#else
pang.xunlei21f7eca2014-11-18 19:15:16 +080076/**
77 * Deprecated. Use do_settimeofday64().
78 */
79static inline int do_settimeofday(const struct timespec *ts)
80{
81 struct timespec64 ts64;
82
83 ts64 = timespec_to_timespec64(*ts);
84 return do_settimeofday64(&ts64);
85}
86
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000087static inline int __getnstimeofday(struct timespec *ts)
88{
89 struct timespec64 ts64;
90 int ret = __getnstimeofday64(&ts64);
91
92 *ts = timespec64_to_timespec(ts64);
93 return ret;
94}
95
96static inline void getnstimeofday(struct timespec *ts)
97{
98 struct timespec64 ts64;
99
100 getnstimeofday64(&ts64);
101 *ts = timespec64_to_timespec(ts64);
102}
103
104static inline void ktime_get_ts(struct timespec *ts)
105{
106 struct timespec64 ts64;
107
108 ktime_get_ts64(&ts64);
109 *ts = timespec64_to_timespec(ts64);
110}
111
112static inline void ktime_get_real_ts(struct timespec *ts)
113{
114 struct timespec64 ts64;
115
116 getnstimeofday64(&ts64);
117 *ts = timespec64_to_timespec(ts64);
118}
John Stultzcdba2ec2014-11-07 11:03:20 -0800119
120static inline void getrawmonotonic(struct timespec *ts)
121{
122 struct timespec64 ts64;
123
124 getrawmonotonic64(&ts64);
125 *ts = timespec64_to_timespec(ts64);
126}
John Stultz334334b2014-11-07 11:20:40 -0800127
128static inline struct timespec get_monotonic_coarse(void)
129{
130 return timespec64_to_timespec(get_monotonic_coarse64());
131}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000132#endif
133
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000134extern void getboottime(struct timespec *ts);
135
136#define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000137#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000138
139/*
140 * ktime_t based interfaces
141 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000142
143enum tk_offsets {
144 TK_OFFS_REAL,
145 TK_OFFS_BOOT,
146 TK_OFFS_TAI,
147 TK_OFFS_MAX,
148};
149
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000150extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000151extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000152extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000153extern ktime_t ktime_get_raw(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000154
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000155/**
156 * ktime_get_real - get the real (wall-) time in ktime_t format
157 */
158static inline ktime_t ktime_get_real(void)
159{
160 return ktime_get_with_offset(TK_OFFS_REAL);
161}
162
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000163/**
164 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
165 *
166 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
167 * time spent in suspend.
168 */
169static inline ktime_t ktime_get_boottime(void)
170{
171 return ktime_get_with_offset(TK_OFFS_BOOT);
172}
173
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000174/**
175 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
176 */
177static inline ktime_t ktime_get_clocktai(void)
178{
179 return ktime_get_with_offset(TK_OFFS_TAI);
180}
181
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000182/**
183 * ktime_mono_to_real - Convert monotonic time to clock realtime
184 */
185static inline ktime_t ktime_mono_to_real(ktime_t mono)
186{
187 return ktime_mono_to_any(mono, TK_OFFS_REAL);
188}
189
Thomas Gleixner897994e2014-07-16 21:04:29 +0000190static inline u64 ktime_get_ns(void)
191{
192 return ktime_to_ns(ktime_get());
193}
194
195static inline u64 ktime_get_real_ns(void)
196{
197 return ktime_to_ns(ktime_get_real());
198}
199
200static inline u64 ktime_get_boot_ns(void)
201{
202 return ktime_to_ns(ktime_get_boottime());
203}
204
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000205static inline u64 ktime_get_raw_ns(void)
206{
207 return ktime_to_ns(ktime_get_raw());
208}
209
Thomas Gleixner4396e052014-07-16 21:05:23 +0000210extern u64 ktime_get_mono_fast_ns(void);
211
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000212/*
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000213 * Timespec interfaces utilizing the ktime based ones
214 */
215static inline void get_monotonic_boottime(struct timespec *ts)
216{
217 *ts = ktime_to_timespec(ktime_get_boottime());
218}
219
Thomas Gleixner61edec82014-07-16 21:05:01 +0000220static inline void timekeeping_clocktai(struct timespec *ts)
221{
222 *ts = ktime_to_timespec(ktime_get_clocktai());
223}
224
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000225/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000226 * RTC specific
227 */
pang.xunlei04d90892014-11-18 19:15:17 +0800228extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
229
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000230/*
231 * PPS accessor
232 */
233extern void getnstime_raw_and_real(struct timespec *ts_raw,
234 struct timespec *ts_real);
235
236/*
237 * Persistent clock related interfaces
238 */
239extern bool persistent_clock_exist;
240extern int persistent_clock_is_local;
241
242static inline bool has_persistent_clock(void)
243{
244 return persistent_clock_exist;
245}
246
247extern void read_persistent_clock(struct timespec *ts);
248extern void read_boot_clock(struct timespec *ts);
249extern int update_persistent_clock(struct timespec now);
250
251
252#endif