xref: /openbsd/sys/dev/pci/drm/include/linux/ktime.h (revision a751cc56)
1 /*	$OpenBSD: ktime.h,v 1.8 2024/03/28 02:36:38 jsg Exp $	*/
2 /*
3  * Copyright (c) 2013, 2014, 2015 Mark Kettenis
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _LINUX_KTIME_H
19 #define _LINUX_KTIME_H
20 
21 #include <sys/time.h>
22 #include <linux/time.h>
23 #include <linux/jiffies.h>
24 
25 typedef int64_t ktime_t;
26 #define KTIME_MAX INT64_MAX
27 
28 static inline ktime_t
ktime_get(void)29 ktime_get(void)
30 {
31 	struct timespec ts;
32 	nanouptime(&ts);
33 	return TIMESPEC_TO_NSEC(&ts);
34 }
35 
36 static inline ktime_t
ktime_get_raw(void)37 ktime_get_raw(void)
38 {
39 	struct timespec ts;
40 	nanouptime(&ts);
41 	return TIMESPEC_TO_NSEC(&ts);
42 }
43 
44 static inline int64_t
ktime_to_ms(ktime_t k)45 ktime_to_ms(ktime_t k)
46 {
47 	return k / NSEC_PER_MSEC;
48 }
49 
50 static inline int64_t
ktime_to_us(ktime_t k)51 ktime_to_us(ktime_t k)
52 {
53 	return k / NSEC_PER_USEC;
54 }
55 
56 static inline int64_t
ktime_to_ns(ktime_t k)57 ktime_to_ns(ktime_t k)
58 {
59 	return k;
60 }
61 
62 static inline int64_t
ktime_get_raw_ns(void)63 ktime_get_raw_ns(void)
64 {
65 	return ktime_to_ns(ktime_get_raw());
66 }
67 
68 static inline int64_t
ktime_get_raw_fast_ns(void)69 ktime_get_raw_fast_ns(void)
70 {
71 	return ktime_to_ns(ktime_get_raw());
72 }
73 
74 static inline struct timespec64
ktime_to_timespec64(ktime_t k)75 ktime_to_timespec64(ktime_t k)
76 {
77 	struct timespec64 ts;
78 	ts.tv_sec = k / NSEC_PER_SEC;
79 	ts.tv_nsec = k % NSEC_PER_SEC;
80 	if (ts.tv_nsec < 0) {
81 		ts.tv_sec--;
82 		ts.tv_nsec += NSEC_PER_SEC;
83 	}
84 	return ts;
85 }
86 
87 static inline ktime_t
ktime_sub(ktime_t a,ktime_t b)88 ktime_sub(ktime_t a, ktime_t b)
89 {
90 	return a - b;
91 }
92 
93 static inline ktime_t
ktime_add(ktime_t a,ktime_t b)94 ktime_add(ktime_t a, ktime_t b)
95 {
96 	return a + b;
97 }
98 
99 static inline ktime_t
ktime_add_us(ktime_t k,uint64_t us)100 ktime_add_us(ktime_t k, uint64_t us)
101 {
102 	return k + (us * NSEC_PER_USEC);
103 }
104 
105 static inline ktime_t
ktime_add_ms(ktime_t k,uint64_t ms)106 ktime_add_ms(ktime_t k, uint64_t ms)
107 {
108 	return k + (ms * NSEC_PER_MSEC);
109 }
110 
111 static inline ktime_t
ktime_add_ns(ktime_t k,int64_t ns)112 ktime_add_ns(ktime_t k, int64_t ns)
113 {
114 	return k + ns;
115 }
116 
117 static inline ktime_t
ktime_sub_ns(ktime_t k,int64_t ns)118 ktime_sub_ns(ktime_t k, int64_t ns)
119 {
120 	return k - ns;
121 }
122 
123 static inline int64_t
ktime_us_delta(ktime_t a,ktime_t b)124 ktime_us_delta(ktime_t a, ktime_t b)
125 {
126 	return ktime_to_us(ktime_sub(a, b));
127 }
128 
129 static inline int64_t
ktime_ms_delta(ktime_t a,ktime_t b)130 ktime_ms_delta(ktime_t a, ktime_t b)
131 {
132 	return ktime_to_ms(ktime_sub(a, b));
133 }
134 
135 static inline bool
ktime_before(ktime_t a,ktime_t b)136 ktime_before(ktime_t a, ktime_t b)
137 {
138 	return a < b;
139 }
140 
141 static inline bool
ktime_after(ktime_t a,ktime_t b)142 ktime_after(ktime_t a, ktime_t b)
143 {
144 	return a > b;
145 }
146 
147 static inline ktime_t
ns_to_ktime(uint64_t ns)148 ns_to_ktime(uint64_t ns)
149 {
150 	return ns;
151 }
152 
153 static inline ktime_t
ms_to_ktime(uint64_t ms)154 ms_to_ktime(uint64_t ms)
155 {
156 	return ms * NSEC_PER_MSEC;
157 }
158 
159 static inline int64_t
ktime_divns(ktime_t a,int64_t ns)160 ktime_divns(ktime_t a, int64_t ns)
161 {
162 	return a / ns;
163 }
164 
165 static inline ktime_t
ktime_set(time_t s,long ns)166 ktime_set(time_t s, long ns)
167 {
168 	struct timespec ts;
169 	ts.tv_sec = s;
170 	ts.tv_nsec = ns;
171 	return TIMESPEC_TO_NSEC(&ts);
172 }
173 
174 #include <linux/timekeeping.h>
175 
176 #endif
177