1#ifndef _LINUX_TIME_H
2#define _LINUX_TIME_H
3
4#include <asm/param.h>
5#include <linux/types.h>
6
7#ifndef _STRUCT_TIMESPEC
8#define _STRUCT_TIMESPEC
9struct timespec {
10 time_t tv_sec;
11 long tv_nsec;
12};
13#endif
14
15struct timeval {
16 time_t tv_sec;
17 suseconds_t tv_usec;
18};
19
20struct timezone {
21 int tz_minuteswest;
22 int tz_dsttime;
23};
24
25#ifdef __KERNEL__
26
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
32
33#define div_long_long_rem(dividend,divisor,remainder) ({ \
34 u64 result = dividend; \
35 *remainder = do_div(result,divisor); \
36 result; })
37
38#endif
39
40
41
42
43
44#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
45
46
47
48
49
50
51
52
53
54
55
56
57
58#define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
59
60
61#ifndef USEC_PER_SEC
62#define USEC_PER_SEC (1000000L)
63#endif
64
65#ifndef NSEC_PER_SEC
66#define NSEC_PER_SEC (1000000000L)
67#endif
68
69#ifndef NSEC_PER_USEC
70#define NSEC_PER_USEC (1000L)
71#endif
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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))
153
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))
159
160
161
162
163
164
165
166#define USEC_ROUND (u64)(((u64)1 << USEC_JIFFIE_SC) - 1)
167
168
169
170
171
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
176# define MAX_SEC_IN_JIFFIES \
177 (SH_DIV((MAX_JIFFY_OFFSET >> SEC_JIFFIE_SC) * TICK_NSEC, NSEC_PER_SEC, 1) - 1)
178
179#endif
180
181
182
183
184
185
186
187static inline unsigned int jiffies_to_msecs(const unsigned long j)
188{
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
196}
197static inline unsigned long msecs_to_jiffies(const unsigned int m)
198{
199#if HZ <= 1000 && !(1000 % HZ)
200 return (m + (1000 / HZ) - 1) / (1000 / HZ);
201#elif HZ > 1000 && !(HZ % 1000)
202 return m * (HZ / 1000);
203#else
204 return (m * HZ + 999) / 1000;
205#endif
206}
207
208
209
210
211
212
213
214
215
216
217
218
219static __inline__ unsigned long
220timespec_to_jiffies(const struct timespec *value)
221{
222 unsigned long sec = value->tv_sec;
223 long nsec = value->tv_nsec + TICK_NSEC - 1;
224
225 if (sec >= MAX_SEC_IN_JIFFIES){
226 sec = MAX_SEC_IN_JIFFIES;
227 nsec = 0;
228 }
229 return (((u64)sec * SEC_CONVERSION) +
230 (((u64)nsec * NSEC_CONVERSION) >>
231 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
232
233}
234
235static __inline__ void
236jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
237{
238
239
240
241
242 u64 nsec = (u64)jiffies * TICK_NSEC;
243 value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_nsec);
244}
245
246
247
248
249
250
251
252
253
254
255
256
257
258static __inline__ unsigned long
259timeval_to_jiffies(const struct timeval *value)
260{
261 unsigned long sec = value->tv_sec;
262 long usec = value->tv_usec;
263
264 if (sec >= MAX_SEC_IN_JIFFIES){
265 sec = MAX_SEC_IN_JIFFIES;
266 usec = 0;
267 }
268 return (((u64)sec * SEC_CONVERSION) +
269 (((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
270 (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
271}
272
273static __inline__ void
274jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
275{
276
277
278
279
280 u64 nsec = (u64)jiffies * TICK_NSEC;
281 value->tv_sec = div_long_long_rem(nsec, NSEC_PER_SEC, &value->tv_usec);
282 value->tv_usec /= NSEC_PER_USEC;
283}
284
285static __inline__ int timespec_equal(struct timespec *a, struct timespec *b)
286{
287 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
288}
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305static inline unsigned long
306mktime (unsigned int year, unsigned int mon,
307 unsigned int day, unsigned int hour,
308 unsigned int min, unsigned int sec)
309{
310 if (0 >= (int) (mon -= 2)) {
311 mon += 12;
312 year -= 1;
313 }
314
315 return (((
316 (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
317 year*365 - 719499
318 )*24 + hour
319 )*60 + min
320 )*60 + sec;
321}
322
323extern struct timespec xtime;
324extern struct timespec wall_to_monotonic;
325extern seqlock_t xtime_lock;
326
327static inline unsigned long get_seconds(void)
328{
329 return xtime.tv_sec;
330}
331
332struct timespec current_kernel_time(void);
333
334#define CURRENT_TIME (current_kernel_time())
335
336#endif
337
338#define NFDBITS __NFDBITS
339
340#ifdef __KERNEL__
341extern void do_gettimeofday(struct timeval *tv);
342extern int do_settimeofday(struct timespec *tv);
343extern int do_sys_settimeofday(struct timespec *tv, struct timezone *tz);
344extern void clock_was_set(void);
345extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
346extern long do_nanosleep(struct timespec *t);
347extern long do_utimes(char __user * filename, struct timeval * times);
348struct itimerval;
349extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);
350extern int do_getitimer(int which, struct itimerval *value);
351
352static inline void
353set_normalized_timespec (struct timespec *ts, time_t sec, long nsec)
354{
355 while (nsec > NSEC_PER_SEC) {
356 nsec -= NSEC_PER_SEC;
357 ++sec;
358 }
359 while (nsec < 0) {
360 nsec += NSEC_PER_SEC;
361 --sec;
362 }
363 ts->tv_sec = sec;
364 ts->tv_nsec = nsec;
365}
366#endif
367
368#define FD_SETSIZE __FD_SETSIZE
369#define FD_SET(fd,fdsetp) __FD_SET(fd,fdsetp)
370#define FD_CLR(fd,fdsetp) __FD_CLR(fd,fdsetp)
371#define FD_ISSET(fd,fdsetp) __FD_ISSET(fd,fdsetp)
372#define FD_ZERO(fdsetp) __FD_ZERO(fdsetp)
373
374
375
376
377
378#define ITIMER_REAL 0
379#define ITIMER_VIRTUAL 1
380#define ITIMER_PROF 2
381
382struct itimerspec {
383 struct timespec it_interval;
384 struct timespec it_value;
385};
386
387struct itimerval {
388 struct timeval it_interval;
389 struct timeval it_value;
390};
391
392
393
394
395
396#define CLOCK_REALTIME 0
397#define CLOCK_MONOTONIC 1
398#define CLOCK_PROCESS_CPUTIME_ID 2
399#define CLOCK_THREAD_CPUTIME_ID 3
400#define CLOCK_REALTIME_HR 4
401#define CLOCK_MONOTONIC_HR 5
402
403#define MAX_CLOCKS 6
404#define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC | \
405 CLOCK_REALTIME_HR | CLOCK_MONOTONIC_HR)
406#define CLOCKS_MONO (CLOCK_MONOTONIC & CLOCK_MONOTONIC_HR)
407
408
409
410
411
412#define TIMER_ABSTIME 0x01
413
414
415#endif
416