1 /*	$NetBSD: jiffies.h,v 1.14 2022/03/18 23:32:49 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_JIFFIES_H_
33 #define _LINUX_JIFFIES_H_
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 
38 #include <asm/param.h>		/* HZ */
39 
40 #include <linux/math64.h>
41 
42 #define	jiffies		getticks()
43 #define	jiffies_64	getticks() /* XXX */
44 
45 /* XXX Er, what?  */
46 #define	MAX_JIFFY_OFFSET	((INT_MAX >> 1) - 1)
47 
48 static inline uint64_t
get_jiffies_64(void)49 get_jiffies_64(void)
50 {
51 
52 	return (uint64_t)(unsigned)getticks();
53 }
54 
55 static inline uint64_t
nsecs_to_jiffies64(uint64_t nsec)56 nsecs_to_jiffies64(uint64_t nsec)
57 {
58 
59 	/* XXX Arbitrary cutoff, should review the arithmetic.  */
60 	if (((1000000000 % hz) == 0) || (nsec >= 20000000000ul))
61 		return (nsec/1000000000)*hz;
62 	else
63 		return (nsec*hz)/1000000000;
64 }
65 
66 static inline uint32_t
nsecs_to_jiffies(uint64_t nsec)67 nsecs_to_jiffies(uint64_t nsec)
68 {
69 
70 	/* XXX Not sure what else to do but truncate...  */
71 	return (uint32_t)nsecs_to_jiffies64(nsec);
72 }
73 
74 static inline unsigned int
msecs_to_jiffies(unsigned int msec)75 msecs_to_jiffies(unsigned int msec)
76 {
77 	return mstohz(msec);
78 }
79 
80 static inline unsigned int
jiffies_to_msecs(unsigned int j)81 jiffies_to_msecs(unsigned int j)
82 {
83 	return hztoms(j);
84 }
85 
86 static inline unsigned int
usecs_to_jiffies(unsigned int usec)87 usecs_to_jiffies(unsigned int usec)
88 {
89 	const struct timeval tv = {
90 		.tv_sec = usec / 1000000,
91 		.tv_usec = usec % 1000000,
92 	};
93 
94 	return tvtohz(&tv);
95 }
96 
97 static inline unsigned int
jiffies_to_usecs(unsigned int j)98 jiffies_to_usecs(unsigned int j)
99 {
100 
101 	/* XXX Do better arithmetic.  */
102 	return (unsigned int)((unsigned long)j*1000000/hz);
103 }
104 
105 static inline unsigned int
timespec_to_jiffies(const struct timespec * ts)106 timespec_to_jiffies(const struct timespec *ts)
107 {
108 	return tstohz(ts);
109 }
110 
111 /* XXX long is the wrong type here times...  */
112 
113 #define	__linux_time_compare(A, OP, B)	(((long)(A) - (long)(B)) OP 0)
114 
115 static inline bool
time_after(unsigned long a,unsigned long b)116 time_after(unsigned long a, unsigned long b)
117 {
118 	return __linux_time_compare(a, >, b);
119 }
120 
121 static inline bool
time_after_eq(unsigned long a,unsigned long b)122 time_after_eq(unsigned long a, unsigned long b)
123 {
124 	return __linux_time_compare(a, >=, b);
125 }
126 
127 static inline bool
time_before(unsigned long a,unsigned long b)128 time_before(unsigned long a, unsigned long b)
129 {
130 	return __linux_time_compare(a, <, b);
131 }
132 
133 static inline bool
time_before_eq(unsigned long a,unsigned long b)134 time_before_eq(unsigned long a, unsigned long b)
135 {
136 	return __linux_time_compare(a, <=, b);
137 }
138 
139 #endif  /* _LINUX_JIFFIES_H_ */
140