1 /*- 2 * Copyright (c) 2014-2015 Mellanox Technologies, Ltd. 3 * Copyright (c) 2015 François Tigeot 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 #ifndef _LINUX_KTIME_H 31 #define _LINUX_KTIME_H 32 33 #include <linux/types.h> 34 #include <linux/time.h> 35 #include <linux/jiffies.h> 36 37 #define ktime_get_ts(x) getnanouptime(x) 38 39 /* time values in nanoseconds */ 40 union ktime { 41 int64_t tv64; 42 }; 43 44 typedef union ktime ktime_t; 45 46 #define KTIME_MAX ((s64)~((u64)1 << 63)) 47 #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 48 49 static inline int64_t 50 ktime_to_ns(ktime_t kt) 51 { 52 return kt.tv64; 53 } 54 55 static inline ktime_t 56 ns_to_ktime(uint64_t nsec) 57 { 58 ktime_t kt; 59 60 kt.tv64 = nsec; 61 return (kt); 62 } 63 64 static inline int64_t 65 ktime_divns(const ktime_t kt, int64_t div) 66 { 67 return kt.tv64 / div; 68 } 69 70 static inline int64_t 71 ktime_to_us(ktime_t kt) 72 { 73 return ktime_divns(kt, NSEC_PER_USEC); 74 } 75 76 static inline int64_t 77 ktime_to_ms(ktime_t kt) 78 { 79 return ktime_divns(kt, NSEC_PER_MSEC); 80 } 81 82 static inline struct timeval 83 ktime_to_timeval(ktime_t kt) 84 { 85 return ns_to_timeval(kt.tv64); 86 } 87 88 static inline ktime_t 89 ktime_add_ns(ktime_t kt, int64_t ns) 90 { 91 ktime_t res; 92 93 res.tv64 = kt.tv64 + ns; 94 return kt; 95 } 96 97 static inline ktime_t 98 ktime_sub_ns(ktime_t kt, int64_t ns) 99 { 100 ktime_t res; 101 102 res.tv64 = kt.tv64 - ns; 103 return kt; 104 } 105 106 static inline ktime_t 107 ktime_set(const long secs, const unsigned long nsecs) 108 { 109 ktime_t retval = { (s64)secs * NSEC_PER_SEC + (s64)nsecs }; 110 return (retval); 111 } 112 113 static inline ktime_t 114 ktime_sub(ktime_t lhs, ktime_t rhs) 115 { 116 lhs.tv64 -= rhs.tv64; 117 return (lhs); 118 } 119 120 static inline int64_t 121 ktime_us_delta(ktime_t later, ktime_t earlier) 122 { 123 ktime_t diff = ktime_sub(later, earlier); 124 return ktime_to_us(diff); 125 } 126 127 static inline int64_t 128 ktime_ms_delta(ktime_t later, ktime_t earlier) 129 { 130 ktime_t diff = ktime_sub(later, earlier); 131 return ktime_to_ms(diff); 132 } 133 134 static inline ktime_t 135 ktime_add(ktime_t lhs, ktime_t rhs) 136 { 137 lhs.tv64 += rhs.tv64; 138 return (lhs); 139 } 140 141 static inline ktime_t 142 timespec_to_ktime(struct timespec ts) 143 { 144 return (ktime_set(ts.tv_sec, ts.tv_nsec)); 145 } 146 147 static inline ktime_t 148 timeval_to_ktime(struct timeval tv) 149 { 150 return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC)); 151 } 152 153 #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) 154 #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) 155 #define ktime_to_ns(kt) ((kt).tv64) 156 157 static inline int64_t 158 ktime_get_ns(void) 159 { 160 struct timespec ts; 161 ktime_t kt; 162 163 ktime_get_ts(&ts); 164 kt = timespec_to_ktime(ts); 165 return (ktime_to_ns(kt)); 166 } 167 168 #define ktime_get_raw_ns() ktime_get_ns() 169 170 static inline ktime_t 171 ktime_get(void) 172 { 173 struct timespec ts; 174 175 ktime_get_ts(&ts); 176 return (timespec_to_ktime(ts)); 177 } 178 179 static inline ktime_t 180 ktime_get_boottime(void) 181 { 182 struct timespec ts; 183 184 nanouptime(&ts); 185 return (timespec_to_ktime(ts)); 186 } 187 188 static inline ktime_t 189 ktime_get_real(void) 190 { 191 struct timespec ts; 192 193 nanotime(&ts); 194 return (timespec_to_ktime(ts)); 195 } 196 197 #endif /* _LINUX_KTIME_H */ 198