1/*- 2 * Copyright (c) 2018 Limelight Networks, Inc. 3 * Copyright (c) 2014-2018 Mellanox Technologies, Ltd. 4 * Copyright (c) 2015 Fran��ois Tigeot 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#ifndef _LINUXKPI_LINUX_KTIME_H 30#define _LINUXKPI_LINUX_KTIME_H 31 32#include <linux/types.h> 33#include <linux/time.h> 34#include <linux/jiffies.h> 35 36/* time values in nanoseconds */ 37typedef s64 ktime_t; 38 39#define KTIME_MAX ((s64)~((u64)1 << 63)) 40#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 41 42static inline int64_t 43ktime_to_ns(ktime_t kt) 44{ 45 return (kt); 46} 47 48static inline ktime_t 49ns_to_ktime(uint64_t nsec) 50{ 51 return (nsec); 52} 53 54static inline int64_t 55ktime_divns(const ktime_t kt, int64_t div) 56{ 57 return (kt / div); 58} 59 60static inline int64_t 61ktime_to_us(ktime_t kt) 62{ 63 return (ktime_divns(kt, NSEC_PER_USEC)); 64} 65 66static inline int64_t 67ktime_to_ms(ktime_t kt) 68{ 69 return (ktime_divns(kt, NSEC_PER_MSEC)); 70} 71 72static inline ktime_t 73ms_to_ktime(uint64_t ms) 74{ 75 return (ms * NSEC_PER_MSEC); 76} 77 78static inline struct timeval 79ktime_to_timeval(ktime_t kt) 80{ 81 return (ns_to_timeval(kt)); 82} 83 84static inline ktime_t 85ktime_add_ns(ktime_t kt, int64_t ns) 86{ 87 return (kt + ns); 88} 89 90static inline ktime_t 91ktime_add_ms(ktime_t kt, int64_t ms) 92{ 93 94 return (ktime_add_ns(kt, ms * NSEC_PER_MSEC)); 95} 96 97static inline ktime_t 98ktime_add_us(ktime_t kt, int64_t us) 99{ 100 101 return (ktime_add_ns(kt, us * NSEC_PER_USEC)); 102} 103 104static inline ktime_t 105ktime_sub_ns(ktime_t kt, int64_t ns) 106{ 107 return (kt - ns); 108} 109 110static inline ktime_t 111ktime_set(const long secs, const unsigned long nsecs) 112{ 113 ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs}; 114 115 return (retval); 116} 117 118static inline ktime_t 119ktime_sub(ktime_t lhs, ktime_t rhs) 120{ 121 return (lhs - rhs); 122} 123 124static inline int64_t 125ktime_us_delta(ktime_t later, ktime_t earlier) 126{ 127 ktime_t diff = ktime_sub(later, earlier); 128 129 return (ktime_to_us(diff)); 130} 131 132static inline int64_t 133ktime_ms_delta(ktime_t later, ktime_t earlier) 134{ 135 ktime_t diff = ktime_sub(later, earlier); 136 137 return (ktime_to_ms(diff)); 138} 139 140static inline ktime_t 141ktime_add(ktime_t lhs, ktime_t rhs) 142{ 143 return (lhs + rhs); 144} 145 146static inline int 147ktime_compare(const ktime_t cmp1, const ktime_t cmp2) 148{ 149 150 if (cmp1 > cmp2) 151 return (1); 152 else if (cmp1 < cmp2) 153 return (-1); 154 else 155 return (0); 156} 157 158static inline bool 159ktime_after(const ktime_t cmp1, const ktime_t cmp2) 160{ 161 162 return (ktime_compare(cmp1, cmp2) > 0); 163} 164 165static inline bool 166ktime_before(const ktime_t cmp1, const ktime_t cmp2) 167{ 168 169 return (ktime_compare(cmp1, cmp2) < 0); 170} 171 172static inline ktime_t 173timespec_to_ktime(struct timespec ts) 174{ 175 return (ktime_set(ts.tv_sec, ts.tv_nsec)); 176} 177 178static inline ktime_t 179timeval_to_ktime(struct timeval tv) 180{ 181 return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC)); 182} 183 184static inline int64_t 185timespec64_to_ns(struct timespec64 *ts) 186{ 187 return (timespec_to_ns(ts)); 188} 189 190#define ktime_to_timespec(kt) ns_to_timespec(kt) 191#define ktime_to_timespec64(kt) ns_to_timespec(kt) 192#define ktime_to_timeval(kt) ns_to_timeval(kt) 193#define ktime_to_ns(kt) (kt) 194#define ktime_get_ts(ts) getnanouptime(ts) 195#define ktime_get_ts64(ts) getnanouptime(ts) 196#define ktime_get_raw_ts64(ts) getnanouptime(ts) 197#define ktime_get_real_ts64(ts) getnanotime(ts) 198#define getrawmonotonic64(ts) getnanouptime(ts) 199 200static inline int64_t 201ktime_get_ns(void) 202{ 203 struct timespec ts; 204 205 ktime_get_ts(&ts); 206 207 return (ktime_to_ns(timespec_to_ktime(ts))); 208} 209 210static inline ktime_t 211ktime_get(void) 212{ 213 struct timespec ts; 214 215 ktime_get_ts(&ts); 216 return (timespec_to_ktime(ts)); 217} 218 219static inline ktime_t 220ktime_get_boottime(void) 221{ 222 struct timespec ts; 223 224 nanouptime(&ts); 225 return (timespec_to_ktime(ts)); 226} 227 228static inline uint64_t 229ktime_get_boottime_ns(void) 230{ 231 232 return (ktime_to_ns(ktime_get_boottime())); 233} 234 235static inline ktime_t 236ktime_get_real(void) 237{ 238 struct timespec ts; 239 240 nanotime(&ts); 241 return (timespec_to_ktime(ts)); 242} 243 244static inline ktime_t 245ktime_get_real_seconds(void) 246{ 247 struct timespec ts; 248 249 nanotime(&ts); 250 return (ts.tv_sec); 251} 252 253static inline ktime_t 254ktime_get_raw(void) 255{ 256 struct timespec ts; 257 258 nanouptime(&ts); 259 return (timespec_to_ktime(ts)); 260} 261 262static inline u64 263ktime_get_raw_ns(void) 264{ 265 struct timespec ts; 266 267 nanouptime(&ts); 268 return (ktime_to_ns(timespec_to_ktime(ts))); 269} 270 271static inline uint64_t 272ktime_get_raw_fast_ns(void) 273{ 274 struct timespec ts; 275 276 getnanouptime(&ts); 277 return (ktime_to_ns(timespec_to_ktime(ts))); 278} 279 280#endif /* _LINUXKPI_LINUX_KTIME_H */ 281