delay.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_DELAY_H
  3. #define _LINUX_DELAY_H
  4. /*
  5. * Copyright (C) 1993 Linus Torvalds
  6. *
  7. * Delay routines, using a pre-computed "loops_per_jiffy" value.
  8. *
  9. * Please note that ndelay(), udelay() and mdelay() may return early for
  10. * several reasons:
  11. * 1. computed loops_per_jiffy too low (due to the time taken to
  12. * execute the timer interrupt.)
  13. * 2. cache behaviour affecting the time it takes to execute the
  14. * loop function.
  15. * 3. CPU clock rate changes.
  16. *
  17. * Please see this thread:
  18. * https://lists.openwall.net/linux-kernel/2011/01/09/56
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. extern unsigned long loops_per_jiffy;
  23. #include <asm/delay.h>
  24. /*
  25. * Using udelay() for intervals greater than a few milliseconds can
  26. * risk overflow for high loops_per_jiffy (high bogomips) machines. The
  27. * mdelay() provides a wrapper to prevent this. For delays greater
  28. * than MAX_UDELAY_MS milliseconds, the wrapper is used. Architecture
  29. * specific values can be defined in asm-???/delay.h as an override.
  30. * The 2nd mdelay() definition ensures GCC will optimize away the
  31. * while loop for the common cases where n <= MAX_UDELAY_MS -- Paul G.
  32. */
  33. #ifndef MAX_UDELAY_MS
  34. #define MAX_UDELAY_MS 5
  35. #endif
  36. #ifndef mdelay
  37. #define mdelay(n) (\
  38. (__builtin_constant_p(n) && (n)<=MAX_UDELAY_MS) ? udelay((n)*1000) : \
  39. ({unsigned long __ms=(n); while (__ms--) udelay(1000);}))
  40. #endif
  41. #ifndef ndelay
  42. static inline void ndelay(unsigned long x)
  43. {
  44. udelay(DIV_ROUND_UP(x, 1000));
  45. }
  46. #define ndelay(x) ndelay(x)
  47. #endif
  48. extern unsigned long lpj_fine;
  49. void calibrate_delay(void);
  50. void __attribute__((weak)) calibration_delay_done(void);
  51. void msleep(unsigned int msecs);
  52. unsigned long msleep_interruptible(unsigned int msecs);
  53. void usleep_range_state(unsigned long min, unsigned long max,
  54. unsigned int state);
  55. void usleep_range(unsigned long min, unsigned long max);
  56. static inline void usleep_idle_range(unsigned long min, unsigned long max)
  57. {
  58. usleep_range_state(min, max, TASK_IDLE);
  59. }
  60. static inline void ssleep(unsigned int seconds)
  61. {
  62. msleep(seconds * 1000);
  63. }
  64. /* see Documentation/timers/timers-howto.rst for the thresholds */
  65. static inline void fsleep(unsigned long usecs)
  66. {
  67. if (usecs <= 10)
  68. udelay(usecs);
  69. else if (usecs <= 20000)
  70. usleep_range(usecs, 2 * usecs);
  71. else
  72. msleep(DIV_ROUND_UP(usecs, 1000));
  73. }
  74. #endif /* defined(_LINUX_DELAY_H) */