MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / include / linux / time.h
blobeb10b1d2f181cba7d17bfc1c574cd41eb8ac29f2
1 #ifndef _LINUX_TIME_H
2 #define _LINUX_TIME_H
4 #include <asm/param.h>
5 #include <linux/types.h>
7 #ifndef _STRUCT_TIMESPEC
8 #define _STRUCT_TIMESPEC
9 struct timespec {
10 time_t tv_sec; /* seconds */
11 long tv_nsec; /* nanoseconds */
13 #endif /* _STRUCT_TIMESPEC */
15 struct timeval {
16 time_t tv_sec; /* seconds */
17 suseconds_t tv_usec; /* microseconds */
20 struct timezone {
21 int tz_minuteswest; /* minutes west of Greenwich */
22 int tz_dsttime; /* type of dst correction */
25 #ifdef __KERNEL__
27 #include <linux/spinlock.h>
28 #include <linux/seqlock.h>
29 #include <linux/timex.h>
30 #include <asm/div64.h>
31 #ifndef div_long_long_rem
33 #define div_long_long_rem(dividend,divisor,remainder) ({ \
34 u64 result = dividend; \
35 *remainder = do_div(result,divisor); \
36 result; })
38 #endif
41 * Have the 32 bit jiffies value wrap 5 minutes after boot
42 * so jiffies wrap bugs show up earlier.
44 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
47 * Change timeval to jiffies, trying to avoid the
48 * most obvious overflows..
50 * And some not so obvious.
52 * Note that we don't want to return MAX_LONG, because
53 * for various timeout reasons we often end up having
54 * to wait "jiffies+1" in order to guarantee that we wait
55 * at _least_ "jiffies" - so "jiffies+1" had better still
56 * be positive.
58 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
60 /* Parameters used to convert the timespec values */
61 #ifndef USEC_PER_SEC
62 #define USEC_PER_SEC (1000000L)
63 #endif
65 #ifndef NSEC_PER_SEC
66 #define NSEC_PER_SEC (1000000000L)
67 #endif
69 #ifndef NSEC_PER_USEC
70 #define NSEC_PER_USEC (1000L)
71 #endif
74 * We want to do realistic conversions of time so we need to use the same
75 * values the update wall clock code uses as the jiffies size. This value
76 * is: TICK_NSEC (which is defined in timex.h). This
77 * is a constant and is in nanoseconds. We will used scaled math
78 * with a set of scales defined here as SEC_JIFFIE_SC, USEC_JIFFIE_SC and
79 * NSEC_JIFFIE_SC. Note that these defines contain nothing but
80 * constants and so are computed at compile time. SHIFT_HZ (computed in
81 * timex.h) adjusts the scaling for different HZ values.
83 * Scaled math??? What is that?
85 * Scaled math is a way to do integer math on values that would,
86 * otherwise, either overflow, underflow, or cause undesired div
87 * instructions to appear in the execution path. In short, we "scale"
88 * up the operands so they take more bits (more precision, less
89 * underflow), do the desired operation and then "scale" the result back
90 * by the same amount. If we do the scaling by shifting we avoid the
91 * costly mpy and the dastardly div instructions.
93 * Suppose, for example, we want to convert from seconds to jiffies
94 * where jiffies is defined in nanoseconds as NSEC_PER_JIFFIE. The
95 * simple math is: jiff = (sec * NSEC_PER_SEC) / NSEC_PER_JIFFIE; We
96 * observe that (NSEC_PER_SEC / NSEC_PER_JIFFIE) is a constant which we
97 * might calculate at compile time, however, the result will only have
98 * about 3-4 bits of precision (less for smaller values of HZ).
100 * So, we scale as follows:
101 * jiff = (sec) * (NSEC_PER_SEC / NSEC_PER_JIFFIE);
102 * jiff = ((sec) * ((NSEC_PER_SEC * SCALE)/ NSEC_PER_JIFFIE)) / SCALE;
103 * Then we make SCALE a power of two so:
104 * jiff = ((sec) * ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE)) >> SCALE;
105 * Now we define:
106 * #define SEC_CONV = ((NSEC_PER_SEC << SCALE)/ NSEC_PER_JIFFIE))
107 * jiff = (sec * SEC_CONV) >> SCALE;
109 * Often the math we use will expand beyond 32-bits so we tell C how to
110 * do this and pass the 64-bit result of the mpy through the ">> SCALE"
111 * which should take the result back to 32-bits. We want this expansion
112 * to capture as much precision as possible. At the same time we don't
113 * want to overflow so we pick the SCALE to avoid this. In this file,
114 * that means using a different scale for each range of HZ values (as
115 * defined in timex.h).
117 * For those who want to know, gcc will give a 64-bit result from a "*"
118 * operator if the result is a long long AND at least one of the
119 * operands is cast to long long (usually just prior to the "*" so as
120 * not to confuse it into thinking it really has a 64-bit operand,
121 * which, buy the way, it can do, but it take more code and at least 2
122 * mpys).
124 * We also need to be aware that one second in nanoseconds is only a
125 * couple of bits away from overflowing a 32-bit word, so we MUST use
126 * 64-bits to get the full range time in nanoseconds.
131 * Here are the scales we will use. One for seconds, nanoseconds and
132 * microseconds.
134 * Within the limits of cpp we do a rough cut at the SEC_JIFFIE_SC and
135 * check if the sign bit is set. If not, we bump the shift count by 1.
136 * (Gets an extra bit of precision where we can use it.)
137 * We know it is set for HZ = 1024 and HZ = 100 not for 1000.
138 * Haven't tested others.
140 * Limits of cpp (for #if expressions) only long (no long long), but
141 * then we only need the most signicant bit.
144 #define SEC_JIFFIE_SC (31 - SHIFT_HZ)
145 #if !((((NSEC_PER_SEC << 2) / TICK_NSEC) << (SEC_JIFFIE_SC - 2)) & 0x80000000)
146 #undef SEC_JIFFIE_SC
147 #define SEC_JIFFIE_SC (32 - SHIFT_HZ)
148 #endif
149 #define NSEC_JIFFIE_SC (SEC_JIFFIE_SC + 29)
150 #define USEC_JIFFIE_SC (SEC_JIFFIE_SC + 19)
151 #define SEC_CONVERSION ((unsigned long)((((u64)NSEC_PER_SEC << SEC_JIFFIE_SC) +\
152 TICK_NSEC -1) / (u64)TICK_NSEC))
154 #define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC) +\
155 TICK_NSEC -1) / (u64)TICK_NSEC))
156 #define USEC_CONVERSION \
157 ((unsigned long)((((u64)NSEC_PER_USEC << USEC_JIFFIE_SC) +\
158 TICK_NSEC -1) / (u64)TICK_NSEC))
160 * USEC_ROUND is used in the timeval to jiffie conversion. See there
161 * for more details. It is the scaled resolution rounding value. Note
162 * that it is a 64-bit value. Since, when it is applied, we are already
163 * in jiffies (albit scaled), it is nothing but the bits we will shift
164 * off.
166 #define USEC_ROUND (u64)(((u64)1 << USEC_JIFFIE_SC) - 1)
168 * The maximum jiffie value is (MAX_INT >> 1). Here we translate that
169 * into seconds. The 64-bit case will overflow if we are not careful,
170 * so use the messy SH_DIV macro to do it. Still all constants.
172 #if BITS_PER_LONG < 64
173 # define MAX_SEC_IN_JIFFIES \
174 (long)((u64)((u64)MAX_JIFFY_OFFSET * TICK_NSEC) / NSEC_PER_SEC)
175 #else /* take care of overflow on 64 bits machines */
176 # define MAX_SEC_IN_JIFFIES \
177 (SH_DIV((MAX_JIFFY_OFFSET >> SEC_JIFFIE_SC) * TICK_NSEC, NSEC_PER_SEC, 1) - 1)
179 #endif
182 * Convert jiffies to milliseconds and back.
184 * Avoid unnecessary multiplications/divisions in the
185 * two most common HZ cases:
187 static inline unsigned int jiffies_to_msecs(const unsigned long j)
189 #if HZ <= 1000 && !(1000 % HZ)
190 return (1000 / HZ) * j;
191 #elif HZ > 1000 && !(HZ % 1000)
192 return (j + (HZ / 1000) - 1)/(HZ / 1000);
193 #else
194 return (j * 1000) / HZ;
195 #endif
198 static inline unsigned int jiffies_to_usecs(const unsigned long j)
200 #if HZ <= 1000 && !(1000 % HZ)
201 return (1000000 / HZ) * j;
202 #elif HZ > 1000 && !(HZ % 1000)
203 return (j*1000 + (HZ - 1000))/(HZ / 1000);
204 #else
205 return (j * 1000000) / HZ;
206 #endif
209 static inline unsigned long msecs_to_jiffies(const unsigned int m)
211 if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
212 return MAX_JIFFY_OFFSET;
213 #if HZ <= 1000 && !(1000 % HZ)
214 return (m + (1000 / HZ) - 1) / (1000 / HZ);
215 #elif HZ > 1000 && !(HZ % 1000)
216 return m * (HZ / 1000);
217 #else
218 return (m * HZ + 999) / 1000;
219 #endif
223 * The TICK_NSEC - 1 rounds up the value to the next resolution. Note
224 * that a remainder subtract here would not do the right thing as the
225 * resolution values don't fall on second boundries. I.e. the line:
226 * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
228 * Rather, we just shift the bits off the right.
230 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
231 * value to a scaled second value.
233 static __inline__ unsigned long
234 timespec_to_jiffies(const struct timespec *value)
236 unsigned long sec = value->tv_sec;
237 long nsec = value->tv_nsec + TICK_NSEC - 1;
239 if (sec >= MAX_SEC_IN_JIFFIES){
240 sec = MAX_SEC_IN_JIFFIES;
241 nsec = 0;
243 return (((u64)sec * SEC_CONVERSION) +
244 (((u64)nsec * NSEC_CONVERSION) >>
245 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
249 static __inline__ void
250 jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
253 * Convert jiffies to nanoseconds and separate with
254 * one divide.
256 u64 nsec = (u64)jiffies * TICK_NSEC;
257 value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_nsec);
260 /* Same for "timeval"
262 * Well, almost. The problem here is that the real system resolution is
263 * in nanoseconds and the value being converted is in micro seconds.
264 * Also for some machines (those that use HZ = 1024, in-particular),
265 * there is a LARGE error in the tick size in microseconds.
267 * The solution we use is to do the rounding AFTER we convert the
268 * microsecond part. Thus the USEC_ROUND, the bits to be shifted off.
269 * Instruction wise, this should cost only an additional add with carry
270 * instruction above the way it was done above.
272 static __inline__ unsigned long
273 timeval_to_jiffies(const struct timeval *value)
275 unsigned long sec = value->tv_sec;
276 long usec = value->tv_usec;
278 if (sec >= MAX_SEC_IN_JIFFIES){
279 sec = MAX_SEC_IN_JIFFIES;
280 usec = 0;
282 return (((u64)sec * SEC_CONVERSION) +
283 (((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
284 (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
287 static __inline__ void
288 jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
291 * Convert jiffies to nanoseconds and separate with
292 * one divide.
294 u64 nsec = (u64)jiffies * TICK_NSEC;
295 value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_usec);
296 value->tv_usec /= NSEC_PER_USEC;
299 static __inline__ int timespec_equal(struct timespec *a, struct timespec *b)
301 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
304 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
305 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
306 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
308 * [For the Julian calendar (which was used in Russia before 1917,
309 * Britain & colonies before 1752, anywhere else before 1582,
310 * and is still in use by some communities) leave out the
311 * -year/100+year/400 terms, and add 10.]
313 * This algorithm was first published by Gauss (I think).
315 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
316 * machines were long is 32-bit! (However, as time_t is signed, we
317 * will already get problems at other places on 2038-01-19 03:14:08)
319 static inline unsigned long
320 mktime (unsigned int year, unsigned int mon,
321 unsigned int day, unsigned int hour,
322 unsigned int min, unsigned int sec)
324 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
325 mon += 12; /* Puts Feb last since it has leap day */
326 year -= 1;
329 return (((
330 (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
331 year*365 - 719499
332 )*24 + hour /* now have hours */
333 )*60 + min /* now have minutes */
334 )*60 + sec; /* finally seconds */
337 extern struct timespec xtime;
338 extern struct timespec wall_to_monotonic;
339 extern seqlock_t xtime_lock;
341 static inline unsigned long get_seconds(void)
343 return xtime.tv_sec;
346 struct timespec current_kernel_time(void);
348 #define CURRENT_TIME (current_kernel_time())
349 #define CURRENT_TIME_SEC ((struct timespec) { xtime.tv_sec, 0 })//johnson add for new jffs2
351 #endif /* __KERNEL__ */
353 #define NFDBITS __NFDBITS
355 #ifdef __KERNEL__
356 extern void do_gettimeofday(struct timeval *tv);
357 extern int do_settimeofday(struct timespec *tv);
358 extern int do_sys_settimeofday(struct timespec *tv, struct timezone *tz);
359 extern void clock_was_set(void); // call when ever the clock is set
360 extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
361 extern long do_nanosleep(struct timespec *t);
362 extern long do_utimes(char __user * filename, struct timeval * times);
363 struct itimerval;
364 extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);
365 extern int do_getitimer(int which, struct itimerval *value);
366 extern void getnstimeofday (struct timespec *tv);
368 static inline void
369 set_normalized_timespec (struct timespec *ts, time_t sec, long nsec)
371 while (nsec > NSEC_PER_SEC) {
372 nsec -= NSEC_PER_SEC;
373 ++sec;
375 while (nsec < 0) {
376 nsec += NSEC_PER_SEC;
377 --sec;
379 ts->tv_sec = sec;
380 ts->tv_nsec = nsec;
382 #endif
384 #define FD_SETSIZE __FD_SETSIZE
385 #define FD_SET(fd,fdsetp) __FD_SET(fd,fdsetp)
386 #define FD_CLR(fd,fdsetp) __FD_CLR(fd,fdsetp)
387 #define FD_ISSET(fd,fdsetp) __FD_ISSET(fd,fdsetp)
388 #define FD_ZERO(fdsetp) __FD_ZERO(fdsetp)
391 * Names of the interval timers, and structure
392 * defining a timer setting.
394 #define ITIMER_REAL 0
395 #define ITIMER_VIRTUAL 1
396 #define ITIMER_PROF 2
398 struct itimerspec {
399 struct timespec it_interval; /* timer period */
400 struct timespec it_value; /* timer expiration */
403 struct itimerval {
404 struct timeval it_interval; /* timer interval */
405 struct timeval it_value; /* current value */
410 * The IDs of the various system clocks (for POSIX.1b interval timers).
412 #define CLOCK_REALTIME 0
413 #define CLOCK_MONOTONIC 1
414 #define CLOCK_PROCESS_CPUTIME_ID 2
415 #define CLOCK_THREAD_CPUTIME_ID 3
416 #define CLOCK_REALTIME_HR 4
417 #define CLOCK_MONOTONIC_HR 5
419 #define MAX_CLOCKS 6
420 #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC | \
421 CLOCK_REALTIME_HR | CLOCK_MONOTONIC_HR)
422 #define CLOCKS_MONO (CLOCK_MONOTONIC & CLOCK_MONOTONIC_HR)
425 * The various flags for setting POSIX.1b interval timers.
428 #define TIMER_ABSTIME 0x01
431 #endif