blob: e40a8d60fb21adee2ace5fc7684475a6991b15d4 [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 */
28struct timespec get_monotonic_coarse(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
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000069#else
pang.xunlei21f7eca2014-11-18 19:15:16 +080070/**
71 * Deprecated. Use do_settimeofday64().
72 */
73static inline int do_settimeofday(const struct timespec *ts)
74{
75 struct timespec64 ts64;
76
77 ts64 = timespec_to_timespec64(*ts);
78 return do_settimeofday64(&ts64);
79}
80
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000081static inline int __getnstimeofday(struct timespec *ts)
82{
83 struct timespec64 ts64;
84 int ret = __getnstimeofday64(&ts64);
85
86 *ts = timespec64_to_timespec(ts64);
87 return ret;
88}
89
90static inline void getnstimeofday(struct timespec *ts)
91{
92 struct timespec64 ts64;
93
94 getnstimeofday64(&ts64);
95 *ts = timespec64_to_timespec(ts64);
96}
97
98static inline void ktime_get_ts(struct timespec *ts)
99{
100 struct timespec64 ts64;
101
102 ktime_get_ts64(&ts64);
103 *ts = timespec64_to_timespec(ts64);
104}
105
106static inline void ktime_get_real_ts(struct timespec *ts)
107{
108 struct timespec64 ts64;
109
110 getnstimeofday64(&ts64);
111 *ts = timespec64_to_timespec(ts64);
112}
John Stultzcdba2ec2014-11-07 11:03:20 -0800113
114static inline void getrawmonotonic(struct timespec *ts)
115{
116 struct timespec64 ts64;
117
118 getrawmonotonic64(&ts64);
119 *ts = timespec64_to_timespec(ts64);
120}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000121#endif
122
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000123extern void getboottime(struct timespec *ts);
124
125#define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000126#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000127
128/*
129 * ktime_t based interfaces
130 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000131
132enum tk_offsets {
133 TK_OFFS_REAL,
134 TK_OFFS_BOOT,
135 TK_OFFS_TAI,
136 TK_OFFS_MAX,
137};
138
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000139extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000140extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000141extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000142extern ktime_t ktime_get_raw(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000143
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000144/**
145 * ktime_get_real - get the real (wall-) time in ktime_t format
146 */
147static inline ktime_t ktime_get_real(void)
148{
149 return ktime_get_with_offset(TK_OFFS_REAL);
150}
151
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000152/**
153 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
154 *
155 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
156 * time spent in suspend.
157 */
158static inline ktime_t ktime_get_boottime(void)
159{
160 return ktime_get_with_offset(TK_OFFS_BOOT);
161}
162
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000163/**
164 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
165 */
166static inline ktime_t ktime_get_clocktai(void)
167{
168 return ktime_get_with_offset(TK_OFFS_TAI);
169}
170
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000171/**
172 * ktime_mono_to_real - Convert monotonic time to clock realtime
173 */
174static inline ktime_t ktime_mono_to_real(ktime_t mono)
175{
176 return ktime_mono_to_any(mono, TK_OFFS_REAL);
177}
178
Thomas Gleixner897994e2014-07-16 21:04:29 +0000179static inline u64 ktime_get_ns(void)
180{
181 return ktime_to_ns(ktime_get());
182}
183
184static inline u64 ktime_get_real_ns(void)
185{
186 return ktime_to_ns(ktime_get_real());
187}
188
189static inline u64 ktime_get_boot_ns(void)
190{
191 return ktime_to_ns(ktime_get_boottime());
192}
193
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000194static inline u64 ktime_get_raw_ns(void)
195{
196 return ktime_to_ns(ktime_get_raw());
197}
198
Thomas Gleixner4396e052014-07-16 21:05:23 +0000199extern u64 ktime_get_mono_fast_ns(void);
200
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000201/*
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000202 * Timespec interfaces utilizing the ktime based ones
203 */
204static inline void get_monotonic_boottime(struct timespec *ts)
205{
206 *ts = ktime_to_timespec(ktime_get_boottime());
207}
208
Thomas Gleixner61edec82014-07-16 21:05:01 +0000209static inline void timekeeping_clocktai(struct timespec *ts)
210{
211 *ts = ktime_to_timespec(ktime_get_clocktai());
212}
213
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000214/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000215 * RTC specific
216 */
pang.xunlei04d90892014-11-18 19:15:17 +0800217extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
218
219/**
220 * Deprecated. Use timekeeping_inject_sleeptime64().
221 */
222static inline void timekeeping_inject_sleeptime(struct timespec *delta)
223{
224 struct timespec64 delta64;
225
226 delta64 = timespec_to_timespec64(*delta);
227 timekeeping_inject_sleeptime64(&delta64);
228}
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000229
230/*
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