xref: /openbmc/linux/include/linux/time64.h (revision 30f3b3f9836c7c7e1f42ca855bbe8127fff4b99a)
1361a3bf0SJohn Stultz #ifndef _LINUX_TIME64_H
2361a3bf0SJohn Stultz #define _LINUX_TIME64_H
3361a3bf0SJohn Stultz 
4361a3bf0SJohn Stultz #include <uapi/linux/time.h>
5*30f3b3f9SXunlei Pang #include <linux/math64.h>
6361a3bf0SJohn Stultz 
7361a3bf0SJohn Stultz typedef __s64 time64_t;
8361a3bf0SJohn Stultz 
9361a3bf0SJohn Stultz /*
10361a3bf0SJohn Stultz  * This wants to go into uapi/linux/time.h once we agreed about the
11361a3bf0SJohn Stultz  * userspace interfaces.
12361a3bf0SJohn Stultz  */
13361a3bf0SJohn Stultz #if __BITS_PER_LONG == 64
14361a3bf0SJohn Stultz # define timespec64 timespec
15361a3bf0SJohn Stultz #else
16361a3bf0SJohn Stultz struct timespec64 {
17361a3bf0SJohn Stultz 	time64_t	tv_sec;			/* seconds */
18361a3bf0SJohn Stultz 	long		tv_nsec;		/* nanoseconds */
19361a3bf0SJohn Stultz };
20361a3bf0SJohn Stultz #endif
21361a3bf0SJohn Stultz 
22361a3bf0SJohn Stultz /* Parameters used to convert the timespec values: */
23361a3bf0SJohn Stultz #define MSEC_PER_SEC	1000L
24361a3bf0SJohn Stultz #define USEC_PER_MSEC	1000L
25361a3bf0SJohn Stultz #define NSEC_PER_USEC	1000L
26361a3bf0SJohn Stultz #define NSEC_PER_MSEC	1000000L
27361a3bf0SJohn Stultz #define USEC_PER_SEC	1000000L
28361a3bf0SJohn Stultz #define NSEC_PER_SEC	1000000000L
29361a3bf0SJohn Stultz #define FSEC_PER_SEC	1000000000000000LL
30361a3bf0SJohn Stultz 
31361a3bf0SJohn Stultz /* Located here for timespec[64]_valid_strict */
32361a3bf0SJohn Stultz #define KTIME_MAX			((s64)~((u64)1 << 63))
33361a3bf0SJohn Stultz #define KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
34361a3bf0SJohn Stultz 
35361a3bf0SJohn Stultz #if __BITS_PER_LONG == 64
36361a3bf0SJohn Stultz 
3749cd6f86SJohn Stultz static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
3849cd6f86SJohn Stultz {
3949cd6f86SJohn Stultz 	return ts64;
4049cd6f86SJohn Stultz }
4149cd6f86SJohn Stultz 
4249cd6f86SJohn Stultz static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
4349cd6f86SJohn Stultz {
4449cd6f86SJohn Stultz 	return ts;
4549cd6f86SJohn Stultz }
4649cd6f86SJohn Stultz 
47361a3bf0SJohn Stultz # define timespec64_equal		timespec_equal
48361a3bf0SJohn Stultz # define timespec64_compare		timespec_compare
49361a3bf0SJohn Stultz # define set_normalized_timespec64	set_normalized_timespec
50361a3bf0SJohn Stultz # define timespec64_add_safe		timespec_add_safe
51361a3bf0SJohn Stultz # define timespec64_add			timespec_add
52361a3bf0SJohn Stultz # define timespec64_sub			timespec_sub
53361a3bf0SJohn Stultz # define timespec64_valid		timespec_valid
54361a3bf0SJohn Stultz # define timespec64_valid_strict	timespec_valid_strict
55361a3bf0SJohn Stultz # define timespec64_to_ns		timespec_to_ns
56361a3bf0SJohn Stultz # define ns_to_timespec64		ns_to_timespec
57361a3bf0SJohn Stultz # define timespec64_add_ns		timespec_add_ns
58361a3bf0SJohn Stultz 
59361a3bf0SJohn Stultz #else
60361a3bf0SJohn Stultz 
6149cd6f86SJohn Stultz static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
6249cd6f86SJohn Stultz {
6349cd6f86SJohn Stultz 	struct timespec ret;
6449cd6f86SJohn Stultz 
6549cd6f86SJohn Stultz 	ret.tv_sec = (time_t)ts64.tv_sec;
6649cd6f86SJohn Stultz 	ret.tv_nsec = ts64.tv_nsec;
6749cd6f86SJohn Stultz 	return ret;
6849cd6f86SJohn Stultz }
6949cd6f86SJohn Stultz 
7049cd6f86SJohn Stultz static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
7149cd6f86SJohn Stultz {
7249cd6f86SJohn Stultz 	struct timespec64 ret;
7349cd6f86SJohn Stultz 
7449cd6f86SJohn Stultz 	ret.tv_sec = ts.tv_sec;
7549cd6f86SJohn Stultz 	ret.tv_nsec = ts.tv_nsec;
7649cd6f86SJohn Stultz 	return ret;
7749cd6f86SJohn Stultz }
7849cd6f86SJohn Stultz 
79361a3bf0SJohn Stultz static inline int timespec64_equal(const struct timespec64 *a,
80361a3bf0SJohn Stultz 				   const struct timespec64 *b)
81361a3bf0SJohn Stultz {
82361a3bf0SJohn Stultz 	return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
83361a3bf0SJohn Stultz }
84361a3bf0SJohn Stultz 
85361a3bf0SJohn Stultz /*
86361a3bf0SJohn Stultz  * lhs < rhs:  return <0
87361a3bf0SJohn Stultz  * lhs == rhs: return 0
88361a3bf0SJohn Stultz  * lhs > rhs:  return >0
89361a3bf0SJohn Stultz  */
90361a3bf0SJohn Stultz static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
91361a3bf0SJohn Stultz {
92361a3bf0SJohn Stultz 	if (lhs->tv_sec < rhs->tv_sec)
93361a3bf0SJohn Stultz 		return -1;
94361a3bf0SJohn Stultz 	if (lhs->tv_sec > rhs->tv_sec)
95361a3bf0SJohn Stultz 		return 1;
96361a3bf0SJohn Stultz 	return lhs->tv_nsec - rhs->tv_nsec;
97361a3bf0SJohn Stultz }
98361a3bf0SJohn Stultz 
99361a3bf0SJohn Stultz extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
100361a3bf0SJohn Stultz 
101361a3bf0SJohn Stultz /*
102361a3bf0SJohn Stultz  * timespec64_add_safe assumes both values are positive and checks for
103361a3bf0SJohn Stultz  * overflow. It will return TIME_T_MAX if the returned value would be
104361a3bf0SJohn Stultz  * smaller then either of the arguments.
105361a3bf0SJohn Stultz  */
106361a3bf0SJohn Stultz extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
107361a3bf0SJohn Stultz 					 const struct timespec64 rhs);
108361a3bf0SJohn Stultz 
109361a3bf0SJohn Stultz 
110361a3bf0SJohn Stultz static inline struct timespec64 timespec64_add(struct timespec64 lhs,
111361a3bf0SJohn Stultz 						struct timespec64 rhs)
112361a3bf0SJohn Stultz {
113361a3bf0SJohn Stultz 	struct timespec64 ts_delta;
114361a3bf0SJohn Stultz 	set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
115361a3bf0SJohn Stultz 				lhs.tv_nsec + rhs.tv_nsec);
116361a3bf0SJohn Stultz 	return ts_delta;
117361a3bf0SJohn Stultz }
118361a3bf0SJohn Stultz 
119361a3bf0SJohn Stultz /*
120361a3bf0SJohn Stultz  * sub = lhs - rhs, in normalized form
121361a3bf0SJohn Stultz  */
122361a3bf0SJohn Stultz static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
123361a3bf0SJohn Stultz 						struct timespec64 rhs)
124361a3bf0SJohn Stultz {
125361a3bf0SJohn Stultz 	struct timespec64 ts_delta;
126361a3bf0SJohn Stultz 	set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
127361a3bf0SJohn Stultz 				lhs.tv_nsec - rhs.tv_nsec);
128361a3bf0SJohn Stultz 	return ts_delta;
129361a3bf0SJohn Stultz }
130361a3bf0SJohn Stultz 
131361a3bf0SJohn Stultz /*
132361a3bf0SJohn Stultz  * Returns true if the timespec64 is norm, false if denorm:
133361a3bf0SJohn Stultz  */
134361a3bf0SJohn Stultz static inline bool timespec64_valid(const struct timespec64 *ts)
135361a3bf0SJohn Stultz {
136361a3bf0SJohn Stultz 	/* Dates before 1970 are bogus */
137361a3bf0SJohn Stultz 	if (ts->tv_sec < 0)
138361a3bf0SJohn Stultz 		return false;
139361a3bf0SJohn Stultz 	/* Can't have more nanoseconds then a second */
140361a3bf0SJohn Stultz 	if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
141361a3bf0SJohn Stultz 		return false;
142361a3bf0SJohn Stultz 	return true;
143361a3bf0SJohn Stultz }
144361a3bf0SJohn Stultz 
145361a3bf0SJohn Stultz static inline bool timespec64_valid_strict(const struct timespec64 *ts)
146361a3bf0SJohn Stultz {
147361a3bf0SJohn Stultz 	if (!timespec64_valid(ts))
148361a3bf0SJohn Stultz 		return false;
149361a3bf0SJohn Stultz 	/* Disallow values that could overflow ktime_t */
150361a3bf0SJohn Stultz 	if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
151361a3bf0SJohn Stultz 		return false;
152361a3bf0SJohn Stultz 	return true;
153361a3bf0SJohn Stultz }
154361a3bf0SJohn Stultz 
155361a3bf0SJohn Stultz /**
156361a3bf0SJohn Stultz  * timespec64_to_ns - Convert timespec64 to nanoseconds
157361a3bf0SJohn Stultz  * @ts:		pointer to the timespec64 variable to be converted
158361a3bf0SJohn Stultz  *
159361a3bf0SJohn Stultz  * Returns the scalar nanosecond representation of the timespec64
160361a3bf0SJohn Stultz  * parameter.
161361a3bf0SJohn Stultz  */
162361a3bf0SJohn Stultz static inline s64 timespec64_to_ns(const struct timespec64 *ts)
163361a3bf0SJohn Stultz {
164361a3bf0SJohn Stultz 	return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
165361a3bf0SJohn Stultz }
166361a3bf0SJohn Stultz 
167361a3bf0SJohn Stultz /**
168361a3bf0SJohn Stultz  * ns_to_timespec64 - Convert nanoseconds to timespec64
169361a3bf0SJohn Stultz  * @nsec:	the nanoseconds value to be converted
170361a3bf0SJohn Stultz  *
171361a3bf0SJohn Stultz  * Returns the timespec64 representation of the nsec parameter.
172361a3bf0SJohn Stultz  */
173361a3bf0SJohn Stultz extern struct timespec64 ns_to_timespec64(const s64 nsec);
174361a3bf0SJohn Stultz 
175361a3bf0SJohn Stultz /**
176361a3bf0SJohn Stultz  * timespec64_add_ns - Adds nanoseconds to a timespec64
177361a3bf0SJohn Stultz  * @a:		pointer to timespec64 to be incremented
178361a3bf0SJohn Stultz  * @ns:		unsigned nanoseconds value to be added
179361a3bf0SJohn Stultz  *
180361a3bf0SJohn Stultz  * This must always be inlined because its used from the x86-64 vdso,
181361a3bf0SJohn Stultz  * which cannot call other kernel functions.
182361a3bf0SJohn Stultz  */
183361a3bf0SJohn Stultz static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
184361a3bf0SJohn Stultz {
185361a3bf0SJohn Stultz 	a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
186361a3bf0SJohn Stultz 	a->tv_nsec = ns;
187361a3bf0SJohn Stultz }
188361a3bf0SJohn Stultz 
189361a3bf0SJohn Stultz #endif
190361a3bf0SJohn Stultz 
191361a3bf0SJohn Stultz #endif /* _LINUX_TIME64_H */
192