08dc59f475d7b9f5572041b4f19b24c5831a8b63
[linux-2.6.git] / include / linux / jiffies.h
1 #ifndef _LINUX_JIFFIES_H
2 #define _LINUX_JIFFIES_H
3
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/time.h>
7 #include <linux/timex.h>
8 #include <asm/param.h>                  /* for HZ */
9 #include <asm/div64.h>
10
11 #ifndef div_long_long_rem
12 #define div_long_long_rem(dividend,divisor,remainder) \
13 ({                                                      \
14         u64 result = dividend;                          \
15         *remainder = do_div(result,divisor);            \
16         result;                                         \
17 })
18 #endif
19
20 /*
21  * The following defines establish the engineering parameters of the PLL
22  * model. The HZ variable establishes the timer interrupt frequency, 100 Hz
23  * for the SunOS kernel, 256 Hz for the Ultrix kernel and 1024 Hz for the
24  * OSF/1 kernel. The SHIFT_HZ define expresses the same value as the
25  * nearest power of two in order to avoid hardware multiply operations.
26  */
27 #if HZ >= 12 && HZ < 24
28 # define SHIFT_HZ       4
29 #elif HZ >= 24 && HZ < 48
30 # define SHIFT_HZ       5
31 #elif HZ >= 48 && HZ < 96
32 # define SHIFT_HZ       6
33 #elif HZ >= 96 && HZ < 192
34 # define SHIFT_HZ       7
35 #elif HZ >= 192 && HZ < 384
36 # define SHIFT_HZ       8
37 #elif HZ >= 384 && HZ < 768
38 # define SHIFT_HZ       9
39 #elif HZ >= 768 && HZ < 1536
40 # define SHIFT_HZ       10
41 #elif HZ >= 1536 && HZ < 3072
42 # define SHIFT_HZ      11
43 #elif HZ >= 3072 && HZ < 6144
44 # define SHIFT_HZ      12
45 #elif HZ >= 6144 && HZ < 12288
46 # define SHIFT_HZ      13
47 #elif HZ >= 12288 && HZ < 24576
48 # define SHIFT_HZ      14
49 #else
50 # error You lose.
51 #endif
52
53 /* LATCH is used in the interval timer and ftape setup. */
54 #define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)  /* For divider */
55
56 /* Suppose we want to devide two numbers NOM and DEN: NOM/DEN, the we can
57  * improve accuracy by shifting LSH bits, hence calculating:
58  *     (NOM << LSH) / DEN
59  * This however means trouble for large NOM, because (NOM << LSH) may no
60  * longer fit in 32 bits. The following way of calculating this gives us
61  * some slack, under the following conditions:
62  *   - (NOM / DEN) fits in (32 - LSH) bits.
63  *   - (NOM % DEN) fits in (32 - LSH) bits.
64  */
65 #define SH_DIV(NOM,DEN,LSH) (   ((NOM / DEN) << LSH)                    \
66                              + (((NOM % DEN) << LSH) + DEN / 2) / DEN)
67
68 /* HZ is the requested value. ACTHZ is actual HZ ("<< 8" is for accuracy) */
69 #define ACTHZ (SH_DIV (CLOCK_TICK_RATE, LATCH, 8))
70
71 /* TICK_NSEC is the time between ticks in nsec assuming real ACTHZ */
72 #define TICK_NSEC (SH_DIV (1000000UL * 1000, ACTHZ, 8))
73
74 /* TICK_USEC is the time between ticks in usec assuming fake USER_HZ */
75 #define TICK_USEC ((1000000UL + USER_HZ/2) / USER_HZ)
76
77 /* TICK_USEC_TO_NSEC is the time between ticks in nsec assuming real ACTHZ and  */
78 /* a value TUSEC for TICK_USEC (can be set bij adjtimex)                */
79 #define TICK_USEC_TO_NSEC(TUSEC) (SH_DIV (TUSEC * USER_HZ * 1000, ACTHZ, 8))
80
81 /* some arch's have a small-data section that can be accessed register-relative
82  * but that can only take up to, say, 4-byte variables. jiffies being part of
83  * an 8-byte variable may not be correctly accessed unless we force the issue
84  */
85 #define __jiffy_data  __attribute__((section(".data")))
86
87 /*
88  * The 64-bit value is not volatile - you MUST NOT read it
89  * without sampling the sequence number in xtime_lock.
90  * get_jiffies_64() will do this for you as appropriate.
91  */
92 extern u64 __jiffy_data jiffies_64;
93 extern unsigned long volatile __jiffy_data jiffies;
94
95 #if (BITS_PER_LONG < 64)
96 u64 get_jiffies_64(void);
97 #else
98 static inline u64 get_jiffies_64(void)
99 {
100         return (u64)jiffies;
101 }
102 #endif
103
104 /*
105  *      These inlines deal with timer wrapping correctly. You are 
106  *      strongly encouraged to use them
107  *      1. Because people otherwise forget
108  *      2. Because if the timer wrap changes in future you won't have to
109  *         alter your driver code.
110  *
111  * time_after(a,b) returns true if the time a is after time b.
112  *
113  * Do this with "<0" and ">=0" to only test the sign of the result. A
114  * good compiler would generate better code (and a really good compiler
115  * wouldn't care). Gcc is currently neither.
116  */
117 #define time_after(a,b)         \
118         (typecheck(unsigned long, a) && \
119          typecheck(unsigned long, b) && \
120          ((long)(b) - (long)(a) < 0))
121 #define time_before(a,b)        time_after(b,a)
122
123 #define time_after_eq(a,b)      \
124         (typecheck(unsigned long, a) && \
125          typecheck(unsigned long, b) && \
126          ((long)(a) - (long)(b) >= 0))
127 #define time_before_eq(a,b)     time_after_eq(b,a)
128
129 /*
130  * Have the 32 bit jiffies value wrap 5 minutes after boot
131  * so jiffies wrap bugs show up earlier.
132  */
133 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
134
135 /*
136  * Change timeval to jiffies, trying to avoid the
137  * most obvious overflows..
138  *
139  * And some not so obvious.
140  *
141  * Note that we don't want to return MAX_LONG, because
142  * for various timeout reasons we often end up having
143  * to wait "jiffies+1" in order to guarantee that we wait
144  * at _least_ "jiffies" - so "jiffies+1" had better still
145  * be positive.
146  */
147 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
148
149 /*
150  * We want to do realistic conversions of time so we need to use the same
151  * values the update wall clock code uses as the jiffies size.  This value
152  * is: TICK_NSEC (which is defined in timex.h).  This
153  * is a constant and is in nanoseconds.  We will used scaled math
154  * with a set of scales defined here as SEC_JIFFIE_SC,  USEC_JIFFIE_SC and
155  * NSEC_JIFFIE_SC.  Note that these defines contain nothing but
156  * constants and so are computed at compile time.  SHIFT_HZ (computed in
157  * timex.h) adjusts the scaling for different HZ values.
158
159  * Scaled math???  What is that?
160  *
161  * Scaled math is a way to do integer math on values that would,
162  * otherwise, either overflow, underflow, or cause undesired div
163  * instructions to appear in the execution path.  In short, we "scale"
164  * up the operands so they take more bits (more precision, less
165  * underflow), do the desired operation and then "scale" the result back
166  * by the same amount.  If we do the scaling by shifting we avoid the
167  * costly mpy and the dastardly div instructions.
168
169  * Suppose, for example, we want to convert from seconds to jiffies
170  * where jiffies is defined in nanoseconds as NSEC_PER_JIFFIE.  The
171  * simple math is: jiff = (sec * NSEC_PER_SEC) / NSEC_PER_JIFFIE; We
172  * observe that (NSEC_PER_SEC / NSEC_PER_JIFFIE) is a constant which we
173  * might calculate at compile time, however, the result will only have
174  * about 3-4 bits of precision (less for smaller values of HZ).
175  *
176  * So, we scale as follows:
177  * jiff = (sec) * (NSEC_PER_SEC / NSEC_PER_JIFFIE);
178  * jiff = ((sec) * ((NSEC_PER_SEC * SCALE)/ NSEC_PER_JIFFIE)) / SCALE;
179  * Then we make SCALE a power of two so:
180  * jiff = ((sec) * ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE)) >> SCALE;
181  * Now we define:
182  * #define SEC_CONV = ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE))
183  * jiff = (sec * SEC_CONV) >> SCALE;
184  *
185  * Often the math we use will expand beyond 32-bits so we tell C how to
186  * do this and pass the 64-bit result of the mpy through the ">> SCALE"
187  * which should take the result back to 32-bits.  We want this expansion
188  * to capture as much precision as possible.  At the same time we don't
189  * want to overflow so we pick the SCALE to avoid this.  In this file,
190  * that means using a different scale for each range of HZ values (as
191  * defined in timex.h).
192  *
193  * For those who want to know, gcc will give a 64-bit result from a "*"
194  * operator if the result is a long long AND at least one of the
195  * operands is cast to long long (usually just prior to the "*" so as
196  * not to confuse it into thinking it really has a 64-bit operand,
197  * which, buy the way, it can do, but it take more code and at least 2
198  * mpys).
199
200  * We also need to be aware that one second in nanoseconds is only a
201  * couple of bits away from overflowing a 32-bit word, so we MUST use
202  * 64-bits to get the full range time in nanoseconds.
203
204  */
205
206 /*
207  * Here are the scales we will use.  One for seconds, nanoseconds and
208  * microseconds.
209  *
210  * Within the limits of cpp we do a rough cut at the SEC_JIFFIE_SC and
211  * check if the sign bit is set.  If not, we bump the shift count by 1.
212  * (Gets an extra bit of precision where we can use it.)
213  * We know it is set for HZ = 1024 and HZ = 100 not for 1000.
214  * Haven't tested others.
215
216  * Limits of cpp (for #if expressions) only long (no long long), but
217  * then we only need the most signicant bit.
218  */
219
220 #define SEC_JIFFIE_SC (31 - SHIFT_HZ)
221 #if !((((NSEC_PER_SEC << 2) / TICK_NSEC) << (SEC_JIFFIE_SC - 2)) & 0x80000000)
222 #undef SEC_JIFFIE_SC
223 #define SEC_JIFFIE_SC (32 - SHIFT_HZ)
224 #endif
225 #define NSEC_JIFFIE_SC (SEC_JIFFIE_SC + 29)
226 #define USEC_JIFFIE_SC (SEC_JIFFIE_SC + 19)
227 #define SEC_CONVERSION ((unsigned long)((((u64)NSEC_PER_SEC << SEC_JIFFIE_SC) +\
228                                 TICK_NSEC -1) / (u64)TICK_NSEC))
229
230 #define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC) +\
231                                         TICK_NSEC -1) / (u64)TICK_NSEC))
232 #define USEC_CONVERSION  \
233                     ((unsigned long)((((u64)NSEC_PER_USEC << USEC_JIFFIE_SC) +\
234                                         TICK_NSEC -1) / (u64)TICK_NSEC))
235 /*
236  * USEC_ROUND is used in the timeval to jiffie conversion.  See there
237  * for more details.  It is the scaled resolution rounding value.  Note
238  * that it is a 64-bit value.  Since, when it is applied, we are already
239  * in jiffies (albit scaled), it is nothing but the bits we will shift
240  * off.
241  */
242 #define USEC_ROUND (u64)(((u64)1 << USEC_JIFFIE_SC) - 1)
243 /*
244  * The maximum jiffie value is (MAX_INT >> 1).  Here we translate that
245  * into seconds.  The 64-bit case will overflow if we are not careful,
246  * so use the messy SH_DIV macro to do it.  Still all constants.
247  */
248 #if BITS_PER_LONG < 64
249 # define MAX_SEC_IN_JIFFIES \
250         (long)((u64)((u64)MAX_JIFFY_OFFSET * TICK_NSEC) / NSEC_PER_SEC)
251 #else   /* take care of overflow on 64 bits machines */
252 # define MAX_SEC_IN_JIFFIES \
253         (SH_DIV((MAX_JIFFY_OFFSET >> SEC_JIFFIE_SC) * TICK_NSEC, NSEC_PER_SEC, 1) - 1)
254
255 #endif
256
257 /*
258  * Convert jiffies to milliseconds and back.
259  *
260  * Avoid unnecessary multiplications/divisions in the
261  * two most common HZ cases:
262  */
263 static inline unsigned int jiffies_to_msecs(const unsigned long j)
264 {
265 #if HZ <= 1000 && !(1000 % HZ)
266         return (1000 / HZ) * j;
267 #elif HZ > 1000 && !(HZ % 1000)
268         return (j + (HZ / 1000) - 1)/(HZ / 1000);
269 #else
270         return (j * 1000) / HZ;
271 #endif
272 }
273
274 static inline unsigned int jiffies_to_usecs(const unsigned long j)
275 {
276 #if HZ <= 1000000 && !(1000000 % HZ)
277         return (1000000 / HZ) * j;
278 #elif HZ > 1000000 && !(HZ % 1000000)
279         return (j + (HZ / 1000000) - 1)/(HZ / 1000000);
280 #else
281         return (j * 1000000) / HZ;
282 #endif
283 }
284
285 static inline unsigned long msecs_to_jiffies(const unsigned int m)
286 {
287         if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
288                 return MAX_JIFFY_OFFSET;
289 #if HZ <= 1000 && !(1000 % HZ)
290         return (m + (1000 / HZ) - 1) / (1000 / HZ);
291 #elif HZ > 1000 && !(HZ % 1000)
292         return m * (HZ / 1000);
293 #else
294         return (m * HZ + 999) / 1000;
295 #endif
296 }
297
298 static inline unsigned long usecs_to_jiffies(const unsigned int u)
299 {
300         if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
301                 return MAX_JIFFY_OFFSET;
302 #if HZ <= 1000000 && !(1000000 % HZ)
303         return (u + (1000000 / HZ) - 1) / (1000000 / HZ);
304 #elif HZ > 1000000 && !(HZ % 1000000)
305         return u * (HZ / 1000000);
306 #else
307         return (u * HZ + 999999) / 1000000;
308 #endif
309 }
310
311 /*
312  * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
313  * that a remainder subtract here would not do the right thing as the
314  * resolution values don't fall on second boundries.  I.e. the line:
315  * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
316  *
317  * Rather, we just shift the bits off the right.
318  *
319  * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
320  * value to a scaled second value.
321  */
322 static __inline__ unsigned long
323 timespec_to_jiffies(const struct timespec *value)
324 {
325         unsigned long sec = value->tv_sec;
326         long nsec = value->tv_nsec + TICK_NSEC - 1;
327
328         if (sec >= MAX_SEC_IN_JIFFIES){
329                 sec = MAX_SEC_IN_JIFFIES;
330                 nsec = 0;
331         }
332         return (((u64)sec * SEC_CONVERSION) +
333                 (((u64)nsec * NSEC_CONVERSION) >>
334                  (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
335
336 }
337
338 static __inline__ void
339 jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
340 {
341         /*
342          * Convert jiffies to nanoseconds and separate with
343          * one divide.
344          */
345         u64 nsec = (u64)jiffies * TICK_NSEC;
346         value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_nsec);
347 }
348
349 /* Same for "timeval"
350  *
351  * Well, almost.  The problem here is that the real system resolution is
352  * in nanoseconds and the value being converted is in micro seconds.
353  * Also for some machines (those that use HZ = 1024, in-particular),
354  * there is a LARGE error in the tick size in microseconds.
355
356  * The solution we use is to do the rounding AFTER we convert the
357  * microsecond part.  Thus the USEC_ROUND, the bits to be shifted off.
358  * Instruction wise, this should cost only an additional add with carry
359  * instruction above the way it was done above.
360  */
361 static __inline__ unsigned long
362 timeval_to_jiffies(const struct timeval *value)
363 {
364         unsigned long sec = value->tv_sec;
365         long usec = value->tv_usec;
366
367         if (sec >= MAX_SEC_IN_JIFFIES){
368                 sec = MAX_SEC_IN_JIFFIES;
369                 usec = 0;
370         }
371         return (((u64)sec * SEC_CONVERSION) +
372                 (((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
373                  (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
374 }
375
376 static __inline__ void
377 jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
378 {
379         /*
380          * Convert jiffies to nanoseconds and separate with
381          * one divide.
382          */
383         u64 nsec = (u64)jiffies * TICK_NSEC;
384         value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_usec);
385         value->tv_usec /= NSEC_PER_USEC;
386 }
387
388 /*
389  * Convert jiffies/jiffies_64 to clock_t and back.
390  */
391 static inline clock_t jiffies_to_clock_t(long x)
392 {
393 #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
394         return x / (HZ / USER_HZ);
395 #else
396         u64 tmp = (u64)x * TICK_NSEC;
397         do_div(tmp, (NSEC_PER_SEC / USER_HZ));
398         return (long)tmp;
399 #endif
400 }
401
402 static inline unsigned long clock_t_to_jiffies(unsigned long x)
403 {
404 #if (HZ % USER_HZ)==0
405         if (x >= ~0UL / (HZ / USER_HZ))
406                 return ~0UL;
407         return x * (HZ / USER_HZ);
408 #else
409         u64 jif;
410
411         /* Don't worry about loss of precision here .. */
412         if (x >= ~0UL / HZ * USER_HZ)
413                 return ~0UL;
414
415         /* .. but do try to contain it here */
416         jif = x * (u64) HZ;
417         do_div(jif, USER_HZ);
418         return jif;
419 #endif
420 }
421
422 static inline u64 jiffies_64_to_clock_t(u64 x)
423 {
424 #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
425         do_div(x, HZ / USER_HZ);
426 #else
427         /*
428          * There are better ways that don't overflow early,
429          * but even this doesn't overflow in hundreds of years
430          * in 64 bits, so..
431          */
432         x *= TICK_NSEC;
433         do_div(x, (NSEC_PER_SEC / USER_HZ));
434 #endif
435         return x;
436 }
437
438 static inline u64 nsec_to_clock_t(u64 x)
439 {
440 #if (NSEC_PER_SEC % USER_HZ) == 0
441         do_div(x, (NSEC_PER_SEC / USER_HZ));
442 #elif (USER_HZ % 512) == 0
443         x *= USER_HZ/512;
444         do_div(x, (NSEC_PER_SEC / 512));
445 #else
446         /*
447          * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
448          * overflow after 64.99 years.
449          * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
450          */
451         x *= 9;
452         do_div(x, (unsigned long)((9ull * NSEC_PER_SEC + (USER_HZ/2))
453                                   / USER_HZ));
454 #endif
455         return x;
456 }
457
458 #endif