blob: 9737fbec7019bd02b9f6bf0ef9d34e1dc67fdaaa [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Thomas Gleixner8b094cd2014-07-16 21:04:02 +00002#ifndef _LINUX_TIMEKEEPING_H
3#define _LINUX_TIMEKEEPING_H
4
Christoph Hellwigb536fd52016-09-22 07:48:17 -07005#include <linux/errno.h>
Baolin Wang86d34732016-04-08 14:02:12 +08006
Thomas Gleixner8b094cd2014-07-16 21:04:02 +00007/* Included from linux/ktime.h */
8
9void timekeeping_init(void);
10extern int timekeeping_suspended;
11
Ingo Molnar93b5a9a2017-02-05 14:53:36 +010012/* Architecture timer tick functions: */
13extern void update_process_times(int user);
14extern void xtime_update(unsigned long ticks);
15
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000016/*
17 * Get and set timeofday
18 */
pang.xunlei21f7eca2014-11-18 19:15:16 +080019extern int do_settimeofday64(const struct timespec64 *ts);
Baolin Wang86d34732016-04-08 14:02:12 +080020extern int do_sys_settimeofday64(const struct timespec64 *tv,
21 const struct timezone *tz);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000022/*
23 * Kernel time accessors
24 */
Baolin Wang8758a242015-07-29 20:09:43 +080025struct timespec64 current_kernel_time64(void);
Baolin Wang8758a242015-07-29 20:09:43 +080026
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000027/*
Arnd Bergmann65469112017-10-19 13:14:49 +020028 * timespec64 based interfaces
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000029 */
John Stultz334334b2014-11-07 11:20:40 -080030struct timespec64 get_monotonic_coarse64(void);
John Stultzcdba2ec2014-11-07 11:03:20 -080031extern void getrawmonotonic64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000032extern void ktime_get_ts64(struct timespec64 *ts);
Heena Sirwani9e3680b2014-10-29 16:01:16 +053033extern time64_t ktime_get_seconds(void);
Arnd Bergmann6909e292017-10-12 16:06:11 +020034extern time64_t __ktime_get_real_seconds(void);
Heena Sirwanidbe7aa62014-10-29 16:01:50 +053035extern time64_t ktime_get_real_seconds(void);
Thomas Gleixner72199322018-03-01 17:33:32 +010036extern void ktime_get_active_ts64(struct timespec64 *ts);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000037
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000038extern int __getnstimeofday64(struct timespec64 *tv);
39extern void getnstimeofday64(struct timespec64 *tv);
John Stultzd08c0cd2014-12-08 12:00:09 -080040extern void getboottime64(struct timespec64 *ts);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +000041
Thomas Gleixnerd6c72702018-03-01 17:33:35 +010042#define ktime_get_real_ts64(ts) getnstimeofday64(ts)
43
44/* Clock BOOTTIME compatibility wrappers */
45static inline void get_monotonic_boottime64(struct timespec64 *ts)
46{
47 ktime_get_ts64(ts);
48}
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000049
50/*
51 * ktime_t based interfaces
52 */
Thomas Gleixner0077dc62014-07-16 21:04:13 +000053enum tk_offsets {
54 TK_OFFS_REAL,
Thomas Gleixner0077dc62014-07-16 21:04:13 +000055 TK_OFFS_TAI,
56 TK_OFFS_MAX,
57};
58
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000059extern ktime_t ktime_get(void);
Thomas Gleixner0077dc62014-07-16 21:04:13 +000060extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
Thomas Gleixner9a6b5192014-07-16 21:04:22 +000061extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +000062extern ktime_t ktime_get_raw(void);
Harald Geyer6374f912015-04-07 11:12:35 +000063extern u32 ktime_get_resolution_ns(void);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000064
Thomas Gleixnerd6c72702018-03-01 17:33:35 +010065/* Clock BOOTTIME compatibility wrappers */
66static inline ktime_t ktime_get_boottime(void) { return ktime_get(); }
67static inline u64 ktime_get_boot_ns(void) { return ktime_get(); }
68
Thomas Gleixnerf5264d52014-07-16 21:04:14 +000069/**
70 * ktime_get_real - get the real (wall-) time in ktime_t format
71 */
72static inline ktime_t ktime_get_real(void)
73{
74 return ktime_get_with_offset(TK_OFFS_REAL);
75}
76
Thomas Gleixnerb82c8172014-07-16 21:04:16 +000077/**
Thomas Gleixnerafab07c2014-07-16 21:04:17 +000078 * 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
Thomas Gleixner9a6b5192014-07-16 21:04:22 +000085/**
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
Thomas Gleixner897994e2014-07-16 21:04:29 +000093static 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
Peter Zijlstrafe5fba02015-03-17 12:39:10 +0100103static inline u64 ktime_get_tai_ns(void)
104{
105 return ktime_to_ns(ktime_get_clocktai());
106}
107
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000108static inline u64 ktime_get_raw_ns(void)
109{
110 return ktime_to_ns(ktime_get_raw());
111}
112
Thomas Gleixner4396e052014-07-16 21:05:23 +0000113extern u64 ktime_get_mono_fast_ns(void);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100114extern u64 ktime_get_raw_fast_ns(void);
Thomas Gleixner4c3711d2017-08-31 17:12:48 +0200115extern u64 ktime_get_real_fast_ns(void);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000116
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000117/*
Arnd Bergmann65469112017-10-19 13:14:49 +0200118 * timespec64 interfaces utilizing the ktime based ones
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000119 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700120static inline void timekeeping_clocktai64(struct timespec64 *ts)
121{
122 *ts = ktime_to_timespec64(ktime_get_clocktai());
123}
124
Thomas Gleixner48f18fd2014-07-16 21:04:57 +0000125/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000126 * RTC specific
127 */
Xunlei Pang0fa88cb2015-04-01 20:34:38 -0700128extern bool timekeeping_rtc_skipsuspend(void);
129extern bool timekeeping_rtc_skipresume(void);
130
pang.xunlei04d90892014-11-18 19:15:17 +0800131extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
132
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000133/*
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800134 * struct system_time_snapshot - simultaneous raw/real time capture with
135 * counter value
136 * @cycles: Clocksource counter value to produce the system times
137 * @real: Realtime system time
138 * @raw: Monotonic raw system time
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800139 * @clock_was_set_seq: The sequence number of clock was set events
140 * @cs_was_changed_seq: The sequence number of clocksource change events
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800141 */
142struct system_time_snapshot {
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100143 u64 cycles;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800144 ktime_t real;
145 ktime_t raw;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800146 unsigned int clock_was_set_seq;
147 u8 cs_was_changed_seq;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800148};
149
150/*
Christopher S. Hall8006c242016-02-22 03:15:22 -0800151 * struct system_device_crosststamp - system/device cross-timestamp
152 * (syncronized capture)
153 * @device: Device time
154 * @sys_realtime: Realtime simultaneous with device time
155 * @sys_monoraw: Monotonic raw simultaneous with device time
156 */
157struct system_device_crosststamp {
158 ktime_t device;
159 ktime_t sys_realtime;
160 ktime_t sys_monoraw;
161};
162
163/*
164 * struct system_counterval_t - system counter value with the pointer to the
165 * corresponding clocksource
166 * @cycles: System counter value
167 * @cs: Clocksource corresponding to system counter value. Used by
168 * timekeeping code to verify comparibility of two cycle values
169 */
170struct system_counterval_t {
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100171 u64 cycles;
Christopher S. Hall8006c242016-02-22 03:15:22 -0800172 struct clocksource *cs;
173};
174
175/*
176 * Get cross timestamp between system clock and device clock
177 */
178extern int get_device_system_crosststamp(
179 int (*get_time_fn)(ktime_t *device_time,
180 struct system_counterval_t *system_counterval,
181 void *ctx),
182 void *ctx,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800183 struct system_time_snapshot *history,
Christopher S. Hall8006c242016-02-22 03:15:22 -0800184 struct system_device_crosststamp *xtstamp);
185
186/*
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800187 * Simultaneously snapshot realtime and monotonic raw clocks
188 */
189extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
190
191/*
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000192 * Persistent clock related interfaces
193 */
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000194extern int persistent_clock_is_local;
195
Xunlei Pang2ee96632015-04-01 20:34:22 -0700196extern void read_persistent_clock64(struct timespec64 *ts);
Xunlei Pang9a806dd2015-04-01 20:34:21 -0700197extern void read_boot_clock64(struct timespec64 *ts);
Xunlei Pang3c00a1f2015-04-01 20:34:23 -0700198extern int update_persistent_clock64(struct timespec64 now);
Thomas Gleixner8b094cd2014-07-16 21:04:02 +0000199
200
201#endif