xref: /dragonfly/sys/dev/drm/include/linux/time.h (revision d998b496)
174681b5dSFrançois Tigeot /*
230c432c9SFrançois Tigeot  * Copyright (c) 2014-2015 François Tigeot
374681b5dSFrançois Tigeot  * All rights reserved.
474681b5dSFrançois Tigeot  *
574681b5dSFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
674681b5dSFrançois Tigeot  * modification, are permitted provided that the following conditions
774681b5dSFrançois Tigeot  * are met:
874681b5dSFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
974681b5dSFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
1074681b5dSFrançois Tigeot  *    disclaimer.
1174681b5dSFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
1274681b5dSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
1374681b5dSFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
1474681b5dSFrançois Tigeot  *
1574681b5dSFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1674681b5dSFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1774681b5dSFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1874681b5dSFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1974681b5dSFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2074681b5dSFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2174681b5dSFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2274681b5dSFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2374681b5dSFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2474681b5dSFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2574681b5dSFrançois Tigeot  */
2674681b5dSFrançois Tigeot 
2774681b5dSFrançois Tigeot #ifndef _LINUX_TIME_H_
2874681b5dSFrançois Tigeot #define _LINUX_TIME_H_
2974681b5dSFrançois Tigeot 
304ab8b20fSFrançois Tigeot #define USEC_PER_SEC	1000000L
314ab8b20fSFrançois Tigeot 
3274681b5dSFrançois Tigeot #define NSEC_PER_USEC	1000L
33788808afSFrançois Tigeot #define NSEC_PER_MSEC	1000000L
3474681b5dSFrançois Tigeot #define NSEC_PER_SEC	1000000000L
3574681b5dSFrançois Tigeot 
36*d998b496SFrançois Tigeot #include <linux/cache.h>
37563a794dSFrançois Tigeot #include <linux/math64.h>
3874681b5dSFrançois Tigeot 
39*d998b496SFrançois Tigeot #include <sys/time.h>
40*d998b496SFrançois Tigeot 
4174681b5dSFrançois Tigeot static inline struct timeval
4274681b5dSFrançois Tigeot ns_to_timeval(const int64_t nsec)
4374681b5dSFrançois Tigeot {
4474681b5dSFrançois Tigeot 	struct timeval tv;
4574681b5dSFrançois Tigeot 	long rem;
4674681b5dSFrançois Tigeot 
4774681b5dSFrançois Tigeot 	if (nsec == 0) {
4874681b5dSFrançois Tigeot 		tv.tv_sec = 0;
4974681b5dSFrançois Tigeot 		tv.tv_usec = 0;
5074681b5dSFrançois Tigeot 		return (tv);
5174681b5dSFrançois Tigeot 	}
5274681b5dSFrançois Tigeot 
5374681b5dSFrançois Tigeot 	tv.tv_sec = nsec / NSEC_PER_SEC;
5474681b5dSFrançois Tigeot 	rem = nsec % NSEC_PER_SEC;
5574681b5dSFrançois Tigeot 	if (rem < 0) {
5674681b5dSFrançois Tigeot 		tv.tv_sec--;
5774681b5dSFrançois Tigeot 		rem += NSEC_PER_SEC;
5874681b5dSFrançois Tigeot 	}
5974681b5dSFrançois Tigeot 	tv.tv_usec = rem / 1000;
6074681b5dSFrançois Tigeot 	return (tv);
6174681b5dSFrançois Tigeot }
6274681b5dSFrançois Tigeot 
6374681b5dSFrançois Tigeot static inline int64_t
6474681b5dSFrançois Tigeot timeval_to_ns(const struct timeval *tv)
6574681b5dSFrançois Tigeot {
6674681b5dSFrançois Tigeot 	return ((int64_t)tv->tv_sec * NSEC_PER_SEC) +
6774681b5dSFrançois Tigeot 		tv->tv_usec * NSEC_PER_USEC;
6874681b5dSFrançois Tigeot }
6974681b5dSFrançois Tigeot 
7074681b5dSFrançois Tigeot #define getrawmonotonic(ts)	nanouptime(ts)
7174681b5dSFrançois Tigeot 
7274681b5dSFrançois Tigeot static inline struct timespec
7374681b5dSFrançois Tigeot timespec_sub(struct timespec lhs, struct timespec rhs)
7474681b5dSFrançois Tigeot {
7574681b5dSFrançois Tigeot 	struct timespec ts;
7674681b5dSFrançois Tigeot 
7774681b5dSFrançois Tigeot 	ts.tv_sec = lhs.tv_sec;
7874681b5dSFrançois Tigeot 	ts.tv_nsec = lhs.tv_nsec;
7974681b5dSFrançois Tigeot 	timespecsub(&ts, &rhs);
8074681b5dSFrançois Tigeot 
8174681b5dSFrançois Tigeot 	return ts;
8274681b5dSFrançois Tigeot }
8374681b5dSFrançois Tigeot 
8474681b5dSFrançois Tigeot static inline void
8574681b5dSFrançois Tigeot set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec)
8674681b5dSFrançois Tigeot {
8774681b5dSFrançois Tigeot 	/* XXX: this doesn't actually normalize anything */
8874681b5dSFrançois Tigeot 	ts->tv_sec = sec;
8974681b5dSFrançois Tigeot 	ts->tv_nsec = nsec;
9074681b5dSFrançois Tigeot }
9174681b5dSFrançois Tigeot 
9216ad3371SFrançois Tigeot static inline int64_t
9316ad3371SFrançois Tigeot timespec_to_ns(const struct timespec *ts)
9416ad3371SFrançois Tigeot {
9516ad3371SFrançois Tigeot 	return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec);
9616ad3371SFrançois Tigeot }
9716ad3371SFrançois Tigeot 
9816ad3371SFrançois Tigeot static inline struct timespec
9916ad3371SFrançois Tigeot ns_to_timespec(const int64_t nsec)
10016ad3371SFrançois Tigeot {
10116ad3371SFrançois Tigeot 	struct timespec ts;
10216ad3371SFrançois Tigeot 	int32_t rem;
10316ad3371SFrançois Tigeot 
10416ad3371SFrançois Tigeot 	if (nsec == 0) {
10516ad3371SFrançois Tigeot 		ts.tv_sec = 0;
10616ad3371SFrançois Tigeot 		ts.tv_nsec = 0;
10716ad3371SFrançois Tigeot 		return (ts);
10816ad3371SFrançois Tigeot 	}
10916ad3371SFrançois Tigeot 
11016ad3371SFrançois Tigeot 	ts.tv_sec = nsec / NSEC_PER_SEC;
11116ad3371SFrançois Tigeot 	rem = nsec % NSEC_PER_SEC;
11216ad3371SFrançois Tigeot 	if (rem < 0) {
11316ad3371SFrançois Tigeot 		ts.tv_sec--;
11416ad3371SFrançois Tigeot 		rem += NSEC_PER_SEC;
11516ad3371SFrançois Tigeot 	}
11616ad3371SFrançois Tigeot 	ts.tv_nsec = rem;
11716ad3371SFrançois Tigeot 	return (ts);
11816ad3371SFrançois Tigeot }
11916ad3371SFrançois Tigeot 
12016ad3371SFrançois Tigeot static inline int
12116ad3371SFrançois Tigeot timespec_valid(const struct timespec *ts)
12216ad3371SFrançois Tigeot {
12316ad3371SFrançois Tigeot 	if (ts->tv_sec < 0 || ts->tv_sec > 100000000 ||
12416ad3371SFrançois Tigeot 	    ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
12516ad3371SFrançois Tigeot 		return (0);
12616ad3371SFrançois Tigeot 	return (1);
12716ad3371SFrançois Tigeot }
12816ad3371SFrançois Tigeot 
12930c432c9SFrançois Tigeot static inline unsigned long
13030c432c9SFrançois Tigeot get_seconds(void)
13130c432c9SFrançois Tigeot {
13230c432c9SFrançois Tigeot 	return time_uptime;
13330c432c9SFrançois Tigeot }
13430c432c9SFrançois Tigeot 
13574681b5dSFrançois Tigeot #endif	/* _LINUX_TIME_H_ */
136