ktime.h revision 329260
1/*-
2 * Copyright (c) 2014-2015 Mellanox Technologies, Ltd.
3 * Copyright (c) 2015 Fran��ois Tigeot
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/ktime.h 329260 2018-02-14 09:53:23Z hselasky $
28 */
29
30#ifndef _LINUX_KTIME_H
31#define	_LINUX_KTIME_H
32
33#include <linux/types.h>
34#include <linux/time.h>
35#include <linux/jiffies.h>
36
37#define ktime_get_ts(x) getnanouptime(x)
38
39/* time values in nanoseconds */
40union ktime {
41	int64_t tv64;
42};
43
44typedef union ktime ktime_t;
45
46#define KTIME_MAX                       ((s64)~((u64)1 << 63))
47#define KTIME_SEC_MAX                   (KTIME_MAX / NSEC_PER_SEC)
48
49static inline int64_t
50ktime_to_ns(ktime_t kt)
51{
52	return kt.tv64;
53}
54
55static inline ktime_t
56ns_to_ktime(uint64_t nsec)
57{
58	ktime_t kt;
59
60	kt.tv64 = nsec;
61	return (kt);
62}
63
64static inline int64_t
65ktime_divns(const ktime_t kt, int64_t div)
66{
67	return kt.tv64 / div;
68}
69
70static inline int64_t
71ktime_to_us(ktime_t kt)
72{
73        return ktime_divns(kt, NSEC_PER_USEC);
74}
75
76static inline int64_t
77ktime_to_ms(ktime_t kt)
78{
79        return ktime_divns(kt, NSEC_PER_MSEC);
80}
81
82static inline struct timeval
83ktime_to_timeval(ktime_t kt)
84{
85	return ns_to_timeval(kt.tv64);
86}
87
88static inline ktime_t
89ktime_add_ns(ktime_t kt, int64_t ns)
90{
91	kt.tv64 += ns;
92	return kt;
93}
94
95static inline ktime_t
96ktime_sub_ns(ktime_t kt, int64_t ns)
97{
98	kt.tv64 -= ns;
99	return kt;
100}
101
102static inline ktime_t
103ktime_set(const long secs, const unsigned long nsecs)
104{
105	ktime_t retval = { (s64)secs * NSEC_PER_SEC + (s64)nsecs };
106	return (retval);
107}
108
109static inline ktime_t
110ktime_sub(ktime_t lhs, ktime_t rhs)
111{
112	lhs.tv64 -= rhs.tv64;
113	return (lhs);
114}
115
116static inline int64_t
117ktime_us_delta(ktime_t later, ktime_t earlier)
118{
119        ktime_t diff = ktime_sub(later, earlier);
120        return ktime_to_us(diff);
121}
122
123static inline int64_t
124ktime_ms_delta(ktime_t later, ktime_t earlier)
125{
126        ktime_t diff = ktime_sub(later, earlier);
127        return ktime_to_ms(diff);
128}
129
130static inline ktime_t
131ktime_add(ktime_t lhs, ktime_t rhs)
132{
133	lhs.tv64 += rhs.tv64;
134	return (lhs);
135}
136
137static inline ktime_t
138timespec_to_ktime(struct timespec ts)
139{
140	return (ktime_set(ts.tv_sec, ts.tv_nsec));
141}
142
143static inline ktime_t
144timeval_to_ktime(struct timeval tv)
145{
146	return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC));
147}
148
149#define ktime_to_timespec(kt)		ns_to_timespec((kt).tv64)
150#define ktime_to_timeval(kt)		ns_to_timeval((kt).tv64)
151#define ktime_to_ns(kt)			((kt).tv64)
152
153static inline int64_t
154ktime_get_ns(void)
155{
156	struct timespec ts;
157	ktime_t kt;
158
159	ktime_get_ts(&ts);
160	kt = timespec_to_ktime(ts);
161	return (ktime_to_ns(kt));
162}
163
164#define	ktime_get_raw_ns()	ktime_get_ns()
165
166static inline ktime_t
167ktime_get(void)
168{
169	struct timespec ts;
170
171	ktime_get_ts(&ts);
172	return (timespec_to_ktime(ts));
173}
174
175static inline ktime_t
176ktime_get_boottime(void)
177{
178	struct timespec ts;
179
180	nanouptime(&ts);
181	return (timespec_to_ktime(ts));
182}
183
184static inline ktime_t
185ktime_get_real(void)
186{
187	struct timespec ts;
188
189	nanotime(&ts);
190	return (timespec_to_ktime(ts));
191}
192
193#endif /* _LINUX_KTIME_H */
194