xref: /netbsd/sys/external/bsd/drm2/include/linux/time.h (revision 483c4957)
1 /*	$NetBSD: time.h,v 1.11 2021/12/19 12:30:23 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_TIME_H_
33 #define _LINUX_TIME_H_
34 
35 #include <sys/time.h>
36 
37 #include <linux/math64.h>
38 #include <linux/seqlock.h>
39 
40 #define	timespec64	timespec	/* take that, 2038 */
41 
42 #define NSEC_PER_USEC	1000L
43 #define NSEC_PER_MSEC	1000000L
44 #define	NSEC_PER_SEC	1000000000L
45 #define USEC_PER_MSEC	1000L
46 
47 /*
48  * XXX get_seconds as implemented by Linux is a Y2038 bug waiting to
49  * happen on 32-bit systems because it returns unsigned long.  Some
50  * callers in Linux (implicitly) convert the result to time_t, though.
51  * We'll pretend get_seconds returns time_t and make sure all our
52  * callers treat it as if it did.
53  */
54 
55 static inline time_t
get_seconds(void)56 get_seconds(void)
57 {
58 	return time_second;
59 }
60 
61 static inline void
getrawmonotonic(struct timespec * ts)62 getrawmonotonic(struct timespec *ts)
63 {
64 	getnanouptime(ts);
65 }
66 
67 static inline void
do_gettimeofday(struct timeval * tv)68 do_gettimeofday(struct timeval *tv)
69 {
70 	microtime(tv);
71 }
72 
73 static inline bool
timespec_valid(const struct timespec * ts)74 timespec_valid(const struct timespec *ts)
75 {
76 	if (ts->tv_sec < 0)
77 		return false;
78 	if (1000000000L <= ts->tv_nsec)
79 		return false;
80 	return true;
81 }
82 
83 static inline struct timespec
ns_to_timespec(const int64_t nsec)84 ns_to_timespec(const int64_t nsec)
85 {
86 	struct timespec ts;
87 
88 	ts.tv_sec = (nsec / 1000000000L);
89 	ts.tv_nsec = (nsec % 1000000000L);
90 	if (ts.tv_nsec < 0) {
91 		ts.tv_sec -= 1;
92 		ts.tv_nsec += 1000000000L;
93 	}
94 
95 	return ts;
96 }
97 
98 static inline int64_t
timespec_to_ns(const struct timespec * ts)99 timespec_to_ns(const struct timespec *ts)
100 {
101 	return (((int64_t)ts->tv_sec * 1000000000LL) + ts->tv_nsec);
102 }
103 
104 static inline struct timeval
ns_to_timeval(int64_t nsec)105 ns_to_timeval(int64_t nsec)
106 {
107 	struct timespec ts;
108 	struct timeval tv;
109 
110 	ts = ns_to_timespec(nsec);
111 	TIMESPEC_TO_TIMEVAL(&tv, &ts);
112 
113 	return tv;
114 }
115 
116 static inline int64_t
timeval_to_ns(const struct timeval * tv)117 timeval_to_ns(const struct timeval *tv)
118 {
119 	return (((int64_t)tv->tv_sec * 1000000000UL) + (tv->tv_usec * 1000ul));
120 }
121 
122 static inline struct timespec
timespec_sub(struct timespec a,struct timespec b)123 timespec_sub(struct timespec a, struct timespec b)
124 {
125 	struct timespec d;
126 
127 	timespecsub(&a, &b, &d);
128 
129 	return d;
130 }
131 
132 static inline void
set_normalized_timespec(struct timespec * ts,time_t sec,int64_t nsec)133 set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec)
134 {
135 	while (nsec >= 1000000000L) {
136 		nsec -= 1000000000L;
137 		sec += 1;
138 	}
139 	while (nsec < 0) {
140 		nsec += 1000000000L;
141 		sec -= 1;
142 	}
143 	ts->tv_sec = sec;
144 	ts->tv_nsec = nsec;
145 }
146 
147 static inline bool
time_after32(uint32_t a,uint32_t b)148 time_after32(uint32_t a, uint32_t b)
149 {
150 	return (int32_t)(b - a) < 0;
151 }
152 
153 #endif  /* _LINUX_TIME_H_ */
154