]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - include/linux/timekeeping.h
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[thirdparty/kernel/linux.git] / include / linux / timekeeping.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
8b094cd0
TG
2#ifndef _LINUX_TIMEKEEPING_H
3#define _LINUX_TIMEKEEPING_H
4
b536fd58 5#include <linux/errno.h>
86d34732 6
8b094cd0
TG
7/* Included from linux/ktime.h */
8
9void timekeeping_init(void);
10extern int timekeeping_suspended;
11
93b5a9a7
IM
12/* Architecture timer tick functions: */
13extern void update_process_times(int user);
14extern void xtime_update(unsigned long ticks);
15
8b094cd0
TG
16/*
17 * Get and set timeofday
18 */
19extern void do_gettimeofday(struct timeval *tv);
21f7eca5 20extern int do_settimeofday64(const struct timespec64 *ts);
86d34732
BW
21extern int do_sys_settimeofday64(const struct timespec64 *tv,
22 const struct timezone *tz);
8b094cd0
TG
23/*
24 * Kernel time accessors
25 */
26unsigned long get_seconds(void);
8758a240 27struct timespec64 current_kernel_time64(void);
8b094cd0
TG
28/* does not take xtime_lock */
29struct timespec __current_kernel_time(void);
30
8758a240
BW
31static inline struct timespec current_kernel_time(void)
32{
33 struct timespec64 now = current_kernel_time64();
34
35 return timespec64_to_timespec(now);
36}
37
8b094cd0
TG
38/*
39 * timespec based interfaces
40 */
334334b5 41struct timespec64 get_monotonic_coarse64(void);
cdba2ec5 42extern void getrawmonotonic64(struct timespec64 *ts);
d6d29896 43extern void ktime_get_ts64(struct timespec64 *ts);
9e3680b1 44extern time64_t ktime_get_seconds(void);
dbe7aa62 45extern time64_t ktime_get_real_seconds(void);
d6d29896
TG
46
47extern int __getnstimeofday64(struct timespec64 *tv);
48extern void getnstimeofday64(struct timespec64 *tv);
d08c0cdd 49extern void getboottime64(struct timespec64 *ts);
d6d29896
TG
50
51#if BITS_PER_LONG == 64
21f7eca5 52/**
53 * Deprecated. Use do_settimeofday64().
54 */
55static inline int do_settimeofday(const struct timespec *ts)
56{
57 return do_settimeofday64(ts);
58}
59
d6d29896
TG
60static inline int __getnstimeofday(struct timespec *ts)
61{
62 return __getnstimeofday64(ts);
63}
64
65static inline void getnstimeofday(struct timespec *ts)
66{
67 getnstimeofday64(ts);
68}
69
70static inline void ktime_get_ts(struct timespec *ts)
71{
72 ktime_get_ts64(ts);
73}
74
75static inline void ktime_get_real_ts(struct timespec *ts)
76{
77 getnstimeofday64(ts);
78}
79
cdba2ec5
JS
80static inline void getrawmonotonic(struct timespec *ts)
81{
82 getrawmonotonic64(ts);
83}
84
334334b5
JS
85static inline struct timespec get_monotonic_coarse(void)
86{
87 return get_monotonic_coarse64();
88}
d08c0cdd
JS
89
90static inline void getboottime(struct timespec *ts)
91{
92 return getboottime64(ts);
93}
d6d29896 94#else
21f7eca5 95/**
96 * Deprecated. Use do_settimeofday64().
97 */
98static inline int do_settimeofday(const struct timespec *ts)
99{
100 struct timespec64 ts64;
101
102 ts64 = timespec_to_timespec64(*ts);
103 return do_settimeofday64(&ts64);
104}
105
d6d29896
TG
106static inline int __getnstimeofday(struct timespec *ts)
107{
108 struct timespec64 ts64;
109 int ret = __getnstimeofday64(&ts64);
110
111 *ts = timespec64_to_timespec(ts64);
112 return ret;
113}
114
115static inline void getnstimeofday(struct timespec *ts)
116{
117 struct timespec64 ts64;
118
119 getnstimeofday64(&ts64);
120 *ts = timespec64_to_timespec(ts64);
121}
122
123static inline void ktime_get_ts(struct timespec *ts)
124{
125 struct timespec64 ts64;
126
127 ktime_get_ts64(&ts64);
128 *ts = timespec64_to_timespec(ts64);
129}
130
131static inline void ktime_get_real_ts(struct timespec *ts)
132{
133 struct timespec64 ts64;
134
135 getnstimeofday64(&ts64);
136 *ts = timespec64_to_timespec(ts64);
137}
cdba2ec5
JS
138
139static inline void getrawmonotonic(struct timespec *ts)
140{
141 struct timespec64 ts64;
142
143 getrawmonotonic64(&ts64);
144 *ts = timespec64_to_timespec(ts64);
145}
334334b5
JS
146
147static inline struct timespec get_monotonic_coarse(void)
148{
149 return timespec64_to_timespec(get_monotonic_coarse64());
150}
8b094cd0 151
d08c0cdd
JS
152static inline void getboottime(struct timespec *ts)
153{
154 struct timespec64 ts64;
155
156 getboottime64(&ts64);
157 *ts = timespec64_to_timespec(ts64);
158}
159#endif
8b094cd0 160
d6d29896 161#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
8b094cd0
TG
162
163/*
164 * ktime_t based interfaces
165 */
0077dc60
TG
166
167enum tk_offsets {
168 TK_OFFS_REAL,
169 TK_OFFS_BOOT,
170 TK_OFFS_TAI,
171 TK_OFFS_MAX,
172};
173
8b094cd0 174extern ktime_t ktime_get(void);
0077dc60 175extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
9a6b5197 176extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
f519b1a2 177extern ktime_t ktime_get_raw(void);
6374f912 178extern u32 ktime_get_resolution_ns(void);
8b094cd0 179
f5264d5d
TG
180/**
181 * ktime_get_real - get the real (wall-) time in ktime_t format
182 */
183static inline ktime_t ktime_get_real(void)
184{
185 return ktime_get_with_offset(TK_OFFS_REAL);
186}
187
b82c817e
TG
188/**
189 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
190 *
191 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
192 * time spent in suspend.
193 */
194static inline ktime_t ktime_get_boottime(void)
195{
196 return ktime_get_with_offset(TK_OFFS_BOOT);
197}
198
afab07c0
TG
199/**
200 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
201 */
202static inline ktime_t ktime_get_clocktai(void)
203{
204 return ktime_get_with_offset(TK_OFFS_TAI);
205}
206
9a6b5197
TG
207/**
208 * ktime_mono_to_real - Convert monotonic time to clock realtime
209 */
210static inline ktime_t ktime_mono_to_real(ktime_t mono)
211{
212 return ktime_mono_to_any(mono, TK_OFFS_REAL);
213}
214
897994e3
TG
215static inline u64 ktime_get_ns(void)
216{
217 return ktime_to_ns(ktime_get());
218}
219
220static inline u64 ktime_get_real_ns(void)
221{
222 return ktime_to_ns(ktime_get_real());
223}
224
225static inline u64 ktime_get_boot_ns(void)
226{
227 return ktime_to_ns(ktime_get_boottime());
228}
229
fe5fba05
PZ
230static inline u64 ktime_get_tai_ns(void)
231{
232 return ktime_to_ns(ktime_get_clocktai());
233}
234
f519b1a2
TG
235static inline u64 ktime_get_raw_ns(void)
236{
237 return ktime_to_ns(ktime_get_raw());
238}
239
4396e058 240extern u64 ktime_get_mono_fast_ns(void);
f09cb9a1 241extern u64 ktime_get_raw_fast_ns(void);
948a5312 242extern u64 ktime_get_boot_fast_ns(void);
4396e058 243
48f18fd6
TG
244/*
245 * Timespec interfaces utilizing the ktime based ones
246 */
247static inline void get_monotonic_boottime(struct timespec *ts)
248{
249 *ts = ktime_to_timespec(ktime_get_boottime());
250}
251
2e0c78ee
JS
252static inline void get_monotonic_boottime64(struct timespec64 *ts)
253{
254 *ts = ktime_to_timespec64(ktime_get_boottime());
255}
256
61edec81
TG
257static inline void timekeeping_clocktai(struct timespec *ts)
258{
259 *ts = ktime_to_timespec(ktime_get_clocktai());
260}
261
3c9c12f4
DD
262static inline void timekeeping_clocktai64(struct timespec64 *ts)
263{
264 *ts = ktime_to_timespec64(ktime_get_clocktai());
265}
266
8b094cd0
TG
267/*
268 * RTC specific
269 */
0fa88cb4
XP
270extern bool timekeeping_rtc_skipsuspend(void);
271extern bool timekeeping_rtc_skipresume(void);
272
04d90890 273extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
274
8b094cd0
TG
275/*
276 * PPS accessor
277 */
071eee45
AB
278extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
279 struct timespec64 *ts_real);
8b094cd0 280
9da0f49c
CH
281/*
282 * struct system_time_snapshot - simultaneous raw/real time capture with
283 * counter value
284 * @cycles: Clocksource counter value to produce the system times
285 * @real: Realtime system time
286 * @raw: Monotonic raw system time
2c756feb
CH
287 * @clock_was_set_seq: The sequence number of clock was set events
288 * @cs_was_changed_seq: The sequence number of clocksource change events
9da0f49c
CH
289 */
290struct system_time_snapshot {
a5a1d1c2 291 u64 cycles;
9da0f49c
CH
292 ktime_t real;
293 ktime_t raw;
2c756feb
CH
294 unsigned int clock_was_set_seq;
295 u8 cs_was_changed_seq;
9da0f49c
CH
296};
297
8006c245
CH
298/*
299 * struct system_device_crosststamp - system/device cross-timestamp
300 * (syncronized capture)
301 * @device: Device time
302 * @sys_realtime: Realtime simultaneous with device time
303 * @sys_monoraw: Monotonic raw simultaneous with device time
304 */
305struct system_device_crosststamp {
306 ktime_t device;
307 ktime_t sys_realtime;
308 ktime_t sys_monoraw;
309};
310
311/*
312 * struct system_counterval_t - system counter value with the pointer to the
313 * corresponding clocksource
314 * @cycles: System counter value
315 * @cs: Clocksource corresponding to system counter value. Used by
316 * timekeeping code to verify comparibility of two cycle values
317 */
318struct system_counterval_t {
a5a1d1c2 319 u64 cycles;
8006c245
CH
320 struct clocksource *cs;
321};
322
323/*
324 * Get cross timestamp between system clock and device clock
325 */
326extern int get_device_system_crosststamp(
327 int (*get_time_fn)(ktime_t *device_time,
328 struct system_counterval_t *system_counterval,
329 void *ctx),
330 void *ctx,
2c756feb 331 struct system_time_snapshot *history,
8006c245
CH
332 struct system_device_crosststamp *xtstamp);
333
9da0f49c
CH
334/*
335 * Simultaneously snapshot realtime and monotonic raw clocks
336 */
337extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
338
8b094cd0
TG
339/*
340 * Persistent clock related interfaces
341 */
8b094cd0
TG
342extern int persistent_clock_is_local;
343
8b094cd0 344extern void read_persistent_clock(struct timespec *ts);
2ee96632 345extern void read_persistent_clock64(struct timespec64 *ts);
9a806ddb 346extern void read_boot_clock64(struct timespec64 *ts);
8b094cd0 347extern int update_persistent_clock(struct timespec now);
3c00a1fe 348extern int update_persistent_clock64(struct timespec64 now);
8b094cd0
TG
349
350
351#endif