]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - include/linux/timekeeping.h
UBUNTU: Ubuntu-5.11.0-22.23
[mirror_ubuntu-hirsute-kernel.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 12/* Architecture timer tick functions: */
b3550164 13extern void legacy_timer_tick(unsigned long ticks);
93b5a9a7 14
8b094cd0
TG
15/*
16 * Get and set timeofday
17 */
21f7eca5 18extern int do_settimeofday64(const struct timespec64 *ts);
86d34732
BW
19extern int do_sys_settimeofday64(const struct timespec64 *tv,
20 const struct timezone *tz);
8758a240 21
0e3fd810
AB
22/*
23 * ktime_get() family: read the current time in a multitude of ways,
24 *
25 * The default time reference is CLOCK_MONOTONIC, starting at
26 * boot time but not counting the time spent in suspend.
27 * For other references, use the functions with "real", "clocktai",
28 * "boottime" and "raw" suffixes.
29 *
30 * To get the time in a different format, use the ones wit
31 * "ns", "ts64" and "seconds" suffix.
32 *
33 * See Documentation/core-api/timekeeping.rst for more details.
34 */
35
36
8b094cd0 37/*
6546911e 38 * timespec64 based interfaces
8b094cd0 39 */
fb7fcc96 40extern void ktime_get_raw_ts64(struct timespec64 *ts);
d6d29896 41extern void ktime_get_ts64(struct timespec64 *ts);
edca71fe 42extern void ktime_get_real_ts64(struct timespec64 *tv);
fb7fcc96
AB
43extern void ktime_get_coarse_ts64(struct timespec64 *ts);
44extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
45
46void getboottime64(struct timespec64 *ts);
47
48/*
49 * time64_t base interfaces
50 */
9e3680b1 51extern time64_t ktime_get_seconds(void);
6909e29f 52extern time64_t __ktime_get_real_seconds(void);
dbe7aa62 53extern time64_t ktime_get_real_seconds(void);
d6d29896 54
8b094cd0
TG
55/*
56 * ktime_t based interfaces
57 */
a3ed0e43 58
0077dc60
TG
59enum tk_offsets {
60 TK_OFFS_REAL,
a3ed0e43 61 TK_OFFS_BOOT,
0077dc60
TG
62 TK_OFFS_TAI,
63 TK_OFFS_MAX,
64};
65
8b094cd0 66extern ktime_t ktime_get(void);
0077dc60 67extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
b9ff604c 68extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs);
9a6b5197 69extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
f519b1a2 70extern ktime_t ktime_get_raw(void);
6374f912 71extern u32 ktime_get_resolution_ns(void);
8b094cd0 72
f5264d5d
TG
73/**
74 * ktime_get_real - get the real (wall-) time in ktime_t format
75 */
76static inline ktime_t ktime_get_real(void)
77{
78 return ktime_get_with_offset(TK_OFFS_REAL);
79}
80
b9ff604c
AB
81static inline ktime_t ktime_get_coarse_real(void)
82{
83 return ktime_get_coarse_with_offset(TK_OFFS_REAL);
84}
85
a3ed0e43
TG
86/**
87 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
88 *
89 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
90 * time spent in suspend.
91 */
92static inline ktime_t ktime_get_boottime(void)
93{
94 return ktime_get_with_offset(TK_OFFS_BOOT);
95}
96
b9ff604c
AB
97static inline ktime_t ktime_get_coarse_boottime(void)
98{
99 return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
100}
101
afab07c0
TG
102/**
103 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
104 */
105static inline ktime_t ktime_get_clocktai(void)
106{
107 return ktime_get_with_offset(TK_OFFS_TAI);
108}
109
b9ff604c
AB
110static inline ktime_t ktime_get_coarse_clocktai(void)
111{
112 return ktime_get_coarse_with_offset(TK_OFFS_TAI);
113}
114
4c54294d
JD
115static inline ktime_t ktime_get_coarse(void)
116{
117 struct timespec64 ts;
118
119 ktime_get_coarse_ts64(&ts);
120 return timespec64_to_ktime(ts);
121}
122
123static inline u64 ktime_get_coarse_ns(void)
124{
125 return ktime_to_ns(ktime_get_coarse());
126}
127
128static inline u64 ktime_get_coarse_real_ns(void)
129{
130 return ktime_to_ns(ktime_get_coarse_real());
131}
132
d48e0cd8 133static inline u64 ktime_get_coarse_boottime_ns(void)
4c54294d
JD
134{
135 return ktime_to_ns(ktime_get_coarse_boottime());
136}
137
138static inline u64 ktime_get_coarse_clocktai_ns(void)
139{
140 return ktime_to_ns(ktime_get_coarse_clocktai());
141}
142
9a6b5197
TG
143/**
144 * ktime_mono_to_real - Convert monotonic time to clock realtime
145 */
146static inline ktime_t ktime_mono_to_real(ktime_t mono)
147{
148 return ktime_mono_to_any(mono, TK_OFFS_REAL);
149}
150
897994e3
TG
151static inline u64 ktime_get_ns(void)
152{
153 return ktime_to_ns(ktime_get());
154}
155
156static inline u64 ktime_get_real_ns(void)
157{
158 return ktime_to_ns(ktime_get_real());
159}
160
9285ec4c 161static inline u64 ktime_get_boottime_ns(void)
a3ed0e43
TG
162{
163 return ktime_to_ns(ktime_get_boottime());
164}
165
9285ec4c 166static inline u64 ktime_get_clocktai_ns(void)
fe5fba05
PZ
167{
168 return ktime_to_ns(ktime_get_clocktai());
169}
170
f519b1a2
TG
171static inline u64 ktime_get_raw_ns(void)
172{
173 return ktime_to_ns(ktime_get_raw());
174}
175
4396e058 176extern u64 ktime_get_mono_fast_ns(void);
f09cb9a1 177extern u64 ktime_get_raw_fast_ns(void);
a3ed0e43 178extern u64 ktime_get_boot_fast_ns(void);
4c3711d7 179extern u64 ktime_get_real_fast_ns(void);
4396e058 180
d6c7270e 181/*
06aa3769
AB
182 * timespec64/time64_t interfaces utilizing the ktime based ones
183 * for API completeness, these could be implemented more efficiently
184 * if needed.
d6c7270e 185 */
fb7fcc96 186static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
a3ed0e43
TG
187{
188 *ts = ktime_to_timespec64(ktime_get_boottime());
189}
190
06aa3769
AB
191static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
192{
193 *ts = ktime_to_timespec64(ktime_get_coarse_boottime());
194}
195
196static inline time64_t ktime_get_boottime_seconds(void)
197{
198 return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
199}
200
fb7fcc96 201static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
3c9c12f4
DD
202{
203 *ts = ktime_to_timespec64(ktime_get_clocktai());
204}
205
06aa3769
AB
206static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
207{
208 *ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
209}
210
211static inline time64_t ktime_get_clocktai_seconds(void)
212{
213 return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
214}
215
8b094cd0
TG
216/*
217 * RTC specific
218 */
0fa88cb4
XP
219extern bool timekeeping_rtc_skipsuspend(void);
220extern bool timekeeping_rtc_skipresume(void);
221
985e6950 222extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
04d90890 223
e2d977c9
TG
224/*
225 * struct ktime_timestanps - Simultaneous mono/boot/real timestamps
226 * @mono: Monotonic timestamp
227 * @boot: Boottime timestamp
228 * @real: Realtime timestamp
229 */
230struct ktime_timestamps {
231 u64 mono;
232 u64 boot;
233 u64 real;
234};
235
f097eb38 236/**
9da0f49c 237 * struct system_time_snapshot - simultaneous raw/real time capture with
f097eb38 238 * counter value
9da0f49c
CH
239 * @cycles: Clocksource counter value to produce the system times
240 * @real: Realtime system time
241 * @raw: Monotonic raw system time
2c756feb
CH
242 * @clock_was_set_seq: The sequence number of clock was set events
243 * @cs_was_changed_seq: The sequence number of clocksource change events
9da0f49c
CH
244 */
245struct system_time_snapshot {
a5a1d1c2 246 u64 cycles;
9da0f49c
CH
247 ktime_t real;
248 ktime_t raw;
2c756feb
CH
249 unsigned int clock_was_set_seq;
250 u8 cs_was_changed_seq;
9da0f49c
CH
251};
252
f097eb38 253/**
8006c245 254 * struct system_device_crosststamp - system/device cross-timestamp
f097eb38 255 * (synchronized capture)
8006c245
CH
256 * @device: Device time
257 * @sys_realtime: Realtime simultaneous with device time
258 * @sys_monoraw: Monotonic raw simultaneous with device time
259 */
260struct system_device_crosststamp {
261 ktime_t device;
262 ktime_t sys_realtime;
263 ktime_t sys_monoraw;
264};
265
f097eb38 266/**
8006c245 267 * struct system_counterval_t - system counter value with the pointer to the
f097eb38 268 * corresponding clocksource
8006c245
CH
269 * @cycles: System counter value
270 * @cs: Clocksource corresponding to system counter value. Used by
f097eb38 271 * timekeeping code to verify comparibility of two cycle values
8006c245
CH
272 */
273struct system_counterval_t {
a5a1d1c2 274 u64 cycles;
8006c245
CH
275 struct clocksource *cs;
276};
277
278/*
279 * Get cross timestamp between system clock and device clock
280 */
281extern int get_device_system_crosststamp(
282 int (*get_time_fn)(ktime_t *device_time,
283 struct system_counterval_t *system_counterval,
284 void *ctx),
285 void *ctx,
2c756feb 286 struct system_time_snapshot *history,
8006c245
CH
287 struct system_device_crosststamp *xtstamp);
288
9da0f49c
CH
289/*
290 * Simultaneously snapshot realtime and monotonic raw clocks
291 */
292extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
293
e2d977c9
TG
294/* NMI safe mono/boot/realtime timestamps */
295extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap);
296
8b094cd0
TG
297/*
298 * Persistent clock related interfaces
299 */
8b094cd0
TG
300extern int persistent_clock_is_local;
301
2ee96632 302extern void read_persistent_clock64(struct timespec64 *ts);
c43c5e9f
CB
303void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
304 struct timespec64 *boot_offset);
3cabca87 305#ifdef CONFIG_GENERIC_CMOS_UPDATE
3c00a1fe 306extern int update_persistent_clock64(struct timespec64 now);
3cabca87 307#endif
8b094cd0 308
8b094cd0 309#endif