timekeeping: Standardize on ktime_get_*() naming
[linux-2.6-block.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 */
21f7eca5 19extern int do_settimeofday64(const struct timespec64 *ts);
86d34732
BW
20extern int do_sys_settimeofday64(const struct timespec64 *tv,
21 const struct timezone *tz);
8758a240 22
8b094cd0 23/*
6546911e 24 * timespec64 based interfaces
8b094cd0 25 */
fb7fcc96 26extern void ktime_get_raw_ts64(struct timespec64 *ts);
d6d29896 27extern void ktime_get_ts64(struct timespec64 *ts);
edca71fe 28extern void ktime_get_real_ts64(struct timespec64 *tv);
fb7fcc96
AB
29extern void ktime_get_coarse_ts64(struct timespec64 *ts);
30extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
31
32void getboottime64(struct timespec64 *ts);
33
34/*
35 * time64_t base interfaces
36 */
9e3680b1 37extern time64_t ktime_get_seconds(void);
6909e29f 38extern time64_t __ktime_get_real_seconds(void);
dbe7aa62 39extern time64_t ktime_get_real_seconds(void);
d6d29896 40
8b094cd0
TG
41/*
42 * ktime_t based interfaces
43 */
a3ed0e43 44
0077dc60
TG
45enum tk_offsets {
46 TK_OFFS_REAL,
a3ed0e43 47 TK_OFFS_BOOT,
0077dc60
TG
48 TK_OFFS_TAI,
49 TK_OFFS_MAX,
50};
51
8b094cd0 52extern ktime_t ktime_get(void);
0077dc60 53extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
9a6b5197 54extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
f519b1a2 55extern ktime_t ktime_get_raw(void);
6374f912 56extern u32 ktime_get_resolution_ns(void);
8b094cd0 57
f5264d5d
TG
58/**
59 * ktime_get_real - get the real (wall-) time in ktime_t format
60 */
61static inline ktime_t ktime_get_real(void)
62{
63 return ktime_get_with_offset(TK_OFFS_REAL);
64}
65
a3ed0e43
TG
66/**
67 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
68 *
69 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
70 * time spent in suspend.
71 */
72static inline ktime_t ktime_get_boottime(void)
73{
74 return ktime_get_with_offset(TK_OFFS_BOOT);
75}
76
afab07c0
TG
77/**
78 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
79 */
80static inline ktime_t ktime_get_clocktai(void)
81{
82 return ktime_get_with_offset(TK_OFFS_TAI);
83}
84
9a6b5197
TG
85/**
86 * ktime_mono_to_real - Convert monotonic time to clock realtime
87 */
88static inline ktime_t ktime_mono_to_real(ktime_t mono)
89{
90 return ktime_mono_to_any(mono, TK_OFFS_REAL);
91}
92
897994e3
TG
93static inline u64 ktime_get_ns(void)
94{
95 return ktime_to_ns(ktime_get());
96}
97
98static inline u64 ktime_get_real_ns(void)
99{
100 return ktime_to_ns(ktime_get_real());
101}
102
a3ed0e43
TG
103static inline u64 ktime_get_boot_ns(void)
104{
105 return ktime_to_ns(ktime_get_boottime());
106}
107
fe5fba05
PZ
108static inline u64 ktime_get_tai_ns(void)
109{
110 return ktime_to_ns(ktime_get_clocktai());
111}
112
f519b1a2
TG
113static inline u64 ktime_get_raw_ns(void)
114{
115 return ktime_to_ns(ktime_get_raw());
116}
117
4396e058 118extern u64 ktime_get_mono_fast_ns(void);
f09cb9a1 119extern u64 ktime_get_raw_fast_ns(void);
a3ed0e43 120extern u64 ktime_get_boot_fast_ns(void);
4c3711d7 121extern u64 ktime_get_real_fast_ns(void);
4396e058 122
d6c7270e
TG
123/*
124 * timespec64 interfaces utilizing the ktime based ones
125 */
fb7fcc96 126static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
a3ed0e43
TG
127{
128 *ts = ktime_to_timespec64(ktime_get_boottime());
129}
130
fb7fcc96 131static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
3c9c12f4
DD
132{
133 *ts = ktime_to_timespec64(ktime_get_clocktai());
134}
135
8b094cd0
TG
136/*
137 * RTC specific
138 */
0fa88cb4
XP
139extern bool timekeeping_rtc_skipsuspend(void);
140extern bool timekeeping_rtc_skipresume(void);
141
04d90890 142extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
143
9da0f49c
CH
144/*
145 * struct system_time_snapshot - simultaneous raw/real time capture with
146 * counter value
147 * @cycles: Clocksource counter value to produce the system times
148 * @real: Realtime system time
149 * @raw: Monotonic raw system time
2c756feb
CH
150 * @clock_was_set_seq: The sequence number of clock was set events
151 * @cs_was_changed_seq: The sequence number of clocksource change events
9da0f49c
CH
152 */
153struct system_time_snapshot {
a5a1d1c2 154 u64 cycles;
9da0f49c
CH
155 ktime_t real;
156 ktime_t raw;
2c756feb
CH
157 unsigned int clock_was_set_seq;
158 u8 cs_was_changed_seq;
9da0f49c
CH
159};
160
8006c245
CH
161/*
162 * struct system_device_crosststamp - system/device cross-timestamp
163 * (syncronized capture)
164 * @device: Device time
165 * @sys_realtime: Realtime simultaneous with device time
166 * @sys_monoraw: Monotonic raw simultaneous with device time
167 */
168struct system_device_crosststamp {
169 ktime_t device;
170 ktime_t sys_realtime;
171 ktime_t sys_monoraw;
172};
173
174/*
175 * struct system_counterval_t - system counter value with the pointer to the
176 * corresponding clocksource
177 * @cycles: System counter value
178 * @cs: Clocksource corresponding to system counter value. Used by
179 * timekeeping code to verify comparibility of two cycle values
180 */
181struct system_counterval_t {
a5a1d1c2 182 u64 cycles;
8006c245
CH
183 struct clocksource *cs;
184};
185
186/*
187 * Get cross timestamp between system clock and device clock
188 */
189extern int get_device_system_crosststamp(
190 int (*get_time_fn)(ktime_t *device_time,
191 struct system_counterval_t *system_counterval,
192 void *ctx),
193 void *ctx,
2c756feb 194 struct system_time_snapshot *history,
8006c245
CH
195 struct system_device_crosststamp *xtstamp);
196
9da0f49c
CH
197/*
198 * Simultaneously snapshot realtime and monotonic raw clocks
199 */
200extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
201
8b094cd0
TG
202/*
203 * Persistent clock related interfaces
204 */
8b094cd0
TG
205extern int persistent_clock_is_local;
206
2ee96632 207extern void read_persistent_clock64(struct timespec64 *ts);
9a806ddb 208extern void read_boot_clock64(struct timespec64 *ts);
3c00a1fe 209extern int update_persistent_clock64(struct timespec64 now);
8b094cd0 210
edca71fe
AB
211/*
212 * deprecated aliases, don't use in new code
213 */
214#define getnstimeofday64(ts) ktime_get_real_ts64(ts)
fb7fcc96
AB
215#define get_monotonic_boottime64(ts) ktime_get_boottime_ts64(ts)
216#define getrawmonotonic64(ts) ktime_get_raw_ts64(ts)
217#define timekeeping_clocktai64(ts) ktime_get_clocktai_ts64(ts)
218
219static inline struct timespec64 current_kernel_time64(void)
220{
221 struct timespec64 ts;
222
223 ktime_get_coarse_real_ts64(&ts);
224
225 return ts;
226}
227
228static inline struct timespec64 get_monotonic_coarse64(void)
229{
230 struct timespec64 ts;
231
232 ktime_get_coarse_ts64(&ts);
233
234 return ts;
235}
8b094cd0
TG
236
237#endif