xref: /dflybsd-src/sys/dev/drm/include/linux/ktime.h (revision d4c6ae4e3d373751c79ca4322285cd3bcc2a71d4)
101c621f3SFrançois Tigeot /*
2*d4c6ae4eSFrançois Tigeot  * Copyright (c) 2015-2016 François Tigeot
301c621f3SFrançois Tigeot  * All rights reserved.
401c621f3SFrançois Tigeot  *
501c621f3SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
601c621f3SFrançois Tigeot  * modification, are permitted provided that the following conditions
701c621f3SFrançois Tigeot  * are met:
801c621f3SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
901c621f3SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
1001c621f3SFrançois Tigeot  *    disclaimer.
1101c621f3SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
1201c621f3SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
1301c621f3SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
1401c621f3SFrançois Tigeot  *
1501c621f3SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1601c621f3SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1701c621f3SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1801c621f3SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1901c621f3SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2001c621f3SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2101c621f3SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2201c621f3SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2301c621f3SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2401c621f3SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2501c621f3SFrançois Tigeot  */
2601c621f3SFrançois Tigeot 
2701c621f3SFrançois Tigeot #ifndef _LINUX_KTIME_H_
2801c621f3SFrançois Tigeot #define _LINUX_KTIME_H_
2901c621f3SFrançois Tigeot 
3001c621f3SFrançois Tigeot #include <linux/time.h>
3101c621f3SFrançois Tigeot #include <linux/jiffies.h>
3201c621f3SFrançois Tigeot 
3301c621f3SFrançois Tigeot /* time values in nanoseconds */
3401c621f3SFrançois Tigeot union ktime {
3501c621f3SFrançois Tigeot 	int64_t tv64;
3601c621f3SFrançois Tigeot };
3701c621f3SFrançois Tigeot 
3801c621f3SFrançois Tigeot typedef union ktime ktime_t;
3901c621f3SFrançois Tigeot 
40*d4c6ae4eSFrançois Tigeot static inline s64
41*d4c6ae4eSFrançois Tigeot ktime_to_us(const ktime_t kt)
42*d4c6ae4eSFrançois Tigeot {
43*d4c6ae4eSFrançois Tigeot 	return kt.tv64 / NSEC_PER_USEC;
44*d4c6ae4eSFrançois Tigeot }
45*d4c6ae4eSFrançois Tigeot 
46*d4c6ae4eSFrançois Tigeot static inline s64
47*d4c6ae4eSFrançois Tigeot ktime_us_delta(const ktime_t later, const ktime_t earlier)
48*d4c6ae4eSFrançois Tigeot {
49*d4c6ae4eSFrançois Tigeot 	return later.tv64 - earlier.tv64;
50*d4c6ae4eSFrançois Tigeot }
51*d4c6ae4eSFrançois Tigeot 
5201c621f3SFrançois Tigeot static inline int64_t ktime_to_ns(ktime_t kt)
5301c621f3SFrançois Tigeot {
5401c621f3SFrançois Tigeot 	return kt.tv64;
5501c621f3SFrançois Tigeot }
5601c621f3SFrançois Tigeot 
5701c621f3SFrançois Tigeot static inline struct timeval ktime_to_timeval(ktime_t kt)
5801c621f3SFrançois Tigeot {
5901c621f3SFrançois Tigeot 	return ns_to_timeval(kt.tv64);
6001c621f3SFrançois Tigeot }
6101c621f3SFrançois Tigeot 
6201c621f3SFrançois Tigeot static inline ktime_t ktime_add_ns(ktime_t kt, int64_t ns)
6301c621f3SFrançois Tigeot {
6401c621f3SFrançois Tigeot 	ktime_t res;
6501c621f3SFrançois Tigeot 
6601c621f3SFrançois Tigeot 	res.tv64 = kt.tv64 + ns;
6701c621f3SFrançois Tigeot 	return kt;
6801c621f3SFrançois Tigeot }
6901c621f3SFrançois Tigeot 
7001c621f3SFrançois Tigeot static inline ktime_t ktime_sub_ns(ktime_t kt, int64_t ns)
7101c621f3SFrançois Tigeot {
7201c621f3SFrançois Tigeot 	ktime_t res;
7301c621f3SFrançois Tigeot 
7401c621f3SFrançois Tigeot 	res.tv64 = kt.tv64 - ns;
7501c621f3SFrançois Tigeot 	return kt;
7601c621f3SFrançois Tigeot }
7701c621f3SFrançois Tigeot 
7801c621f3SFrançois Tigeot #define NSEC_PER_SEC	1000000000L
7901c621f3SFrançois Tigeot 
8001c621f3SFrançois Tigeot static inline ktime_t ktime_get(void)
8101c621f3SFrançois Tigeot {
8201c621f3SFrançois Tigeot 	struct timespec ts;
8301c621f3SFrançois Tigeot 	ktime_t kt;
8401c621f3SFrançois Tigeot 
8501c621f3SFrançois Tigeot 	nanouptime(&ts);
8601c621f3SFrançois Tigeot 	kt.tv64 = (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
8701c621f3SFrançois Tigeot 	return kt;
8801c621f3SFrançois Tigeot }
8901c621f3SFrançois Tigeot 
9081d31d56SFrançois Tigeot #include <linux/timekeeping.h>
9181d31d56SFrançois Tigeot 
9201c621f3SFrançois Tigeot #endif	/* _LINUX_KTIME_H_ */
93