1 #ifndef _LINUX_DELAY_H
2 #define _LINUX_DELAY_H
3 
4 /*
5  * Copyright (C) 1993 Linus Torvalds
6  *
7  * Delay routines, using a pre-computed "loops_per_jiffy" value.
8  */
9 
10 extern unsigned long loops_per_jiffy;
11 
12 #include <linux/time.h>
13 #include <linux/sched.h>
14 #include <asm/delay.h>
15 
16 /*
17  * We define MAX_MSEC_OFFSET as the maximal value that can be accepted by
18  * msecs_to_jiffies() without risking a multiply overflow. This function
19  * returns MAX_JIFFY_OFFSET for arguments above those values.
20  */
21 
22 #if HZ <= 1000 && !(1000 % HZ)
23 #  define MAX_MSEC_OFFSET \
24 	(ULONG_MAX - (1000 / HZ) + 1)
25 #elif HZ > 1000 && !(HZ % 1000)
26 #  define MAX_MSEC_OFFSET \
27 	(ULONG_MAX / (HZ / 1000))
28 #else
29 #  define MAX_MSEC_OFFSET \
30 	((ULONG_MAX - 999) / HZ)
31 #endif
32 
33 
34 /*
35  * Convert jiffies to milliseconds and back.
36  *
37  * Avoid unnecessary multiplications/divisions in the
38  * two most common HZ cases:
39  */
jiffies_to_msecs(const unsigned long j)40 static inline unsigned int jiffies_to_msecs(const unsigned long j)
41 {
42 #if HZ <= 1000 && !(1000 % HZ)
43 	return (1000 / HZ) * j;
44 #elif HZ > 1000 && !(HZ % 1000)
45 	return (j + (HZ / 1000) - 1)/(HZ / 1000);
46 #else
47 	return (j * 1000) / HZ;
48 #endif
49 }
50 
jiffies_to_usecs(const unsigned long j)51 static inline unsigned int jiffies_to_usecs(const unsigned long j)
52 {
53 #if HZ <= 1000 && !(1000 % HZ)
54 	return (1000000 / HZ) * j;
55 #elif HZ > 1000 && !(HZ % 1000)
56 	return (j*1000 + (HZ - 1000))/(HZ / 1000);
57 #else
58 	return (j * 1000000) / HZ;
59 #endif
60 }
61 
msecs_to_jiffies(const unsigned int m)62 static inline unsigned long msecs_to_jiffies(const unsigned int m)
63 {
64 	if (MAX_MSEC_OFFSET < UINT_MAX && m > (unsigned int)MAX_MSEC_OFFSET)
65 		return MAX_JIFFY_OFFSET;
66 #if HZ <= 1000 && !(1000 % HZ)
67 	return ((unsigned long)m + (1000 / HZ) - 1) / (1000 / HZ);
68 #elif HZ > 1000 && !(HZ % 1000)
69 	return (unsigned long)m * (HZ / 1000);
70 #else
71 	return ((unsigned long)m * HZ + 999) / 1000;
72 #endif
73 }
74 
msleep(unsigned long msecs)75 static inline void msleep(unsigned long msecs)
76 {
77         set_current_state(TASK_UNINTERRUPTIBLE);
78         schedule_timeout(msecs_to_jiffies(msecs) + 1);
79 }
80 
ssleep(unsigned long secs)81 static inline void ssleep(unsigned long secs)
82 {
83         set_current_state(TASK_UNINTERRUPTIBLE);
84         schedule_timeout((HZ * secs) + 1);
85 }
86 
87 /*
88  * Using udelay() for intervals greater than a few milliseconds can
89  * risk overflow for high loops_per_jiffy (high bogomips) machines. The
90  * mdelay() provides a wrapper to prevent this.  For delays greater
91  * than MAX_UDELAY_MS milliseconds, the wrapper is used.  Architecture
92  * specific values can be defined in asm-???/delay.h as an override.
93  * The 2nd mdelay() definition ensures GCC will optimize away the
94  * while loop for the common cases where n <= MAX_UDELAY_MS  --  Paul G.
95  */
96 
97 #ifndef MAX_UDELAY_MS
98 #define MAX_UDELAY_MS	5
99 #endif
100 
101 #ifdef notdef
102 #define mdelay(n) (\
103 	{unsigned long msec=(n); while (msec--) udelay(1000);})
104 #else
105 #define mdelay(n) (\
106 	(__builtin_constant_p(n) && (n)<=MAX_UDELAY_MS) ? udelay((n)*1000) : \
107 	({unsigned long msec=(n); while (msec--) udelay(1000);}))
108 #endif
109 
110 #endif /* defined(_LINUX_DELAY_H) */
111