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 int do_settimeofday64(const struct timespec64 *ts);
20extern int do_sys_settimeofday64(const struct timespec64 *tv,
21 const struct timezone *tz);
22/*
23 * Kernel time accessors
24 */
25struct timespec64 current_kernel_time64(void);
26
27/*
28 * timespec64 based interfaces
29 */
30struct timespec64 get_monotonic_coarse64(void);
31extern void getrawmonotonic64(struct timespec64 *ts);
32extern void ktime_get_ts64(struct timespec64 *ts);
33extern time64_t ktime_get_seconds(void);
34extern time64_t __ktime_get_real_seconds(void);
35extern time64_t ktime_get_real_seconds(void);
36
37extern int __getnstimeofday64(struct timespec64 *tv);
38extern void getnstimeofday64(struct timespec64 *tv);
39extern void getboottime64(struct timespec64 *ts);
40
41#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
42
43/*
44 * ktime_t based interfaces
45 */
46
47enum tk_offsets {
48 TK_OFFS_REAL,
49 TK_OFFS_BOOT,
50 TK_OFFS_TAI,
51 TK_OFFS_MAX,
52};
53
54extern ktime_t ktime_get(void);
55extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
56extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
57extern ktime_t ktime_get_raw(void);
58extern u32 ktime_get_resolution_ns(void);
59
60/**
61 * ktime_get_real - get the real (wall-) time in ktime_t format
62 */
63static inline ktime_t ktime_get_real(void)
64{
65 return ktime_get_with_offset(TK_OFFS_REAL);
66}
67
68/**
69 * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
70 *
71 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
72 * time spent in suspend.
73 */
74static inline ktime_t ktime_get_boottime(void)
75{
76 return ktime_get_with_offset(TK_OFFS_BOOT);
77}
78
79/**
80 * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
81 */
82static inline ktime_t ktime_get_clocktai(void)
83{
84 return ktime_get_with_offset(TK_OFFS_TAI);
85}
86
87/**
88 * ktime_mono_to_real - Convert monotonic time to clock realtime
89 */
90static inline ktime_t ktime_mono_to_real(ktime_t mono)
91{
92 return ktime_mono_to_any(mono, TK_OFFS_REAL);
93}
94
95static inline u64 ktime_get_ns(void)
96{
97 return ktime_to_ns(ktime_get());
98}
99
100static inline u64 ktime_get_real_ns(void)
101{
102 return ktime_to_ns(ktime_get_real());
103}
104
105static inline u64 ktime_get_boot_ns(void)
106{
107 return ktime_to_ns(ktime_get_boottime());
108}
109
110static inline u64 ktime_get_tai_ns(void)
111{
112 return ktime_to_ns(ktime_get_clocktai());
113}
114
115static inline u64 ktime_get_raw_ns(void)
116{
117 return ktime_to_ns(ktime_get_raw());
118}
119
120extern u64 ktime_get_mono_fast_ns(void);
121extern u64 ktime_get_raw_fast_ns(void);
122extern u64 ktime_get_boot_fast_ns(void);
123extern u64 ktime_get_real_fast_ns(void);
124
125/*
126 * timespec64 interfaces utilizing the ktime based ones
127 */
128static inline void get_monotonic_boottime64(struct timespec64 *ts)
129{
130 *ts = ktime_to_timespec64(ktime_get_boottime());
131}
132
133static inline void timekeeping_clocktai64(struct timespec64 *ts)
134{
135 *ts = ktime_to_timespec64(ktime_get_clocktai());
136}
137
138/*
139 * RTC specific
140 */
141extern bool timekeeping_rtc_skipsuspend(void);
142extern bool timekeeping_rtc_skipresume(void);
143
144extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
145
146/*
147 * struct system_time_snapshot - simultaneous raw/real time capture with
148 * counter value
149 * @cycles: Clocksource counter value to produce the system times
150 * @real: Realtime system time
151 * @raw: Monotonic raw system time
152 * @clock_was_set_seq: The sequence number of clock was set events
153 * @cs_was_changed_seq: The sequence number of clocksource change events
154 */
155struct system_time_snapshot {
156 u64 cycles;
157 ktime_t real;
158 ktime_t raw;
159 unsigned int clock_was_set_seq;
160 u8 cs_was_changed_seq;
161};
162
163/*
164 * struct system_device_crosststamp - system/device cross-timestamp
165 * (syncronized capture)
166 * @device: Device time
167 * @sys_realtime: Realtime simultaneous with device time
168 * @sys_monoraw: Monotonic raw simultaneous with device time
169 */
170struct system_device_crosststamp {
171 ktime_t device;
172 ktime_t sys_realtime;
173 ktime_t sys_monoraw;
174};
175
176/*
177 * struct system_counterval_t - system counter value with the pointer to the
178 * corresponding clocksource
179 * @cycles: System counter value
180 * @cs: Clocksource corresponding to system counter value. Used by
181 * timekeeping code to verify comparibility of two cycle values
182 */
183struct system_counterval_t {
184 u64 cycles;
185 struct clocksource *cs;
186};
187
188/*
189 * Get cross timestamp between system clock and device clock
190 */
191extern int get_device_system_crosststamp(
192 int (*get_time_fn)(ktime_t *device_time,
193 struct system_counterval_t *system_counterval,
194 void *ctx),
195 void *ctx,
196 struct system_time_snapshot *history,
197 struct system_device_crosststamp *xtstamp);
198
199/*
200 * Simultaneously snapshot realtime and monotonic raw clocks
201 */
202extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
203
204/*
205 * Persistent clock related interfaces
206 */
207extern int persistent_clock_is_local;
208
209extern void read_persistent_clock64(struct timespec64 *ts);
210extern void read_boot_clock64(struct timespec64 *ts);
211extern int update_persistent_clock64(struct timespec64 now);
212
213
214#endif