1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_DELAY_H 3 #define _LINUX_DELAY_H 4 5 /* 6 * Copyright (C) 1993 Linus Torvalds 7 * 8 * Delay routines, using a pre-computed "loops_per_jiffy" value. 9 * Sleep routines using timer list timers or hrtimers. 10 */ 11 12 #include <linux/math.h> 13 #include <linux/sched.h> 14 15 extern unsigned long loops_per_jiffy; 16 17 #include <asm/delay.h> 18 19 /* 20 * Using udelay() for intervals greater than a few milliseconds can 21 * risk overflow for high loops_per_jiffy (high bogomips) machines. The 22 * mdelay() provides a wrapper to prevent this. For delays greater 23 * than MAX_UDELAY_MS milliseconds, the wrapper is used. Architecture 24 * specific values can be defined in asm-???/delay.h as an override. 25 * The 2nd mdelay() definition ensures GCC will optimize away the 26 * while loop for the common cases where n <= MAX_UDELAY_MS -- Paul G. 27 */ 28 #ifndef MAX_UDELAY_MS 29 #define MAX_UDELAY_MS 5 30 #endif 31 32 #ifndef mdelay 33 /** 34 * mdelay - Inserting a delay based on milliseconds with busy waiting 35 * @n: requested delay in milliseconds 36 * 37 * See udelay() for basic information about mdelay() and it's variants. 38 * 39 * Please double check, whether mdelay() is the right way to go or whether a 40 * refactoring of the code is the better variant to be able to use msleep() 41 * instead. 42 */ 43 #define mdelay(n) (\ 44 (__builtin_constant_p(n) && (n)<=MAX_UDELAY_MS) ? udelay((n)*1000) : \ 45 ({unsigned long __ms=(n); while (__ms--) udelay(1000);})) 46 #endif 47 48 #ifndef ndelay 49 static inline void ndelay(unsigned long x) 50 { 51 udelay(DIV_ROUND_UP(x, 1000)); 52 } 53 #define ndelay(x) ndelay(x) 54 #endif 55 56 extern unsigned long lpj_fine; 57 void calibrate_delay(void); 58 unsigned long calibrate_delay_is_known(void); 59 void __attribute__((weak)) calibration_delay_done(void); 60 void msleep(unsigned int msecs); 61 unsigned long msleep_interruptible(unsigned int msecs); 62 void usleep_range_state(unsigned long min, unsigned long max, 63 unsigned int state); 64 65 /** 66 * usleep_range - Sleep for an approximate time 67 * @min: Minimum time in microseconds to sleep 68 * @max: Maximum time in microseconds to sleep 69 * 70 * For basic information please refere to usleep_range_state(). 71 * 72 * The task will be in the state TASK_UNINTERRUPTIBLE during the sleep. 73 */ 74 static inline void usleep_range(unsigned long min, unsigned long max) 75 { 76 usleep_range_state(min, max, TASK_UNINTERRUPTIBLE); 77 } 78 79 /** 80 * usleep_range_idle - Sleep for an approximate time with idle time accounting 81 * @min: Minimum time in microseconds to sleep 82 * @max: Maximum time in microseconds to sleep 83 * 84 * For basic information please refere to usleep_range_state(). 85 * 86 * The sleeping task has the state TASK_IDLE during the sleep to prevent 87 * contribution to the load avarage. 88 */ 89 static inline void usleep_range_idle(unsigned long min, unsigned long max) 90 { 91 usleep_range_state(min, max, TASK_IDLE); 92 } 93 94 /** 95 * ssleep - wrapper for seconds around msleep 96 * @seconds: Requested sleep duration in seconds 97 * 98 * Please refere to msleep() for detailed information. 99 */ 100 static inline void ssleep(unsigned int seconds) 101 { 102 msleep(seconds * 1000); 103 } 104 105 /* see Documentation/timers/timers-howto.rst for the thresholds */ 106 static inline void fsleep(unsigned long usecs) 107 { 108 if (usecs <= 10) 109 udelay(usecs); 110 else if (usecs <= 20000) 111 usleep_range(usecs, 2 * usecs); 112 else 113 msleep(DIV_ROUND_UP(usecs, 1000)); 114 } 115 116 #endif /* defined(_LINUX_DELAY_H) */ 117