Loading...
1#ifndef _LINUX_TIMEKEEPING_H
2#define _LINUX_TIMEKEEPING_H
3
4#include <linux/errno.h>
5
6/* Included from linux/ktime.h */
7
8void timekeeping_init(void);
9extern int timekeeping_suspended;
10
11/*
12 * Get and set timeofday
13 */
14extern void do_gettimeofday(struct timeval *tv);
15extern int do_settimeofday64(const struct timespec64 *ts);
16extern int do_sys_settimeofday64(const struct timespec64 *tv,
17 const struct timezone *tz);
18static inline int do_sys_settimeofday(const struct timespec *tv,
19 const struct timezone *tz)
20{
21 struct timespec64 ts64;
22
23 if (!tv)
24 return do_sys_settimeofday64(NULL, tz);
25
26 if (!timespec_valid(tv))
27 return -EINVAL;
28
29 ts64 = timespec_to_timespec64(*tv);
30 return do_sys_settimeofday64(&ts64, tz);
31}
32
33/*
34 * Kernel time accessors
35 */
36unsigned long get_seconds(void);
37struct timespec64 current_kernel_time64(void);
38/* does not take xtime_lock */
39struct timespec __current_kernel_time(void);
40
41static inline struct timespec current_kernel_time(void)
42{
43 struct timespec64 now = current_kernel_time64();
44
45 return timespec64_to_timespec(now);
46}
47
48/*
49 * timespec based interfaces
50 */
51struct timespec64 get_monotonic_coarse64(void);
52extern void getrawmonotonic64(struct timespec64 *ts);
53extern void ktime_get_ts64(struct timespec64 *ts);
54extern time64_t ktime_get_seconds(void);
55extern time64_t ktime_get_real_seconds(void);
56
57extern int __getnstimeofday64(struct timespec64 *tv);
58extern void getnstimeofday64(struct timespec64 *tv);
59extern void getboottime64(struct timespec64 *ts);
60
61#if BITS_PER_LONG == 64
62/**
63 * Deprecated. Use do_settimeofday64().
64 */
65static inline int do_settimeofday(const struct timespec *ts)
66{
67 return do_settimeofday64(ts);
68}
69
70static inline int __getnstimeofday(struct timespec *ts)
71{
72 return __getnstimeofday64(ts);
73}
74
75static inline void getnstimeofday(struct timespec *ts)
76{
77 getnstimeofday64(ts);
78}
79
80static inline void ktime_get_ts(struct timespec *ts)
81{
82 ktime_get_ts64(ts);
83}
84
85static inline void ktime_get_real_ts(struct timespec *ts)
86{
87 getnstimeofday64(ts);
88}
89
90static inline void getrawmonotonic(struct timespec *ts)
91{
92 getrawmonotonic64(ts);
93}
94
95static inline struct timespec get_monotonic_coarse(void)
96{
97 return get_monotonic_coarse64();
98}
99
100static inline void getboottime(struct timespec *ts)
101{
102 return getboottime64(ts);
103}
104#else
105/**
106 * Deprecated. Use do_settimeofday64().
107 */
108static inline int do_settimeofday(const struct timespec *ts)
109{
110 struct timespec64 ts64;
111
112 ts64 = timespec_to_timespec64(*ts);
113 return do_settimeofday64(&ts64);
114}
115
116static inline int __getnstimeofday(struct timespec *ts)
117{
118 struct timespec64 ts64;
119 int ret = __getnstimeofday64(&ts64);
120
121 *ts = timespec64_to_timespec(ts64);
122 return ret;
123}
124
125static inline void getnstimeofday(struct timespec *ts)
126{
127 struct timespec64 ts64;
128
129 getnstimeofday64(&ts64);
130 *ts = timespec64_to_timespec(ts64);
131}
132
133static inline void ktime_get_ts(struct timespec *ts)
134{
135 struct timespec64 ts64;
136
137 ktime_get_ts64(&ts64);
138 *ts = timespec64_to_timespec(ts64);
139}
140
141static inline void ktime_get_real_ts(struct timespec *ts)
142{
143 struct timespec64 ts64;
144
145 getnstimeofday64(&ts64);
146 *ts = timespec64_to_timespec(ts64);
147}
148
149static inline void getrawmonotonic(struct timespec *ts)
150{
151 struct timespec64 ts64;
152
153 getrawmonotonic64(&ts64);
154 *ts = timespec64_to_timespec(ts64);
155}
156
157static inline struct timespec get_monotonic_coarse(void)
158{
159 return timespec64_to_timespec(get_monotonic_coarse64());
160}
161
162static inline void getboottime(struct timespec *ts)
163{
164 struct timespec64 ts64;
165
166 getboottime64(&ts64);
167 *ts = timespec64_to_timespec(ts64);
168}
169#endif
170
171#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
172
173/*
174 * ktime_t based interfaces
175 */
176
177enum tk_offsets {
178 TK_OFFS_REAL,
179 TK_OFFS_BOOT,
180 TK_OFFS_TAI,
181 TK_OFFS_MAX,
182};
183
184extern ktime_t ktime_get(void);
185extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
186extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
187extern ktime_t ktime_get_raw(void);
188extern u32 ktime_get_resolution_ns(void);
189
190/**
191 * ktime_get_real - get the real (wall-) time in ktime_t format
192 */
193static inline ktime_t ktime_get_real(void)
194{
195 return ktime_get_with_offset(TK_OFFS_REAL);
196}
197
198/**
199 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
200 *
201 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
202 * time spent in suspend.
203 */
204static inline ktime_t ktime_get_boottime(void)
205{
206 return ktime_get_with_offset(TK_OFFS_BOOT);
207}
208
209/**
210 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
211 */
212static inline ktime_t ktime_get_clocktai(void)
213{
214 return ktime_get_with_offset(TK_OFFS_TAI);
215}
216
217/**
218 * ktime_mono_to_real - Convert monotonic time to clock realtime
219 */
220static inline ktime_t ktime_mono_to_real(ktime_t mono)
221{
222 return ktime_mono_to_any(mono, TK_OFFS_REAL);
223}
224
225static inline u64 ktime_get_ns(void)
226{
227 return ktime_to_ns(ktime_get());
228}
229
230static inline u64 ktime_get_real_ns(void)
231{
232 return ktime_to_ns(ktime_get_real());
233}
234
235static inline u64 ktime_get_boot_ns(void)
236{
237 return ktime_to_ns(ktime_get_boottime());
238}
239
240static inline u64 ktime_get_tai_ns(void)
241{
242 return ktime_to_ns(ktime_get_clocktai());
243}
244
245static inline u64 ktime_get_raw_ns(void)
246{
247 return ktime_to_ns(ktime_get_raw());
248}
249
250extern u64 ktime_get_mono_fast_ns(void);
251extern u64 ktime_get_raw_fast_ns(void);
252extern u64 ktime_get_boot_fast_ns(void);
253
254/*
255 * Timespec interfaces utilizing the ktime based ones
256 */
257static inline void get_monotonic_boottime(struct timespec *ts)
258{
259 *ts = ktime_to_timespec(ktime_get_boottime());
260}
261
262static inline void get_monotonic_boottime64(struct timespec64 *ts)
263{
264 *ts = ktime_to_timespec64(ktime_get_boottime());
265}
266
267static inline void timekeeping_clocktai(struct timespec *ts)
268{
269 *ts = ktime_to_timespec(ktime_get_clocktai());
270}
271
272/*
273 * RTC specific
274 */
275extern bool timekeeping_rtc_skipsuspend(void);
276extern bool timekeeping_rtc_skipresume(void);
277
278extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
279
280/*
281 * PPS accessor
282 */
283extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
284 struct timespec64 *ts_real);
285
286/*
287 * struct system_time_snapshot - simultaneous raw/real time capture with
288 * counter value
289 * @cycles: Clocksource counter value to produce the system times
290 * @real: Realtime system time
291 * @raw: Monotonic raw system time
292 * @clock_was_set_seq: The sequence number of clock was set events
293 * @cs_was_changed_seq: The sequence number of clocksource change events
294 */
295struct system_time_snapshot {
296 u64 cycles;
297 ktime_t real;
298 ktime_t raw;
299 unsigned int clock_was_set_seq;
300 u8 cs_was_changed_seq;
301};
302
303/*
304 * struct system_device_crosststamp - system/device cross-timestamp
305 * (syncronized capture)
306 * @device: Device time
307 * @sys_realtime: Realtime simultaneous with device time
308 * @sys_monoraw: Monotonic raw simultaneous with device time
309 */
310struct system_device_crosststamp {
311 ktime_t device;
312 ktime_t sys_realtime;
313 ktime_t sys_monoraw;
314};
315
316/*
317 * struct system_counterval_t - system counter value with the pointer to the
318 * corresponding clocksource
319 * @cycles: System counter value
320 * @cs: Clocksource corresponding to system counter value. Used by
321 * timekeeping code to verify comparibility of two cycle values
322 */
323struct system_counterval_t {
324 u64 cycles;
325 struct clocksource *cs;
326};
327
328/*
329 * Get cross timestamp between system clock and device clock
330 */
331extern int get_device_system_crosststamp(
332 int (*get_time_fn)(ktime_t *device_time,
333 struct system_counterval_t *system_counterval,
334 void *ctx),
335 void *ctx,
336 struct system_time_snapshot *history,
337 struct system_device_crosststamp *xtstamp);
338
339/*
340 * Simultaneously snapshot realtime and monotonic raw clocks
341 */
342extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
343
344/*
345 * Persistent clock related interfaces
346 */
347extern int persistent_clock_is_local;
348
349extern void read_persistent_clock(struct timespec *ts);
350extern void read_persistent_clock64(struct timespec64 *ts);
351extern void read_boot_clock64(struct timespec64 *ts);
352extern int update_persistent_clock(struct timespec now);
353extern int update_persistent_clock64(struct timespec64 now);
354
355
356#endif
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_TIMEKEEPING_H
3#define _LINUX_TIMEKEEPING_H
4
5#include <linux/errno.h>
6#include <linux/clocksource_ids.h>
7
8/* Included from linux/ktime.h */
9
10void timekeeping_init(void);
11extern int timekeeping_suspended;
12
13/* Architecture timer tick functions: */
14extern void legacy_timer_tick(unsigned long ticks);
15
16/*
17 * Get and set timeofday
18 */
19extern int do_settimeofday64(const struct timespec64 *ts);
20extern int do_sys_settimeofday64(const struct timespec64 *tv,
21 const struct timezone *tz);
22
23/*
24 * ktime_get() family: read the current time in a multitude of ways,
25 *
26 * The default time reference is CLOCK_MONOTONIC, starting at
27 * boot time but not counting the time spent in suspend.
28 * For other references, use the functions with "real", "clocktai",
29 * "boottime" and "raw" suffixes.
30 *
31 * To get the time in a different format, use the ones wit
32 * "ns", "ts64" and "seconds" suffix.
33 *
34 * See Documentation/core-api/timekeeping.rst for more details.
35 */
36
37
38/*
39 * timespec64 based interfaces
40 */
41extern void ktime_get_raw_ts64(struct timespec64 *ts);
42extern void ktime_get_ts64(struct timespec64 *ts);
43extern void ktime_get_real_ts64(struct timespec64 *tv);
44extern void ktime_get_coarse_ts64(struct timespec64 *ts);
45extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
46
47void getboottime64(struct timespec64 *ts);
48
49/*
50 * time64_t base interfaces
51 */
52extern time64_t ktime_get_seconds(void);
53extern time64_t __ktime_get_real_seconds(void);
54extern time64_t ktime_get_real_seconds(void);
55
56/*
57 * ktime_t based interfaces
58 */
59
60enum tk_offsets {
61 TK_OFFS_REAL,
62 TK_OFFS_BOOT,
63 TK_OFFS_TAI,
64 TK_OFFS_MAX,
65};
66
67extern ktime_t ktime_get(void);
68extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
69extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs);
70extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
71extern ktime_t ktime_get_raw(void);
72extern u32 ktime_get_resolution_ns(void);
73
74/**
75 * ktime_get_real - get the real (wall-) time in ktime_t format
76 */
77static inline ktime_t ktime_get_real(void)
78{
79 return ktime_get_with_offset(TK_OFFS_REAL);
80}
81
82static inline ktime_t ktime_get_coarse_real(void)
83{
84 return ktime_get_coarse_with_offset(TK_OFFS_REAL);
85}
86
87/**
88 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
89 *
90 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
91 * time spent in suspend.
92 */
93static inline ktime_t ktime_get_boottime(void)
94{
95 return ktime_get_with_offset(TK_OFFS_BOOT);
96}
97
98static inline ktime_t ktime_get_coarse_boottime(void)
99{
100 return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
101}
102
103/**
104 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
105 */
106static inline ktime_t ktime_get_clocktai(void)
107{
108 return ktime_get_with_offset(TK_OFFS_TAI);
109}
110
111static inline ktime_t ktime_get_coarse_clocktai(void)
112{
113 return ktime_get_coarse_with_offset(TK_OFFS_TAI);
114}
115
116static inline ktime_t ktime_get_coarse(void)
117{
118 struct timespec64 ts;
119
120 ktime_get_coarse_ts64(&ts);
121 return timespec64_to_ktime(ts);
122}
123
124static inline u64 ktime_get_coarse_ns(void)
125{
126 return ktime_to_ns(ktime_get_coarse());
127}
128
129static inline u64 ktime_get_coarse_real_ns(void)
130{
131 return ktime_to_ns(ktime_get_coarse_real());
132}
133
134static inline u64 ktime_get_coarse_boottime_ns(void)
135{
136 return ktime_to_ns(ktime_get_coarse_boottime());
137}
138
139static inline u64 ktime_get_coarse_clocktai_ns(void)
140{
141 return ktime_to_ns(ktime_get_coarse_clocktai());
142}
143
144/**
145 * ktime_mono_to_real - Convert monotonic time to clock realtime
146 */
147static inline ktime_t ktime_mono_to_real(ktime_t mono)
148{
149 return ktime_mono_to_any(mono, TK_OFFS_REAL);
150}
151
152static inline u64 ktime_get_ns(void)
153{
154 return ktime_to_ns(ktime_get());
155}
156
157static inline u64 ktime_get_real_ns(void)
158{
159 return ktime_to_ns(ktime_get_real());
160}
161
162static inline u64 ktime_get_boottime_ns(void)
163{
164 return ktime_to_ns(ktime_get_boottime());
165}
166
167static inline u64 ktime_get_clocktai_ns(void)
168{
169 return ktime_to_ns(ktime_get_clocktai());
170}
171
172static inline u64 ktime_get_raw_ns(void)
173{
174 return ktime_to_ns(ktime_get_raw());
175}
176
177extern u64 ktime_get_mono_fast_ns(void);
178extern u64 ktime_get_raw_fast_ns(void);
179extern u64 ktime_get_boot_fast_ns(void);
180extern u64 ktime_get_tai_fast_ns(void);
181extern u64 ktime_get_real_fast_ns(void);
182
183/*
184 * timespec64/time64_t interfaces utilizing the ktime based ones
185 * for API completeness, these could be implemented more efficiently
186 * if needed.
187 */
188static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
189{
190 *ts = ktime_to_timespec64(ktime_get_boottime());
191}
192
193static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
194{
195 *ts = ktime_to_timespec64(ktime_get_coarse_boottime());
196}
197
198static inline time64_t ktime_get_boottime_seconds(void)
199{
200 return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
201}
202
203static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
204{
205 *ts = ktime_to_timespec64(ktime_get_clocktai());
206}
207
208static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
209{
210 *ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
211}
212
213static inline time64_t ktime_get_clocktai_seconds(void)
214{
215 return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
216}
217
218/*
219 * RTC specific
220 */
221extern bool timekeeping_rtc_skipsuspend(void);
222extern bool timekeeping_rtc_skipresume(void);
223
224extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
225
226/*
227 * struct ktime_timestanps - Simultaneous mono/boot/real timestamps
228 * @mono: Monotonic timestamp
229 * @boot: Boottime timestamp
230 * @real: Realtime timestamp
231 */
232struct ktime_timestamps {
233 u64 mono;
234 u64 boot;
235 u64 real;
236};
237
238/**
239 * struct system_time_snapshot - simultaneous raw/real time capture with
240 * counter value
241 * @cycles: Clocksource counter value to produce the system times
242 * @real: Realtime system time
243 * @raw: Monotonic raw system time
244 * @clock_was_set_seq: The sequence number of clock was set events
245 * @cs_was_changed_seq: The sequence number of clocksource change events
246 */
247struct system_time_snapshot {
248 u64 cycles;
249 ktime_t real;
250 ktime_t raw;
251 enum clocksource_ids cs_id;
252 unsigned int clock_was_set_seq;
253 u8 cs_was_changed_seq;
254};
255
256/**
257 * struct system_device_crosststamp - system/device cross-timestamp
258 * (synchronized capture)
259 * @device: Device time
260 * @sys_realtime: Realtime simultaneous with device time
261 * @sys_monoraw: Monotonic raw simultaneous with device time
262 */
263struct system_device_crosststamp {
264 ktime_t device;
265 ktime_t sys_realtime;
266 ktime_t sys_monoraw;
267};
268
269/**
270 * struct system_counterval_t - system counter value with the pointer to the
271 * corresponding clocksource
272 * @cycles: System counter value
273 * @cs: Clocksource corresponding to system counter value. Used by
274 * timekeeping code to verify comparibility of two cycle values
275 */
276struct system_counterval_t {
277 u64 cycles;
278 struct clocksource *cs;
279};
280
281/*
282 * Get cross timestamp between system clock and device clock
283 */
284extern int get_device_system_crosststamp(
285 int (*get_time_fn)(ktime_t *device_time,
286 struct system_counterval_t *system_counterval,
287 void *ctx),
288 void *ctx,
289 struct system_time_snapshot *history,
290 struct system_device_crosststamp *xtstamp);
291
292/*
293 * Simultaneously snapshot realtime and monotonic raw clocks
294 */
295extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
296
297/* NMI safe mono/boot/realtime timestamps */
298extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap);
299
300/*
301 * Persistent clock related interfaces
302 */
303extern int persistent_clock_is_local;
304
305extern void read_persistent_clock64(struct timespec64 *ts);
306void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
307 struct timespec64 *boot_offset);
308#ifdef CONFIG_GENERIC_CMOS_UPDATE
309extern int update_persistent_clock64(struct timespec64 now);
310#endif
311
312#endif