1270710Shselasky/*-
2329977Shselasky * Copyright (c) 2018 Limelight Networks, Inc.
3329977Shselasky * Copyright (c) 2014-2018 Mellanox Technologies, Ltd.
4289572Shselasky * Copyright (c) 2015 Fran��ois Tigeot
5270710Shselasky * All rights reserved.
6270710Shselasky *
7270710Shselasky * Redistribution and use in source and binary forms, with or without
8270710Shselasky * modification, are permitted provided that the following conditions
9270710Shselasky * are met:
10270710Shselasky * 1. Redistributions of source code must retain the above copyright
11270710Shselasky *    notice unmodified, this list of conditions, and the following
12270710Shselasky *    disclaimer.
13270710Shselasky * 2. Redistributions in binary form must reproduce the above copyright
14270710Shselasky *    notice, this list of conditions and the following disclaimer in the
15270710Shselasky *    documentation and/or other materials provided with the distribution.
16270710Shselasky *
17270710Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18270710Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19270710Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20270710Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21270710Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22270710Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23270710Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24270710Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25270710Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26270710Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27289644Shselasky *
28289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/ktime.h 340942 2018-11-26 11:07:43Z hselasky $
29270710Shselasky */
30328653Shselasky
31270710Shselasky#ifndef _LINUX_KTIME_H
32328653Shselasky#define	_LINUX_KTIME_H
33270710Shselasky
34270710Shselasky#include <linux/types.h>
35289572Shselasky#include <linux/time.h>
36270710Shselasky#include <linux/jiffies.h>
37270710Shselasky
38329977Shselasky#define	ktime_get_ts(x) getnanouptime(x)
39270710Shselasky
40289572Shselasky/* time values in nanoseconds */
41329977Shselaskytypedef s64 ktime_t;
42270710Shselasky
43331756Semaste#define	KTIME_MAX			((s64)~((u64)1 << 63))
44331756Semaste#define	KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
45270710Shselasky
46289572Shselaskystatic inline int64_t
47289572Shselaskyktime_to_ns(ktime_t kt)
48270710Shselasky{
49329977Shselasky	return (kt);
50270710Shselasky}
51270710Shselasky
52328653Shselaskystatic inline ktime_t
53328653Shselaskyns_to_ktime(uint64_t nsec)
54328653Shselasky{
55329977Shselasky	return (nsec);
56328653Shselasky}
57328653Shselasky
58300498Shselaskystatic inline int64_t
59300498Shselaskyktime_divns(const ktime_t kt, int64_t div)
60300498Shselasky{
61329977Shselasky	return (kt / div);
62300498Shselasky}
63300498Shselasky
64300498Shselaskystatic inline int64_t
65300498Shselaskyktime_to_us(ktime_t kt)
66300498Shselasky{
67329977Shselasky	return (ktime_divns(kt, NSEC_PER_USEC));
68300498Shselasky}
69300498Shselasky
70300498Shselaskystatic inline int64_t
71300498Shselaskyktime_to_ms(ktime_t kt)
72300498Shselasky{
73329977Shselasky	return (ktime_divns(kt, NSEC_PER_MSEC));
74300498Shselasky}
75300498Shselasky
76289572Shselaskystatic inline struct timeval
77289572Shselaskyktime_to_timeval(ktime_t kt)
78270710Shselasky{
79329977Shselasky	return (ns_to_timeval(kt));
80270710Shselasky}
81270710Shselasky
82289572Shselaskystatic inline ktime_t
83289572Shselaskyktime_add_ns(ktime_t kt, int64_t ns)
84270710Shselasky{
85329977Shselasky	return (kt + ns);
86270710Shselasky}
87270710Shselasky
88289572Shselaskystatic inline ktime_t
89337894Shselaskyktime_add_ms(ktime_t kt, int64_t ms)
90337894Shselasky{
91337894Shselasky
92337894Shselasky	return (ktime_add_ns(kt, ms * NSEC_PER_MSEC));
93337894Shselasky}
94337894Shselasky
95337894Shselaskystatic inline ktime_t
96289572Shselaskyktime_sub_ns(ktime_t kt, int64_t ns)
97270710Shselasky{
98329977Shselasky	return (kt - ns);
99270710Shselasky}
100270710Shselasky
101289572Shselaskystatic inline ktime_t
102289572Shselaskyktime_set(const long secs, const unsigned long nsecs)
103270710Shselasky{
104329977Shselasky	ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs};
105329977Shselasky
106289572Shselasky	return (retval);
107270710Shselasky}
108270710Shselasky
109289572Shselaskystatic inline ktime_t
110289572Shselaskyktime_sub(ktime_t lhs, ktime_t rhs)
111270710Shselasky{
112329977Shselasky	return (lhs - rhs);
113270710Shselasky}
114270710Shselasky
115300498Shselaskystatic inline int64_t
116300498Shselaskyktime_us_delta(ktime_t later, ktime_t earlier)
117300498Shselasky{
118329977Shselasky	ktime_t diff = ktime_sub(later, earlier);
119329977Shselasky
120329977Shselasky	return (ktime_to_us(diff));
121300498Shselasky}
122300498Shselasky
123300498Shselaskystatic inline int64_t
124300498Shselaskyktime_ms_delta(ktime_t later, ktime_t earlier)
125300498Shselasky{
126329977Shselasky	ktime_t diff = ktime_sub(later, earlier);
127329977Shselasky
128329977Shselasky	return (ktime_to_ms(diff));
129300498Shselasky}
130300498Shselasky
131289572Shselaskystatic inline ktime_t
132289572Shselaskyktime_add(ktime_t lhs, ktime_t rhs)
133270710Shselasky{
134329977Shselasky	return (lhs + rhs);
135270710Shselasky}
136270710Shselasky
137335421Shselaskystatic inline int
138335421Shselaskyktime_compare(const ktime_t cmp1, const ktime_t cmp2)
139335421Shselasky{
140335421Shselasky
141335421Shselasky	if (cmp1 > cmp2)
142335421Shselasky		return (1);
143335421Shselasky	else if (cmp1 < cmp2)
144335421Shselasky		return (-1);
145335421Shselasky	else
146335421Shselasky		return (0);
147335421Shselasky}
148335421Shselasky
149335421Shselaskystatic inline bool
150335421Shselaskyktime_after(const ktime_t cmp1, const ktime_t cmp2)
151335421Shselasky{
152335421Shselasky
153335421Shselasky	return (ktime_compare(cmp1, cmp2) > 0);
154335421Shselasky}
155335421Shselasky
156337894Shselaskystatic inline bool
157337894Shselaskyktime_before(const ktime_t cmp1, const ktime_t cmp2)
158337894Shselasky{
159337894Shselasky
160337894Shselasky	return (ktime_compare(cmp1, cmp2) < 0);
161337894Shselasky}
162337894Shselasky
163289572Shselaskystatic inline ktime_t
164289572Shselaskytimespec_to_ktime(struct timespec ts)
165270710Shselasky{
166289572Shselasky	return (ktime_set(ts.tv_sec, ts.tv_nsec));
167270710Shselasky}
168270710Shselasky
169289572Shselaskystatic inline ktime_t
170289572Shselaskytimeval_to_ktime(struct timeval tv)
171270710Shselasky{
172289572Shselasky	return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC));
173270710Shselasky}
174270710Shselasky
175329977Shselasky#define	ktime_to_timespec(kt)		ns_to_timespec(kt)
176335429Shselasky#define	ktime_to_timespec64(kt)		ns_to_timespec(kt)
177329977Shselasky#define	ktime_to_timeval(kt)		ns_to_timeval(kt)
178329977Shselasky#define	ktime_to_ns(kt)			(kt)
179340942Shselasky#define	ktime_get_ts64(ts)		ktime_get_ts(ts)
180270710Shselasky
181328653Shselaskystatic inline int64_t
182289572Shselaskyktime_get_ns(void)
183277396Shselasky{
184277396Shselasky	struct timespec ts;
185289572Shselasky
186277396Shselasky	ktime_get_ts(&ts);
187329977Shselasky
188329977Shselasky	return (ktime_to_ns(timespec_to_ktime(ts)));
189277396Shselasky}
190277396Shselasky
191289572Shselaskystatic inline ktime_t
192289572Shselaskyktime_get(void)
193289572Shselasky{
194289572Shselasky	struct timespec ts;
195289572Shselasky
196289572Shselasky	ktime_get_ts(&ts);
197289572Shselasky	return (timespec_to_ktime(ts));
198289572Shselasky}
199289572Shselasky
200328653Shselaskystatic inline ktime_t
201328653Shselaskyktime_get_boottime(void)
202328653Shselasky{
203328653Shselasky	struct timespec ts;
204328653Shselasky
205328653Shselasky	nanouptime(&ts);
206328653Shselasky	return (timespec_to_ktime(ts));
207328653Shselasky}
208328653Shselasky
209328653Shselaskystatic inline ktime_t
210328653Shselaskyktime_get_real(void)
211328653Shselasky{
212328653Shselasky	struct timespec ts;
213328653Shselasky
214328653Shselasky	nanotime(&ts);
215328653Shselasky	return (timespec_to_ktime(ts));
216328653Shselasky}
217328653Shselasky
218329977Shselaskystatic inline ktime_t
219329977Shselaskyktime_get_real_seconds(void)
220329977Shselasky{
221329977Shselasky	struct timespec ts;
222329977Shselasky
223329977Shselasky	nanotime(&ts);
224329977Shselasky	return (ts.tv_sec);
225329977Shselasky}
226329977Shselasky
227330848Shselaskystatic inline ktime_t
228330848Shselaskyktime_get_raw(void)
229330848Shselasky{
230330848Shselasky	struct timespec ts;
231330848Shselasky
232330848Shselasky	nanotime(&ts);
233330848Shselasky	return (timespec_to_ktime(ts));
234330848Shselasky}
235330848Shselasky
236329977Shselaskystatic inline u64
237329977Shselaskyktime_get_raw_ns(void)
238329977Shselasky{
239329977Shselasky	struct timespec ts;
240329977Shselasky
241329977Shselasky	nanouptime(&ts);
242329977Shselasky	return (ktime_to_ns(timespec_to_ktime(ts)));
243329977Shselasky}
244329977Shselasky
245328653Shselasky#endif /* _LINUX_KTIME_H */
246