Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_TIMEKEEPING_H
3#define _LINUX_TIMEKEEPING_H
4
5#include <linux/errno.h>
6
7/* Included from linux/ktime.h */
8
9void timekeeping_init(void);
10extern int timekeeping_suspended;
11
12/* Architecture timer tick functions: */
13extern void update_process_times(int user);
14extern void xtime_update(unsigned long ticks);
15
16/*
17 * Get and set timeofday
18 */
19extern void do_gettimeofday(struct timeval *tv);
20extern int do_settimeofday64(const struct timespec64 *ts);
21extern int do_sys_settimeofday64(const struct timespec64 *tv,
22 const struct timezone *tz);
23/*
24 * Kernel time accessors
25 */
26unsigned long get_seconds(void);
27struct timespec64 current_kernel_time64(void);
28/* does not take xtime_lock */
29struct timespec __current_kernel_time(void);
30
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
38/*
39 * timespec based interfaces
40 */
41struct timespec64 get_monotonic_coarse64(void);
42extern void getrawmonotonic64(struct timespec64 *ts);
43extern void ktime_get_ts64(struct timespec64 *ts);
44extern time64_t ktime_get_seconds(void);
45extern time64_t ktime_get_real_seconds(void);
46
47extern int __getnstimeofday64(struct timespec64 *tv);
48extern void getnstimeofday64(struct timespec64 *tv);
49extern void getboottime64(struct timespec64 *ts);
50
51#if BITS_PER_LONG == 64
52/**
53 * Deprecated. Use do_settimeofday64().
54 */
55static inline int do_settimeofday(const struct timespec *ts)
56{
57 return do_settimeofday64(ts);
58}
59
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
80static inline void getrawmonotonic(struct timespec *ts)
81{
82 getrawmonotonic64(ts);
83}
84
85static inline struct timespec get_monotonic_coarse(void)
86{
87 return get_monotonic_coarse64();
88}
89
90static inline void getboottime(struct timespec *ts)
91{
92 return getboottime64(ts);
93}
94#else
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
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}
138
139static inline void getrawmonotonic(struct timespec *ts)
140{
141 struct timespec64 ts64;
142
143 getrawmonotonic64(&ts64);
144 *ts = timespec64_to_timespec(ts64);
145}
146
147static inline struct timespec get_monotonic_coarse(void)
148{
149 return timespec64_to_timespec(get_monotonic_coarse64());
150}
151
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
160
161#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
162
163/*
164 * ktime_t based interfaces
165 */
166
167enum tk_offsets {
168 TK_OFFS_REAL,
169 TK_OFFS_BOOT,
170 TK_OFFS_TAI,
171 TK_OFFS_MAX,
172};
173
174extern ktime_t ktime_get(void);
175extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
176extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
177extern ktime_t ktime_get_raw(void);
178extern u32 ktime_get_resolution_ns(void);
179
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
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
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
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
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
230static inline u64 ktime_get_tai_ns(void)
231{
232 return ktime_to_ns(ktime_get_clocktai());
233}
234
235static inline u64 ktime_get_raw_ns(void)
236{
237 return ktime_to_ns(ktime_get_raw());
238}
239
240extern u64 ktime_get_mono_fast_ns(void);
241extern u64 ktime_get_raw_fast_ns(void);
242extern u64 ktime_get_boot_fast_ns(void);
243
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
252static inline void get_monotonic_boottime64(struct timespec64 *ts)
253{
254 *ts = ktime_to_timespec64(ktime_get_boottime());
255}
256
257static inline void timekeeping_clocktai(struct timespec *ts)
258{
259 *ts = ktime_to_timespec(ktime_get_clocktai());
260}
261
262static inline void timekeeping_clocktai64(struct timespec64 *ts)
263{
264 *ts = ktime_to_timespec64(ktime_get_clocktai());
265}
266
267/*
268 * RTC specific
269 */
270extern bool timekeeping_rtc_skipsuspend(void);
271extern bool timekeeping_rtc_skipresume(void);
272
273extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
274
275/*
276 * PPS accessor
277 */
278extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
279 struct timespec64 *ts_real);
280
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
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
289 */
290struct system_time_snapshot {
291 u64 cycles;
292 ktime_t real;
293 ktime_t raw;
294 unsigned int clock_was_set_seq;
295 u8 cs_was_changed_seq;
296};
297
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 {
319 u64 cycles;
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,
331 struct system_time_snapshot *history,
332 struct system_device_crosststamp *xtstamp);
333
334/*
335 * Simultaneously snapshot realtime and monotonic raw clocks
336 */
337extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
338
339/*
340 * Persistent clock related interfaces
341 */
342extern int persistent_clock_is_local;
343
344extern void read_persistent_clock(struct timespec *ts);
345extern void read_persistent_clock64(struct timespec64 *ts);
346extern void read_boot_clock64(struct timespec64 *ts);
347extern int update_persistent_clock(struct timespec now);
348extern int update_persistent_clock64(struct timespec64 now);
349
350
351#endif