blob: 8ea3ca1b0ee526a79a7b6f20b839acdd13a690a9 [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 Gleixner8b094cd2014-07-16 21:04:02 +000030extern void get_monotonic_boottime(struct timespec *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000031extern void ktime_get_ts64(struct timespec64 *ts);
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 Gleixner8b094cd2014-07-16 21:04:02 +0000111
Thomas Gleixnerf5264d52014-07-16 21:04:14 +0000112/**
113 * ktime_get_real - get the real (wall-) time in ktime_t format
114 */
115static inline ktime_t ktime_get_real(void)
116{
117 return ktime_get_with_offset(TK_OFFS_REAL);
118}
119
Thomas Gleixnerb82c8172014-07-16 21:04:16 +0000120/**
121 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
122 *
123 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
124 * time spent in suspend.
125 */
126static inline ktime_t ktime_get_boottime(void)
127{
128 return ktime_get_with_offset(TK_OFFS_BOOT);
129}
130
Thomas Gleixnerafab07c2014-07-16 21:04:17 +0000131/**
132 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
133 */
134static inline ktime_t ktime_get_clocktai(void)
135{
136 return ktime_get_with_offset(TK_OFFS_TAI);
137}
138
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000139/**
140 * ktime_mono_to_real - Convert monotonic time to clock realtime
141 */
142static inline ktime_t ktime_mono_to_real(ktime_t mono)
143{
144 return ktime_mono_to_any(mono, TK_OFFS_REAL);
145}
146
Thomas Gleixner897994e2014-07-16 21:04:29 +0000147static inline u64 ktime_get_ns(void)
148{
149 return ktime_to_ns(ktime_get());
150}
151
152static inline u64 ktime_get_real_ns(void)
153{
154 return ktime_to_ns(ktime_get_real());
155}
156
157static inline u64 ktime_get_boot_ns(void)
158{
159 return ktime_to_ns(ktime_get_boottime());
160}
161
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000162/*
163 * RTC specific
164 */
165extern void timekeeping_inject_sleeptime(struct timespec *delta);
166
167/*
168 * PPS accessor
169 */
170extern void getnstime_raw_and_real(struct timespec *ts_raw,
171 struct timespec *ts_real);
172
173/*
174 * Persistent clock related interfaces
175 */
176extern bool persistent_clock_exist;
177extern int persistent_clock_is_local;
178
179static inline bool has_persistent_clock(void)
180{
181 return persistent_clock_exist;
182}
183
184extern void read_persistent_clock(struct timespec *ts);
185extern void read_boot_clock(struct timespec *ts);
186extern int update_persistent_clock(struct timespec now);
187
188
189#endif