xref: /dragonfly/sys/dev/drm/include/linux/time.h (revision ef3ac1d1)
1 /*
2  * Copyright (c) 2014 François Tigeot
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef _LINUX_TIME_H_
28 #define _LINUX_TIME_H_
29 
30 #define NSEC_PER_USEC	1000L
31 #define NSEC_PER_SEC	1000000000L
32 
33 #include <sys/time.h>
34 
35 static inline struct timeval
36 ns_to_timeval(const int64_t nsec)
37 {
38 	struct timeval tv;
39 	long rem;
40 
41 	if (nsec == 0) {
42 		tv.tv_sec = 0;
43 		tv.tv_usec = 0;
44 		return (tv);
45 	}
46 
47 	tv.tv_sec = nsec / NSEC_PER_SEC;
48 	rem = nsec % NSEC_PER_SEC;
49 	if (rem < 0) {
50 		tv.tv_sec--;
51 		rem += NSEC_PER_SEC;
52 	}
53 	tv.tv_usec = rem / 1000;
54 	return (tv);
55 }
56 
57 static inline int64_t
58 timeval_to_ns(const struct timeval *tv)
59 {
60 	return ((int64_t)tv->tv_sec * NSEC_PER_SEC) +
61 		tv->tv_usec * NSEC_PER_USEC;
62 }
63 
64 #define getrawmonotonic(ts)	nanouptime(ts)
65 
66 static inline struct timespec
67 timespec_sub(struct timespec lhs, struct timespec rhs)
68 {
69 	struct timespec ts;
70 
71 	ts.tv_sec = lhs.tv_sec;
72 	ts.tv_nsec = lhs.tv_nsec;
73 	timespecsub(&ts, &rhs);
74 
75 	return ts;
76 }
77 
78 static inline void
79 set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec)
80 {
81 	/* XXX: this doesn't actually normalize anything */
82 	ts->tv_sec = sec;
83 	ts->tv_nsec = nsec;
84 }
85 
86 static inline int64_t
87 timespec_to_ns(const struct timespec *ts)
88 {
89 	return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec);
90 }
91 
92 static inline struct timespec
93 ns_to_timespec(const int64_t nsec)
94 {
95 	struct timespec ts;
96 	int32_t rem;
97 
98 	if (nsec == 0) {
99 		ts.tv_sec = 0;
100 		ts.tv_nsec = 0;
101 		return (ts);
102 	}
103 
104 	ts.tv_sec = nsec / NSEC_PER_SEC;
105 	rem = nsec % NSEC_PER_SEC;
106 	if (rem < 0) {
107 		ts.tv_sec--;
108 		rem += NSEC_PER_SEC;
109 	}
110 	ts.tv_nsec = rem;
111 	return (ts);
112 }
113 
114 static inline int
115 timespec_valid(const struct timespec *ts)
116 {
117 	if (ts->tv_sec < 0 || ts->tv_sec > 100000000 ||
118 	    ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
119 		return (0);
120 	return (1);
121 }
122 
123 #endif	/* _LINUX_TIME_H_ */
124