1a6f1bef2SFrançois Tigeot /* 29ad212b2SFrançois Tigeot * Copyright (c) 2014-2015 François Tigeot 3a6f1bef2SFrançois Tigeot * All rights reserved. 4a6f1bef2SFrançois Tigeot * 5a6f1bef2SFrançois Tigeot * Redistribution and use in source and binary forms, with or without 6a6f1bef2SFrançois Tigeot * modification, are permitted provided that the following conditions 7a6f1bef2SFrançois Tigeot * are met: 8a6f1bef2SFrançois Tigeot * 1. Redistributions of source code must retain the above copyright 9a6f1bef2SFrançois Tigeot * notice unmodified, this list of conditions, and the following 10a6f1bef2SFrançois Tigeot * disclaimer. 11a6f1bef2SFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright 12a6f1bef2SFrançois Tigeot * notice, this list of conditions and the following disclaimer in the 13a6f1bef2SFrançois Tigeot * documentation and/or other materials provided with the distribution. 14a6f1bef2SFrançois Tigeot * 15a6f1bef2SFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16a6f1bef2SFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17a6f1bef2SFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18a6f1bef2SFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19a6f1bef2SFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20a6f1bef2SFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21a6f1bef2SFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22a6f1bef2SFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23a6f1bef2SFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24a6f1bef2SFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25a6f1bef2SFrançois Tigeot */ 26a6f1bef2SFrançois Tigeot 27a6f1bef2SFrançois Tigeot #ifndef _LINUX_JIFFIES_H_ 28a6f1bef2SFrançois Tigeot #define _LINUX_JIFFIES_H_ 29a6f1bef2SFrançois Tigeot 30e3440f96SFrançois Tigeot #include <sys/kernel.h> 31e3440f96SFrançois Tigeot #include <machine/limits.h> 32e3440f96SFrançois Tigeot 33563a794dSFrançois Tigeot #include <linux/math64.h> 34563a794dSFrançois Tigeot #include <linux/time.h> 35563a794dSFrançois Tigeot 36e3440f96SFrançois Tigeot #define HZ hz 37a6f1bef2SFrançois Tigeot 38dd664e5cSFrançois Tigeot #define MAX_JIFFY_OFFSET ((LONG_MAX >> 1) - 1) 39dd664e5cSFrançois Tigeot 40a6f1bef2SFrançois Tigeot #define jiffies_to_msecs(x) (((int64_t)(x)) * 1000 / hz) 41a6f1bef2SFrançois Tigeot #define msecs_to_jiffies(x) (((int64_t)(x)) * hz / 1000) 42a6f1bef2SFrançois Tigeot #define jiffies ticks 434c7821dbSzrj #define jiffies_64 ticks /* XXX hmmm */ 44a6f1bef2SFrançois Tigeot #define time_after(a,b) ((long)(b) - (long)(a) < 0) 45a6f1bef2SFrançois Tigeot #define time_after_eq(a,b) ((long)(b) - (long)(a) <= 0) 46a6f1bef2SFrançois Tigeot 47dd664e5cSFrançois Tigeot #define time_before(a,b) time_after(b,a) 48dd664e5cSFrançois Tigeot #define time_before_eq(a,b) time_after_eq(b,a) 49dd664e5cSFrançois Tigeot 50dd664e5cSFrançois Tigeot #define time_in_range(a,b,c) \ 51dd664e5cSFrançois Tigeot time_after_eq(a,b) && time_before_eq(a,c) 52dd664e5cSFrançois Tigeot 53a6f1bef2SFrançois Tigeot static inline unsigned long 54a6f1bef2SFrançois Tigeot timespec_to_jiffies(const struct timespec *ts) 55a6f1bef2SFrançois Tigeot { 56a6f1bef2SFrançois Tigeot unsigned long result; 57a6f1bef2SFrançois Tigeot 58a6f1bef2SFrançois Tigeot result = ((unsigned long)hz * ts->tv_sec) + (ts->tv_nsec / NSEC_PER_SEC); 59a6f1bef2SFrançois Tigeot if (result > LONG_MAX) 60a6f1bef2SFrançois Tigeot result = LONG_MAX; 61a6f1bef2SFrançois Tigeot 62a6f1bef2SFrançois Tigeot return result; 63a6f1bef2SFrançois Tigeot } 64a6f1bef2SFrançois Tigeot 659ad212b2SFrançois Tigeot static inline 669ad212b2SFrançois Tigeot unsigned long usecs_to_jiffies(const unsigned int u) 679ad212b2SFrançois Tigeot { 689ad212b2SFrançois Tigeot unsigned long jiffies; 699ad212b2SFrançois Tigeot 709ad212b2SFrançois Tigeot jiffies = (u * hz) / 1000000; 719ad212b2SFrançois Tigeot if (jiffies < 1) 729ad212b2SFrançois Tigeot return 1; 739ad212b2SFrançois Tigeot else 749ad212b2SFrançois Tigeot return jiffies; 759ad212b2SFrançois Tigeot } 769ad212b2SFrançois Tigeot 7761e58940SFrançois Tigeot static inline u64 get_jiffies_64(void) 7861e58940SFrançois Tigeot { 7961e58940SFrançois Tigeot return (u64)ticks; 8061e58940SFrançois Tigeot } 8161e58940SFrançois Tigeot 82cba13cc1SFrançois Tigeot static inline u64 nsecs_to_jiffies(u64 n) 83cba13cc1SFrançois Tigeot { 84cba13cc1SFrançois Tigeot return (n * hz) / NSEC_PER_SEC; 85cba13cc1SFrançois Tigeot } 86cba13cc1SFrançois Tigeot 87*4ab8b20fSFrançois Tigeot static inline u64 nsecs_to_jiffies64(u64 n) 88*4ab8b20fSFrançois Tigeot { 89*4ab8b20fSFrançois Tigeot return nsecs_to_jiffies(n); 90*4ab8b20fSFrançois Tigeot } 91*4ab8b20fSFrançois Tigeot 92*4ab8b20fSFrançois Tigeot static inline 93*4ab8b20fSFrançois Tigeot unsigned int jiffies_to_usecs(const unsigned long j) 94*4ab8b20fSFrançois Tigeot { 95*4ab8b20fSFrançois Tigeot return j * (USEC_PER_SEC / HZ); 96*4ab8b20fSFrançois Tigeot } 97*4ab8b20fSFrançois Tigeot 98a6f1bef2SFrançois Tigeot #endif /* _LINUX_JIFFIES_H_ */ 99