blob: 115d55e11bc99cc2b55d0f155c99cfd2c2900443 [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);
13extern int do_settimeofday(const struct timespec *tv);
14extern 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);
29extern void getrawmonotonic(struct timespec *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);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000032
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000033extern int __getnstimeofday64(struct timespec64 *tv);
34extern void getnstimeofday64(struct timespec64 *tv);
35
36#if BITS_PER_LONG == 64
37static inline int __getnstimeofday(struct timespec *ts)
38{
39 return __getnstimeofday64(ts);
40}
41
42static inline void getnstimeofday(struct timespec *ts)
43{
44 getnstimeofday64(ts);
45}
46
47static inline void ktime_get_ts(struct timespec *ts)
48{
49 ktime_get_ts64(ts);
50}
51
52static inline void ktime_get_real_ts(struct timespec *ts)
53{
54 getnstimeofday64(ts);
55}
56
57#else
58static inline int __getnstimeofday(struct timespec *ts)
59{
60 struct timespec64 ts64;
61 int ret = __getnstimeofday64(&ts64);
62
63 *ts = timespec64_to_timespec(ts64);
64 return ret;
65}
66
67static inline void getnstimeofday(struct timespec *ts)
68{
69 struct timespec64 ts64;
70
71 getnstimeofday64(&ts64);
72 *ts = timespec64_to_timespec(ts64);
73}
74
75static inline void ktime_get_ts(struct timespec *ts)
76{
77 struct timespec64 ts64;
78
79 ktime_get_ts64(&ts64);
80 *ts = timespec64_to_timespec(ts64);
81}
82
83static inline void ktime_get_real_ts(struct timespec *ts)
84{
85 struct timespec64 ts64;
86
87 getnstimeofday64(&ts64);
88 *ts = timespec64_to_timespec(ts64);
89}
90#endif
91
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000092extern void getboottime(struct timespec *ts);
93
94#define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000095#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000096
97/*
98 * ktime_t based interfaces
99 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000100
101enum tk_offsets {
102 TK_OFFS_REAL,
103 TK_OFFS_BOOT,
104 TK_OFFS_TAI,
105 TK_OFFS_MAX,
106};
107
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000108extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000109extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000110extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000111extern ktime_t ktime_get_raw(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000112
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000113/**
114 * ktime_get_real - get the real (wall-) time in ktime_t format
115 */
116static inline ktime_t ktime_get_real(void)
117{
118 return ktime_get_with_offset(TK_OFFS_REAL);
119}
120
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000121/**
122 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
123 *
124 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
125 * time spent in suspend.
126 */
127static inline ktime_t ktime_get_boottime(void)
128{
129 return ktime_get_with_offset(TK_OFFS_BOOT);
130}
131
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000132/**
133 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
134 */
135static inline ktime_t ktime_get_clocktai(void)
136{
137 return ktime_get_with_offset(TK_OFFS_TAI);
138}
139
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000140/**
141 * ktime_mono_to_real - Convert monotonic time to clock realtime
142 */
143static inline ktime_t ktime_mono_to_real(ktime_t mono)
144{
145 return ktime_mono_to_any(mono, TK_OFFS_REAL);
146}
147
Thomas Gleixner897994e2014-07-16 21:04:29 +0000148static inline u64 ktime_get_ns(void)
149{
150 return ktime_to_ns(ktime_get());
151}
152
153static inline u64 ktime_get_real_ns(void)
154{
155 return ktime_to_ns(ktime_get_real());
156}
157
158static inline u64 ktime_get_boot_ns(void)
159{
160 return ktime_to_ns(ktime_get_boottime());
161}
162
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000163static inline u64 ktime_get_raw_ns(void)
164{
165 return ktime_to_ns(ktime_get_raw());
166}
167
Thomas Gleixner4396e052014-07-16 21:05:23 +0000168extern u64 ktime_get_mono_fast_ns(void);
169
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000170/*
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000171 * Timespec interfaces utilizing the ktime based ones
172 */
173static inline void get_monotonic_boottime(struct timespec *ts)
174{
175 *ts = ktime_to_timespec(ktime_get_boottime());
176}
177
Thomas Gleixner61edec82014-07-16 21:05:01 +0000178static inline void timekeeping_clocktai(struct timespec *ts)
179{
180 *ts = ktime_to_timespec(ktime_get_clocktai());
181}
182
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000183/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000184 * RTC specific
185 */
186extern void timekeeping_inject_sleeptime(struct timespec *delta);
187
188/*
189 * PPS accessor
190 */
191extern void getnstime_raw_and_real(struct timespec *ts_raw,
192 struct timespec *ts_real);
193
194/*
195 * Persistent clock related interfaces
196 */
197extern bool persistent_clock_exist;
198extern int persistent_clock_is_local;
199
200static inline bool has_persistent_clock(void)
201{
202 return persistent_clock_exist;
203}
204
205extern void read_persistent_clock(struct timespec *ts);
206extern void read_boot_clock(struct timespec *ts);
207extern int update_persistent_clock(struct timespec now);
208
209
210#endif