ktime.h revision 289624
1/* $FreeBSD: head/sys/ofed/include/linux/ktime.h 289624 2015-10-20 11:40:04Z hselasky $ */
2/*-
3 * Copyright (c) 2014-2015 Mellanox Technologies, Ltd.
4 * Copyright (c) 2015 Fran��ois Tigeot
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef _LINUX_KTIME_H
30#define _LINUX_KTIME_H
31
32#include <linux/types.h>
33#include <linux/time.h>
34#include <linux/jiffies.h>
35
36#define ktime_get_ts(x) getnanouptime(x)
37
38/* time values in nanoseconds */
39union ktime {
40	int64_t tv64;
41};
42
43typedef union ktime ktime_t;
44
45#define KTIME_MAX                       ((s64)~((u64)1 << 63))
46#define KTIME_SEC_MAX                   (KTIME_MAX / NSEC_PER_SEC)
47
48static inline int64_t
49ktime_to_ns(ktime_t kt)
50{
51	return kt.tv64;
52}
53
54static inline struct timeval
55ktime_to_timeval(ktime_t kt)
56{
57	return ns_to_timeval(kt.tv64);
58}
59
60static inline ktime_t
61ktime_add_ns(ktime_t kt, int64_t ns)
62{
63	ktime_t res;
64
65	res.tv64 = kt.tv64 + ns;
66	return kt;
67}
68
69static inline ktime_t
70ktime_sub_ns(ktime_t kt, int64_t ns)
71{
72	ktime_t res;
73
74	res.tv64 = kt.tv64 - ns;
75	return kt;
76}
77
78static inline ktime_t
79ktime_set(const long secs, const unsigned long nsecs)
80{
81	ktime_t retval = { (s64)secs * NSEC_PER_SEC + (s64)nsecs };
82	return (retval);
83}
84
85static inline ktime_t
86ktime_sub(ktime_t lhs, ktime_t rhs)
87{
88	lhs.tv64 -= rhs.tv64;
89	return (lhs);
90}
91
92static inline ktime_t
93ktime_add(ktime_t lhs, ktime_t rhs)
94{
95	lhs.tv64 += rhs.tv64;
96	return (lhs);
97}
98
99static inline ktime_t
100timespec_to_ktime(struct timespec ts)
101{
102	return (ktime_set(ts.tv_sec, ts.tv_nsec));
103}
104
105static inline ktime_t
106timeval_to_ktime(struct timeval tv)
107{
108	return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC));
109}
110
111#define ktime_to_timespec(kt)		ns_to_timespec((kt).tv64)
112#define ktime_to_timeval(kt)		ns_to_timeval((kt).tv64)
113#define ktime_to_ns(kt)			((kt).tv64)
114
115static inline s64
116ktime_get_ns(void)
117{
118	struct timespec ts;
119	ktime_t kt;
120
121	ktime_get_ts(&ts);
122	kt = timespec_to_ktime(ts);
123	return (ktime_to_ns(kt));
124}
125
126static inline ktime_t
127ktime_get(void)
128{
129	struct timespec ts;
130
131	ktime_get_ts(&ts);
132	return (timespec_to_ktime(ts));
133}
134
135#endif	/* _LINUX_KTIME_H */
136