![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 #ifndef _LINUX_JIFFIES_H 0003 #define _LINUX_JIFFIES_H 0004 0005 #include <linux/cache.h> 0006 #include <linux/limits.h> 0007 #include <linux/math64.h> 0008 #include <linux/minmax.h> 0009 #include <linux/types.h> 0010 #include <linux/time.h> 0011 #include <linux/timex.h> 0012 #include <vdso/jiffies.h> 0013 #include <asm/param.h> /* for HZ */ 0014 #include <generated/timeconst.h> 0015 0016 /* 0017 * The following defines establish the engineering parameters of the PLL 0018 * model. The HZ variable establishes the timer interrupt frequency, 100 Hz 0019 * for the SunOS kernel, 256 Hz for the Ultrix kernel and 1024 Hz for the 0020 * OSF/1 kernel. The SHIFT_HZ define expresses the same value as the 0021 * nearest power of two in order to avoid hardware multiply operations. 0022 */ 0023 #if HZ >= 12 && HZ < 24 0024 # define SHIFT_HZ 4 0025 #elif HZ >= 24 && HZ < 48 0026 # define SHIFT_HZ 5 0027 #elif HZ >= 48 && HZ < 96 0028 # define SHIFT_HZ 6 0029 #elif HZ >= 96 && HZ < 192 0030 # define SHIFT_HZ 7 0031 #elif HZ >= 192 && HZ < 384 0032 # define SHIFT_HZ 8 0033 #elif HZ >= 384 && HZ < 768 0034 # define SHIFT_HZ 9 0035 #elif HZ >= 768 && HZ < 1536 0036 # define SHIFT_HZ 10 0037 #elif HZ >= 1536 && HZ < 3072 0038 # define SHIFT_HZ 11 0039 #elif HZ >= 3072 && HZ < 6144 0040 # define SHIFT_HZ 12 0041 #elif HZ >= 6144 && HZ < 12288 0042 # define SHIFT_HZ 13 0043 #else 0044 # error Invalid value of HZ. 0045 #endif 0046 0047 /* Suppose we want to divide two numbers NOM and DEN: NOM/DEN, then we can 0048 * improve accuracy by shifting LSH bits, hence calculating: 0049 * (NOM << LSH) / DEN 0050 * This however means trouble for large NOM, because (NOM << LSH) may no 0051 * longer fit in 32 bits. The following way of calculating this gives us 0052 * some slack, under the following conditions: 0053 * - (NOM / DEN) fits in (32 - LSH) bits. 0054 * - (NOM % DEN) fits in (32 - LSH) bits. 0055 */ 0056 #define SH_DIV(NOM,DEN,LSH) ( (((NOM) / (DEN)) << (LSH)) \ 0057 + ((((NOM) % (DEN)) << (LSH)) + (DEN) / 2) / (DEN)) 0058 0059 /* LATCH is used in the interval timer and ftape setup. */ 0060 #define LATCH ((CLOCK_TICK_RATE + HZ/2) / HZ) /* For divider */ 0061 0062 extern int register_refined_jiffies(long clock_tick_rate); 0063 0064 /* TICK_USEC is the time between ticks in usec assuming SHIFTED_HZ */ 0065 #define TICK_USEC ((USEC_PER_SEC + HZ/2) / HZ) 0066 0067 /* USER_TICK_USEC is the time between ticks in usec assuming fake USER_HZ */ 0068 #define USER_TICK_USEC ((1000000UL + USER_HZ/2) / USER_HZ) 0069 0070 #ifndef __jiffy_arch_data 0071 #define __jiffy_arch_data 0072 #endif 0073 0074 /* 0075 * The 64-bit value is not atomic - you MUST NOT read it 0076 * without sampling the sequence number in jiffies_lock. 0077 * get_jiffies_64() will do this for you as appropriate. 0078 */ 0079 extern u64 __cacheline_aligned_in_smp jiffies_64; 0080 extern unsigned long volatile __cacheline_aligned_in_smp __jiffy_arch_data jiffies; 0081 0082 #if (BITS_PER_LONG < 64) 0083 u64 get_jiffies_64(void); 0084 #else 0085 static inline u64 get_jiffies_64(void) 0086 { 0087 return (u64)jiffies; 0088 } 0089 #endif 0090 0091 /* 0092 * These inlines deal with timer wrapping correctly. You are 0093 * strongly encouraged to use them 0094 * 1. Because people otherwise forget 0095 * 2. Because if the timer wrap changes in future you won't have to 0096 * alter your driver code. 0097 * 0098 * time_after(a,b) returns true if the time a is after time b. 0099 * 0100 * Do this with "<0" and ">=0" to only test the sign of the result. A 0101 * good compiler would generate better code (and a really good compiler 0102 * wouldn't care). Gcc is currently neither. 0103 */ 0104 #define time_after(a,b) \ 0105 (typecheck(unsigned long, a) && \ 0106 typecheck(unsigned long, b) && \ 0107 ((long)((b) - (a)) < 0)) 0108 #define time_before(a,b) time_after(b,a) 0109 0110 #define time_after_eq(a,b) \ 0111 (typecheck(unsigned long, a) && \ 0112 typecheck(unsigned long, b) && \ 0113 ((long)((a) - (b)) >= 0)) 0114 #define time_before_eq(a,b) time_after_eq(b,a) 0115 0116 /* 0117 * Calculate whether a is in the range of [b, c]. 0118 */ 0119 #define time_in_range(a,b,c) \ 0120 (time_after_eq(a,b) && \ 0121 time_before_eq(a,c)) 0122 0123 /* 0124 * Calculate whether a is in the range of [b, c). 0125 */ 0126 #define time_in_range_open(a,b,c) \ 0127 (time_after_eq(a,b) && \ 0128 time_before(a,c)) 0129 0130 /* Same as above, but does so with platform independent 64bit types. 0131 * These must be used when utilizing jiffies_64 (i.e. return value of 0132 * get_jiffies_64() */ 0133 #define time_after64(a,b) \ 0134 (typecheck(__u64, a) && \ 0135 typecheck(__u64, b) && \ 0136 ((__s64)((b) - (a)) < 0)) 0137 #define time_before64(a,b) time_after64(b,a) 0138 0139 #define time_after_eq64(a,b) \ 0140 (typecheck(__u64, a) && \ 0141 typecheck(__u64, b) && \ 0142 ((__s64)((a) - (b)) >= 0)) 0143 #define time_before_eq64(a,b) time_after_eq64(b,a) 0144 0145 #define time_in_range64(a, b, c) \ 0146 (time_after_eq64(a, b) && \ 0147 time_before_eq64(a, c)) 0148 0149 /* 0150 * These four macros compare jiffies and 'a' for convenience. 0151 */ 0152 0153 /* time_is_before_jiffies(a) return true if a is before jiffies */ 0154 #define time_is_before_jiffies(a) time_after(jiffies, a) 0155 #define time_is_before_jiffies64(a) time_after64(get_jiffies_64(), a) 0156 0157 /* time_is_after_jiffies(a) return true if a is after jiffies */ 0158 #define time_is_after_jiffies(a) time_before(jiffies, a) 0159 #define time_is_after_jiffies64(a) time_before64(get_jiffies_64(), a) 0160 0161 /* time_is_before_eq_jiffies(a) return true if a is before or equal to jiffies*/ 0162 #define time_is_before_eq_jiffies(a) time_after_eq(jiffies, a) 0163 #define time_is_before_eq_jiffies64(a) time_after_eq64(get_jiffies_64(), a) 0164 0165 /* time_is_after_eq_jiffies(a) return true if a is after or equal to jiffies*/ 0166 #define time_is_after_eq_jiffies(a) time_before_eq(jiffies, a) 0167 #define time_is_after_eq_jiffies64(a) time_before_eq64(get_jiffies_64(), a) 0168 0169 /* 0170 * Have the 32 bit jiffies value wrap 5 minutes after boot 0171 * so jiffies wrap bugs show up earlier. 0172 */ 0173 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ)) 0174 0175 /* 0176 * Change timeval to jiffies, trying to avoid the 0177 * most obvious overflows.. 0178 * 0179 * And some not so obvious. 0180 * 0181 * Note that we don't want to return LONG_MAX, because 0182 * for various timeout reasons we often end up having 0183 * to wait "jiffies+1" in order to guarantee that we wait 0184 * at _least_ "jiffies" - so "jiffies+1" had better still 0185 * be positive. 0186 */ 0187 #define MAX_JIFFY_OFFSET ((LONG_MAX >> 1)-1) 0188 0189 extern unsigned long preset_lpj; 0190 0191 /* 0192 * We want to do realistic conversions of time so we need to use the same 0193 * values the update wall clock code uses as the jiffies size. This value 0194 * is: TICK_NSEC (which is defined in timex.h). This 0195 * is a constant and is in nanoseconds. We will use scaled math 0196 * with a set of scales defined here as SEC_JIFFIE_SC, USEC_JIFFIE_SC and 0197 * NSEC_JIFFIE_SC. Note that these defines contain nothing but 0198 * constants and so are computed at compile time. SHIFT_HZ (computed in 0199 * timex.h) adjusts the scaling for different HZ values. 0200 0201 * Scaled math??? What is that? 0202 * 0203 * Scaled math is a way to do integer math on values that would, 0204 * otherwise, either overflow, underflow, or cause undesired div 0205 * instructions to appear in the execution path. In short, we "scale" 0206 * up the operands so they take more bits (more precision, less 0207 * underflow), do the desired operation and then "scale" the result back 0208 * by the same amount. If we do the scaling by shifting we avoid the 0209 * costly mpy and the dastardly div instructions. 0210 0211 * Suppose, for example, we want to convert from seconds to jiffies 0212 * where jiffies is defined in nanoseconds as NSEC_PER_JIFFIE. The 0213 * simple math is: jiff = (sec * NSEC_PER_SEC) / NSEC_PER_JIFFIE; We 0214 * observe that (NSEC_PER_SEC / NSEC_PER_JIFFIE) is a constant which we 0215 * might calculate at compile time, however, the result will only have 0216 * about 3-4 bits of precision (less for smaller values of HZ). 0217 * 0218 * So, we scale as follows: 0219 * jiff = (sec) * (NSEC_PER_SEC / NSEC_PER_JIFFIE); 0220 * jiff = ((sec) * ((NSEC_PER_SEC * SCALE)/ NSEC_PER_JIFFIE)) / SCALE; 0221 * Then we make SCALE a power of two so: 0222 * jiff = ((sec) * ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE)) >> SCALE; 0223 * Now we define: 0224 * #define SEC_CONV = ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE)) 0225 * jiff = (sec * SEC_CONV) >> SCALE; 0226 * 0227 * Often the math we use will expand beyond 32-bits so we tell C how to 0228 * do this and pass the 64-bit result of the mpy through the ">> SCALE" 0229 * which should take the result back to 32-bits. We want this expansion 0230 * to capture as much precision as possible. At the same time we don't 0231 * want to overflow so we pick the SCALE to avoid this. In this file, 0232 * that means using a different scale for each range of HZ values (as 0233 * defined in timex.h). 0234 * 0235 * For those who want to know, gcc will give a 64-bit result from a "*" 0236 * operator if the result is a long long AND at least one of the 0237 * operands is cast to long long (usually just prior to the "*" so as 0238 * not to confuse it into thinking it really has a 64-bit operand, 0239 * which, buy the way, it can do, but it takes more code and at least 2 0240 * mpys). 0241 0242 * We also need to be aware that one second in nanoseconds is only a 0243 * couple of bits away from overflowing a 32-bit word, so we MUST use 0244 * 64-bits to get the full range time in nanoseconds. 0245 0246 */ 0247 0248 /* 0249 * Here are the scales we will use. One for seconds, nanoseconds and 0250 * microseconds. 0251 * 0252 * Within the limits of cpp we do a rough cut at the SEC_JIFFIE_SC and 0253 * check if the sign bit is set. If not, we bump the shift count by 1. 0254 * (Gets an extra bit of precision where we can use it.) 0255 * We know it is set for HZ = 1024 and HZ = 100 not for 1000. 0256 * Haven't tested others. 0257 0258 * Limits of cpp (for #if expressions) only long (no long long), but 0259 * then we only need the most signicant bit. 0260 */ 0261 0262 #define SEC_JIFFIE_SC (31 - SHIFT_HZ) 0263 #if !((((NSEC_PER_SEC << 2) / TICK_NSEC) << (SEC_JIFFIE_SC - 2)) & 0x80000000) 0264 #undef SEC_JIFFIE_SC 0265 #define SEC_JIFFIE_SC (32 - SHIFT_HZ) 0266 #endif 0267 #define NSEC_JIFFIE_SC (SEC_JIFFIE_SC + 29) 0268 #define SEC_CONVERSION ((unsigned long)((((u64)NSEC_PER_SEC << SEC_JIFFIE_SC) +\ 0269 TICK_NSEC -1) / (u64)TICK_NSEC)) 0270 0271 #define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC) +\ 0272 TICK_NSEC -1) / (u64)TICK_NSEC)) 0273 /* 0274 * The maximum jiffie value is (MAX_INT >> 1). Here we translate that 0275 * into seconds. The 64-bit case will overflow if we are not careful, 0276 * so use the messy SH_DIV macro to do it. Still all constants. 0277 */ 0278 #if BITS_PER_LONG < 64 0279 # define MAX_SEC_IN_JIFFIES \ 0280 (long)((u64)((u64)MAX_JIFFY_OFFSET * TICK_NSEC) / NSEC_PER_SEC) 0281 #else /* take care of overflow on 64 bits machines */ 0282 # define MAX_SEC_IN_JIFFIES \ 0283 (SH_DIV((MAX_JIFFY_OFFSET >> SEC_JIFFIE_SC) * TICK_NSEC, NSEC_PER_SEC, 1) - 1) 0284 0285 #endif 0286 0287 /* 0288 * Convert various time units to each other: 0289 */ 0290 extern unsigned int jiffies_to_msecs(const unsigned long j); 0291 extern unsigned int jiffies_to_usecs(const unsigned long j); 0292 0293 static inline u64 jiffies_to_nsecs(const unsigned long j) 0294 { 0295 return (u64)jiffies_to_usecs(j) * NSEC_PER_USEC; 0296 } 0297 0298 extern u64 jiffies64_to_nsecs(u64 j); 0299 extern u64 jiffies64_to_msecs(u64 j); 0300 0301 extern unsigned long __msecs_to_jiffies(const unsigned int m); 0302 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) 0303 /* 0304 * HZ is equal to or smaller than 1000, and 1000 is a nice round 0305 * multiple of HZ, divide with the factor between them, but round 0306 * upwards: 0307 */ 0308 static inline unsigned long _msecs_to_jiffies(const unsigned int m) 0309 { 0310 return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); 0311 } 0312 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) 0313 /* 0314 * HZ is larger than 1000, and HZ is a nice round multiple of 1000 - 0315 * simply multiply with the factor between them. 0316 * 0317 * But first make sure the multiplication result cannot overflow: 0318 */ 0319 static inline unsigned long _msecs_to_jiffies(const unsigned int m) 0320 { 0321 if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) 0322 return MAX_JIFFY_OFFSET; 0323 return m * (HZ / MSEC_PER_SEC); 0324 } 0325 #else 0326 /* 0327 * Generic case - multiply, round and divide. But first check that if 0328 * we are doing a net multiplication, that we wouldn't overflow: 0329 */ 0330 static inline unsigned long _msecs_to_jiffies(const unsigned int m) 0331 { 0332 if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) 0333 return MAX_JIFFY_OFFSET; 0334 0335 return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32) >> MSEC_TO_HZ_SHR32; 0336 } 0337 #endif 0338 /** 0339 * msecs_to_jiffies: - convert milliseconds to jiffies 0340 * @m: time in milliseconds 0341 * 0342 * conversion is done as follows: 0343 * 0344 * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) 0345 * 0346 * - 'too large' values [that would result in larger than 0347 * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. 0348 * 0349 * - all other values are converted to jiffies by either multiplying 0350 * the input value by a factor or dividing it with a factor and 0351 * handling any 32-bit overflows. 0352 * for the details see __msecs_to_jiffies() 0353 * 0354 * msecs_to_jiffies() checks for the passed in value being a constant 0355 * via __builtin_constant_p() allowing gcc to eliminate most of the 0356 * code, __msecs_to_jiffies() is called if the value passed does not 0357 * allow constant folding and the actual conversion must be done at 0358 * runtime. 0359 * the HZ range specific helpers _msecs_to_jiffies() are called both 0360 * directly here and from __msecs_to_jiffies() in the case where 0361 * constant folding is not possible. 0362 */ 0363 static __always_inline unsigned long msecs_to_jiffies(const unsigned int m) 0364 { 0365 if (__builtin_constant_p(m)) { 0366 if ((int)m < 0) 0367 return MAX_JIFFY_OFFSET; 0368 return _msecs_to_jiffies(m); 0369 } else { 0370 return __msecs_to_jiffies(m); 0371 } 0372 } 0373 0374 extern unsigned long __usecs_to_jiffies(const unsigned int u); 0375 #if !(USEC_PER_SEC % HZ) 0376 static inline unsigned long _usecs_to_jiffies(const unsigned int u) 0377 { 0378 return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ); 0379 } 0380 #else 0381 static inline unsigned long _usecs_to_jiffies(const unsigned int u) 0382 { 0383 return (USEC_TO_HZ_MUL32 * u + USEC_TO_HZ_ADJ32) 0384 >> USEC_TO_HZ_SHR32; 0385 } 0386 #endif 0387 0388 /** 0389 * usecs_to_jiffies: - convert microseconds to jiffies 0390 * @u: time in microseconds 0391 * 0392 * conversion is done as follows: 0393 * 0394 * - 'too large' values [that would result in larger than 0395 * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. 0396 * 0397 * - all other values are converted to jiffies by either multiplying 0398 * the input value by a factor or dividing it with a factor and 0399 * handling any 32-bit overflows as for msecs_to_jiffies. 0400 * 0401 * usecs_to_jiffies() checks for the passed in value being a constant 0402 * via __builtin_constant_p() allowing gcc to eliminate most of the 0403 * code, __usecs_to_jiffies() is called if the value passed does not 0404 * allow constant folding and the actual conversion must be done at 0405 * runtime. 0406 * the HZ range specific helpers _usecs_to_jiffies() are called both 0407 * directly here and from __msecs_to_jiffies() in the case where 0408 * constant folding is not possible. 0409 */ 0410 static __always_inline unsigned long usecs_to_jiffies(const unsigned int u) 0411 { 0412 if (__builtin_constant_p(u)) { 0413 if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) 0414 return MAX_JIFFY_OFFSET; 0415 return _usecs_to_jiffies(u); 0416 } else { 0417 return __usecs_to_jiffies(u); 0418 } 0419 } 0420 0421 extern unsigned long timespec64_to_jiffies(const struct timespec64 *value); 0422 extern void jiffies_to_timespec64(const unsigned long jiffies, 0423 struct timespec64 *value); 0424 extern clock_t jiffies_to_clock_t(unsigned long x); 0425 static inline clock_t jiffies_delta_to_clock_t(long delta) 0426 { 0427 return jiffies_to_clock_t(max(0L, delta)); 0428 } 0429 0430 static inline unsigned int jiffies_delta_to_msecs(long delta) 0431 { 0432 return jiffies_to_msecs(max(0L, delta)); 0433 } 0434 0435 extern unsigned long clock_t_to_jiffies(unsigned long x); 0436 extern u64 jiffies_64_to_clock_t(u64 x); 0437 extern u64 nsec_to_clock_t(u64 x); 0438 extern u64 nsecs_to_jiffies64(u64 n); 0439 extern unsigned long nsecs_to_jiffies(u64 n); 0440 0441 #define TIMESTAMP_SIZE 30 0442 0443 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |