Commit | Line | Data |
---|---|---|
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 | ||
9 | void timekeeping_init(void); | |
10 | extern int timekeeping_suspended; | |
11 | ||
93b5a9a7 IM |
12 | /* Architecture timer tick functions: */ |
13 | extern void update_process_times(int user); | |
14 | extern void xtime_update(unsigned long ticks); | |
15 | ||
8b094cd0 TG |
16 | /* |
17 | * Get and set timeofday | |
18 | */ | |
21f7eca5 | 19 | extern int do_settimeofday64(const struct timespec64 *ts); |
86d34732 BW |
20 | extern int do_sys_settimeofday64(const struct timespec64 *tv, |
21 | const struct timezone *tz); | |
8758a240 | 22 | |
0e3fd810 AB |
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 | ||
8b094cd0 | 38 | /* |
6546911e | 39 | * timespec64 based interfaces |
8b094cd0 | 40 | */ |
fb7fcc96 | 41 | extern void ktime_get_raw_ts64(struct timespec64 *ts); |
d6d29896 | 42 | extern void ktime_get_ts64(struct timespec64 *ts); |
edca71fe | 43 | extern void ktime_get_real_ts64(struct timespec64 *tv); |
fb7fcc96 AB |
44 | extern void ktime_get_coarse_ts64(struct timespec64 *ts); |
45 | extern void ktime_get_coarse_real_ts64(struct timespec64 *ts); | |
46 | ||
47 | void getboottime64(struct timespec64 *ts); | |
48 | ||
49 | /* | |
50 | * time64_t base interfaces | |
51 | */ | |
9e3680b1 | 52 | extern time64_t ktime_get_seconds(void); |
6909e29f | 53 | extern time64_t __ktime_get_real_seconds(void); |
dbe7aa62 | 54 | extern time64_t ktime_get_real_seconds(void); |
d6d29896 | 55 | |
8b094cd0 TG |
56 | /* |
57 | * ktime_t based interfaces | |
58 | */ | |
a3ed0e43 | 59 | |
0077dc60 TG |
60 | enum tk_offsets { |
61 | TK_OFFS_REAL, | |
a3ed0e43 | 62 | TK_OFFS_BOOT, |
0077dc60 TG |
63 | TK_OFFS_TAI, |
64 | TK_OFFS_MAX, | |
65 | }; | |
66 | ||
8b094cd0 | 67 | extern ktime_t ktime_get(void); |
0077dc60 | 68 | extern ktime_t ktime_get_with_offset(enum tk_offsets offs); |
b9ff604c | 69 | extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs); |
9a6b5197 | 70 | extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs); |
f519b1a2 | 71 | extern ktime_t ktime_get_raw(void); |
6374f912 | 72 | extern u32 ktime_get_resolution_ns(void); |
8b094cd0 | 73 | |
f5264d5d TG |
74 | /** |
75 | * ktime_get_real - get the real (wall-) time in ktime_t format | |
76 | */ | |
77 | static inline ktime_t ktime_get_real(void) | |
78 | { | |
79 | return ktime_get_with_offset(TK_OFFS_REAL); | |
80 | } | |
81 | ||
b9ff604c AB |
82 | static inline ktime_t ktime_get_coarse_real(void) |
83 | { | |
84 | return ktime_get_coarse_with_offset(TK_OFFS_REAL); | |
85 | } | |
86 | ||
a3ed0e43 TG |
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 | */ | |
93 | static inline ktime_t ktime_get_boottime(void) | |
94 | { | |
95 | return ktime_get_with_offset(TK_OFFS_BOOT); | |
96 | } | |
97 | ||
b9ff604c AB |
98 | static inline ktime_t ktime_get_coarse_boottime(void) |
99 | { | |
100 | return ktime_get_coarse_with_offset(TK_OFFS_BOOT); | |
101 | } | |
102 | ||
afab07c0 TG |
103 | /** |
104 | * ktime_get_clocktai - Returns the TAI time of day in ktime_t format | |
105 | */ | |
106 | static inline ktime_t ktime_get_clocktai(void) | |
107 | { | |
108 | return ktime_get_with_offset(TK_OFFS_TAI); | |
109 | } | |
110 | ||
b9ff604c AB |
111 | static inline ktime_t ktime_get_coarse_clocktai(void) |
112 | { | |
113 | return ktime_get_coarse_with_offset(TK_OFFS_TAI); | |
114 | } | |
115 | ||
9a6b5197 TG |
116 | /** |
117 | * ktime_mono_to_real - Convert monotonic time to clock realtime | |
118 | */ | |
119 | static inline ktime_t ktime_mono_to_real(ktime_t mono) | |
120 | { | |
121 | return ktime_mono_to_any(mono, TK_OFFS_REAL); | |
122 | } | |
123 | ||
897994e3 TG |
124 | static inline u64 ktime_get_ns(void) |
125 | { | |
126 | return ktime_to_ns(ktime_get()); | |
127 | } | |
128 | ||
129 | static inline u64 ktime_get_real_ns(void) | |
130 | { | |
131 | return ktime_to_ns(ktime_get_real()); | |
132 | } | |
133 | ||
a3ed0e43 TG |
134 | static inline u64 ktime_get_boot_ns(void) |
135 | { | |
136 | return ktime_to_ns(ktime_get_boottime()); | |
137 | } | |
138 | ||
fe5fba05 PZ |
139 | static inline u64 ktime_get_tai_ns(void) |
140 | { | |
141 | return ktime_to_ns(ktime_get_clocktai()); | |
142 | } | |
143 | ||
f519b1a2 TG |
144 | static inline u64 ktime_get_raw_ns(void) |
145 | { | |
146 | return ktime_to_ns(ktime_get_raw()); | |
147 | } | |
148 | ||
4396e058 | 149 | extern u64 ktime_get_mono_fast_ns(void); |
f09cb9a1 | 150 | extern u64 ktime_get_raw_fast_ns(void); |
a3ed0e43 | 151 | extern u64 ktime_get_boot_fast_ns(void); |
4c3711d7 | 152 | extern u64 ktime_get_real_fast_ns(void); |
4396e058 | 153 | |
d6c7270e | 154 | /* |
06aa3769 AB |
155 | * timespec64/time64_t interfaces utilizing the ktime based ones |
156 | * for API completeness, these could be implemented more efficiently | |
157 | * if needed. | |
d6c7270e | 158 | */ |
fb7fcc96 | 159 | static inline void ktime_get_boottime_ts64(struct timespec64 *ts) |
a3ed0e43 TG |
160 | { |
161 | *ts = ktime_to_timespec64(ktime_get_boottime()); | |
162 | } | |
163 | ||
06aa3769 AB |
164 | static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts) |
165 | { | |
166 | *ts = ktime_to_timespec64(ktime_get_coarse_boottime()); | |
167 | } | |
168 | ||
169 | static inline time64_t ktime_get_boottime_seconds(void) | |
170 | { | |
171 | return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC); | |
172 | } | |
173 | ||
fb7fcc96 | 174 | static inline void ktime_get_clocktai_ts64(struct timespec64 *ts) |
3c9c12f4 DD |
175 | { |
176 | *ts = ktime_to_timespec64(ktime_get_clocktai()); | |
177 | } | |
178 | ||
06aa3769 AB |
179 | static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts) |
180 | { | |
181 | *ts = ktime_to_timespec64(ktime_get_coarse_clocktai()); | |
182 | } | |
183 | ||
184 | static inline time64_t ktime_get_clocktai_seconds(void) | |
185 | { | |
186 | return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC); | |
187 | } | |
188 | ||
8b094cd0 TG |
189 | /* |
190 | * RTC specific | |
191 | */ | |
0fa88cb4 XP |
192 | extern bool timekeeping_rtc_skipsuspend(void); |
193 | extern bool timekeeping_rtc_skipresume(void); | |
194 | ||
985e6950 | 195 | extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta); |
04d90890 | 196 | |
9da0f49c CH |
197 | /* |
198 | * struct system_time_snapshot - simultaneous raw/real time capture with | |
199 | * counter value | |
200 | * @cycles: Clocksource counter value to produce the system times | |
201 | * @real: Realtime system time | |
202 | * @raw: Monotonic raw system time | |
2c756feb CH |
203 | * @clock_was_set_seq: The sequence number of clock was set events |
204 | * @cs_was_changed_seq: The sequence number of clocksource change events | |
9da0f49c CH |
205 | */ |
206 | struct system_time_snapshot { | |
a5a1d1c2 | 207 | u64 cycles; |
9da0f49c CH |
208 | ktime_t real; |
209 | ktime_t raw; | |
2c756feb CH |
210 | unsigned int clock_was_set_seq; |
211 | u8 cs_was_changed_seq; | |
9da0f49c CH |
212 | }; |
213 | ||
8006c245 CH |
214 | /* |
215 | * struct system_device_crosststamp - system/device cross-timestamp | |
216 | * (syncronized capture) | |
217 | * @device: Device time | |
218 | * @sys_realtime: Realtime simultaneous with device time | |
219 | * @sys_monoraw: Monotonic raw simultaneous with device time | |
220 | */ | |
221 | struct system_device_crosststamp { | |
222 | ktime_t device; | |
223 | ktime_t sys_realtime; | |
224 | ktime_t sys_monoraw; | |
225 | }; | |
226 | ||
227 | /* | |
228 | * struct system_counterval_t - system counter value with the pointer to the | |
229 | * corresponding clocksource | |
230 | * @cycles: System counter value | |
231 | * @cs: Clocksource corresponding to system counter value. Used by | |
232 | * timekeeping code to verify comparibility of two cycle values | |
233 | */ | |
234 | struct system_counterval_t { | |
a5a1d1c2 | 235 | u64 cycles; |
8006c245 CH |
236 | struct clocksource *cs; |
237 | }; | |
238 | ||
239 | /* | |
240 | * Get cross timestamp between system clock and device clock | |
241 | */ | |
242 | extern int get_device_system_crosststamp( | |
243 | int (*get_time_fn)(ktime_t *device_time, | |
244 | struct system_counterval_t *system_counterval, | |
245 | void *ctx), | |
246 | void *ctx, | |
2c756feb | 247 | struct system_time_snapshot *history, |
8006c245 CH |
248 | struct system_device_crosststamp *xtstamp); |
249 | ||
9da0f49c CH |
250 | /* |
251 | * Simultaneously snapshot realtime and monotonic raw clocks | |
252 | */ | |
253 | extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot); | |
254 | ||
8b094cd0 TG |
255 | /* |
256 | * Persistent clock related interfaces | |
257 | */ | |
8b094cd0 TG |
258 | extern int persistent_clock_is_local; |
259 | ||
2ee96632 | 260 | extern void read_persistent_clock64(struct timespec64 *ts); |
c43c5e9f CB |
261 | void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock, |
262 | struct timespec64 *boot_offset); | |
3c00a1fe | 263 | extern int update_persistent_clock64(struct timespec64 now); |
8b094cd0 | 264 | |
edca71fe AB |
265 | /* |
266 | * deprecated aliases, don't use in new code | |
267 | */ | |
268 | #define getnstimeofday64(ts) ktime_get_real_ts64(ts) | |
fb7fcc96 AB |
269 | #define get_monotonic_boottime64(ts) ktime_get_boottime_ts64(ts) |
270 | #define getrawmonotonic64(ts) ktime_get_raw_ts64(ts) | |
271 | #define timekeeping_clocktai64(ts) ktime_get_clocktai_ts64(ts) | |
272 | ||
273 | static inline struct timespec64 current_kernel_time64(void) | |
274 | { | |
275 | struct timespec64 ts; | |
276 | ||
277 | ktime_get_coarse_real_ts64(&ts); | |
278 | ||
279 | return ts; | |
280 | } | |
281 | ||
282 | static inline struct timespec64 get_monotonic_coarse64(void) | |
283 | { | |
284 | struct timespec64 ts; | |
285 | ||
286 | ktime_get_coarse_ts64(&ts); | |
287 | ||
288 | return ts; | |
289 | } | |
8b094cd0 TG |
290 | |
291 | #endif |