delay.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_GENERIC_DELAY_H
  3. #define __ASM_GENERIC_DELAY_H
  4. #include <linux/math.h>
  5. #include <vdso/time64.h>
  6. /* Undefined functions to get compile-time errors */
  7. extern void __bad_udelay(void);
  8. extern void __bad_ndelay(void);
  9. extern void __udelay(unsigned long usecs);
  10. extern void __ndelay(unsigned long nsecs);
  11. extern void __const_udelay(unsigned long xloops);
  12. extern void __delay(unsigned long loops);
  13. /*
  14. * The microseconds/nanosecond delay multiplicators are used to convert a
  15. * constant microseconds/nanoseconds value to a value which can be used by the
  16. * architectures specific implementation to transform it into loops.
  17. */
  18. #define UDELAY_CONST_MULT ((unsigned long)DIV_ROUND_UP(1ULL << 32, USEC_PER_SEC))
  19. #define NDELAY_CONST_MULT ((unsigned long)DIV_ROUND_UP(1ULL << 32, NSEC_PER_SEC))
  20. /*
  21. * The maximum constant udelay/ndelay value picked out of thin air to prevent
  22. * too long constant udelays/ndelays.
  23. */
  24. #define DELAY_CONST_MAX 20000
  25. /**
  26. * udelay - Inserting a delay based on microseconds with busy waiting
  27. * @usec: requested delay in microseconds
  28. *
  29. * When delaying in an atomic context ndelay(), udelay() and mdelay() are the
  30. * only valid variants of delaying/sleeping to go with.
  31. *
  32. * When inserting delays in non atomic context which are shorter than the time
  33. * which is required to queue e.g. an hrtimer and to enter then the scheduler,
  34. * it is also valuable to use udelay(). But it is not simple to specify a
  35. * generic threshold for this which will fit for all systems. An approximation
  36. * is a threshold for all delays up to 10 microseconds.
  37. *
  38. * When having a delay which is larger than the architecture specific
  39. * %MAX_UDELAY_MS value, please make sure mdelay() is used. Otherwise a overflow
  40. * risk is given.
  41. *
  42. * Please note that ndelay(), udelay() and mdelay() may return early for several
  43. * reasons (https://lists.openwall.net/linux-kernel/2011/01/09/56):
  44. *
  45. * #. computed loops_per_jiffy too low (due to the time taken to execute the
  46. * timer interrupt.)
  47. * #. cache behaviour affecting the time it takes to execute the loop function.
  48. * #. CPU clock rate changes.
  49. */
  50. static __always_inline void udelay(unsigned long usec)
  51. {
  52. if (__builtin_constant_p(usec)) {
  53. if (usec >= DELAY_CONST_MAX)
  54. __bad_udelay();
  55. else
  56. __const_udelay(usec * UDELAY_CONST_MULT);
  57. } else {
  58. __udelay(usec);
  59. }
  60. }
  61. /**
  62. * ndelay - Inserting a delay based on nanoseconds with busy waiting
  63. * @nsec: requested delay in nanoseconds
  64. *
  65. * See udelay() for basic information about ndelay() and it's variants.
  66. */
  67. static __always_inline void ndelay(unsigned long nsec)
  68. {
  69. if (__builtin_constant_p(nsec)) {
  70. if (nsec >= DELAY_CONST_MAX)
  71. __bad_ndelay();
  72. else
  73. __const_udelay(nsec * NDELAY_CONST_MULT);
  74. } else {
  75. __ndelay(nsec);
  76. }
  77. }
  78. #define ndelay(x) ndelay(x)
  79. #endif /* __ASM_GENERIC_DELAY_H */