blob: 8cab4b754be76be957968970ef11ad642a8f4247 [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);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000031
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000032extern int __getnstimeofday64(struct timespec64 *tv);
33extern void getnstimeofday64(struct timespec64 *tv);
34
35#if BITS_PER_LONG == 64
pang.xunlei21f7eca2014-11-18 19:15:16 +080036/**
37 * Deprecated. Use do_settimeofday64().
38 */
39static inline int do_settimeofday(const struct timespec *ts)
40{
41 return do_settimeofday64(ts);
42}
43
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000044static inline int __getnstimeofday(struct timespec *ts)
45{
46 return __getnstimeofday64(ts);
47}
48
49static inline void getnstimeofday(struct timespec *ts)
50{
51 getnstimeofday64(ts);
52}
53
54static inline void ktime_get_ts(struct timespec *ts)
55{
56 ktime_get_ts64(ts);
57}
58
59static inline void ktime_get_real_ts(struct timespec *ts)
60{
61 getnstimeofday64(ts);
62}
63
John Stultzcdba2ec2014-11-07 11:03:20 -080064static inline void getrawmonotonic(struct timespec *ts)
65{
66 getrawmonotonic64(ts);
67}
68
John Stultz334334b2014-11-07 11:20:40 -080069static inline struct timespec get_monotonic_coarse(void)
70{
71 return get_monotonic_coarse64();
72}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000073#else
pang.xunlei21f7eca2014-11-18 19:15:16 +080074/**
75 * Deprecated. Use do_settimeofday64().
76 */
77static inline int do_settimeofday(const struct timespec *ts)
78{
79 struct timespec64 ts64;
80
81 ts64 = timespec_to_timespec64(*ts);
82 return do_settimeofday64(&ts64);
83}
84
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000085static inline int __getnstimeofday(struct timespec *ts)
86{
87 struct timespec64 ts64;
88 int ret = __getnstimeofday64(&ts64);
89
90 *ts = timespec64_to_timespec(ts64);
91 return ret;
92}
93
94static inline void getnstimeofday(struct timespec *ts)
95{
96 struct timespec64 ts64;
97
98 getnstimeofday64(&ts64);
99 *ts = timespec64_to_timespec(ts64);
100}
101
102static inline void ktime_get_ts(struct timespec *ts)
103{
104 struct timespec64 ts64;
105
106 ktime_get_ts64(&ts64);
107 *ts = timespec64_to_timespec(ts64);
108}
109
110static inline void ktime_get_real_ts(struct timespec *ts)
111{
112 struct timespec64 ts64;
113
114 getnstimeofday64(&ts64);
115 *ts = timespec64_to_timespec(ts64);
116}
John Stultzcdba2ec2014-11-07 11:03:20 -0800117
118static inline void getrawmonotonic(struct timespec *ts)
119{
120 struct timespec64 ts64;
121
122 getrawmonotonic64(&ts64);
123 *ts = timespec64_to_timespec(ts64);
124}
John Stultz334334b2014-11-07 11:20:40 -0800125
126static inline struct timespec get_monotonic_coarse(void)
127{
128 return timespec64_to_timespec(get_monotonic_coarse64());
129}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000130#endif
131
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000132extern void getboottime(struct timespec *ts);
133
134#define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000135#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000136
137/*
138 * ktime_t based interfaces
139 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000140
141enum tk_offsets {
142 TK_OFFS_REAL,
143 TK_OFFS_BOOT,
144 TK_OFFS_TAI,
145 TK_OFFS_MAX,
146};
147
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000148extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000149extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000150extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000151extern ktime_t ktime_get_raw(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000152
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000153/**
154 * ktime_get_real - get the real (wall-) time in ktime_t format
155 */
156static inline ktime_t ktime_get_real(void)
157{
158 return ktime_get_with_offset(TK_OFFS_REAL);
159}
160
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000161/**
162 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
163 *
164 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
165 * time spent in suspend.
166 */
167static inline ktime_t ktime_get_boottime(void)
168{
169 return ktime_get_with_offset(TK_OFFS_BOOT);
170}
171
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000172/**
173 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
174 */
175static inline ktime_t ktime_get_clocktai(void)
176{
177 return ktime_get_with_offset(TK_OFFS_TAI);
178}
179
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000180/**
181 * ktime_mono_to_real - Convert monotonic time to clock realtime
182 */
183static inline ktime_t ktime_mono_to_real(ktime_t mono)
184{
185 return ktime_mono_to_any(mono, TK_OFFS_REAL);
186}
187
Thomas Gleixner897994e2014-07-16 21:04:29 +0000188static inline u64 ktime_get_ns(void)
189{
190 return ktime_to_ns(ktime_get());
191}
192
193static inline u64 ktime_get_real_ns(void)
194{
195 return ktime_to_ns(ktime_get_real());
196}
197
198static inline u64 ktime_get_boot_ns(void)
199{
200 return ktime_to_ns(ktime_get_boottime());
201}
202
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000203static inline u64 ktime_get_raw_ns(void)
204{
205 return ktime_to_ns(ktime_get_raw());
206}
207
Thomas Gleixner4396e052014-07-16 21:05:23 +0000208extern u64 ktime_get_mono_fast_ns(void);
209
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000210/*
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000211 * Timespec interfaces utilizing the ktime based ones
212 */
213static inline void get_monotonic_boottime(struct timespec *ts)
214{
215 *ts = ktime_to_timespec(ktime_get_boottime());
216}
217
Thomas Gleixner61edec82014-07-16 21:05:01 +0000218static inline void timekeeping_clocktai(struct timespec *ts)
219{
220 *ts = ktime_to_timespec(ktime_get_clocktai());
221}
222
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000223/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000224 * RTC specific
225 */
pang.xunlei04d90892014-11-18 19:15:17 +0800226extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
227
228/**
229 * Deprecated. Use timekeeping_inject_sleeptime64().
230 */
231static inline void timekeeping_inject_sleeptime(struct timespec *delta)
232{
233 struct timespec64 delta64;
234
235 delta64 = timespec_to_timespec64(*delta);
236 timekeeping_inject_sleeptime64(&delta64);
237}
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000238
239/*
240 * PPS accessor
241 */
242extern void getnstime_raw_and_real(struct timespec *ts_raw,
243 struct timespec *ts_real);
244
245/*
246 * Persistent clock related interfaces
247 */
248extern bool persistent_clock_exist;
249extern int persistent_clock_is_local;
250
251static inline bool has_persistent_clock(void)
252{
253 return persistent_clock_exist;
254}
255
256extern void read_persistent_clock(struct timespec *ts);
257extern void read_boot_clock(struct timespec *ts);
258extern int update_persistent_clock(struct timespec now);
259
260
261#endif