iopoll.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
  4. */
  5. #ifndef _LINUX_IOPOLL_H
  6. #define _LINUX_IOPOLL_H
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/ktime.h>
  10. #include <linux/delay.h>
  11. #include <linux/errno.h>
  12. #include <linux/io.h>
  13. /**
  14. * read_poll_timeout - Periodically poll an address until a condition is
  15. * met or a timeout occurs
  16. * @op: accessor function (takes @args as its arguments)
  17. * @val: Variable to read the value into
  18. * @cond: Break condition (usually involving @val)
  19. * @sleep_us: Maximum time to sleep between reads in us (0
  20. * tight-loops). Should be less than ~20ms since usleep_range
  21. * is used (see Documentation/timers/timers-howto.rst).
  22. * @timeout_us: Timeout in us, 0 means never timeout
  23. * @sleep_before_read: if it is true, sleep @sleep_us before read.
  24. * @args: arguments for @op poll
  25. *
  26. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  27. * case, the last read value at @args is stored in @val. Must not
  28. * be called from atomic context if sleep_us or timeout_us are used.
  29. *
  30. * When available, you'll probably want to use one of the specialized
  31. * macros defined below rather than this macro directly.
  32. */
  33. #define read_poll_timeout(op, val, cond, sleep_us, timeout_us, \
  34. sleep_before_read, args...) \
  35. ({ \
  36. u64 __timeout_us = (timeout_us); \
  37. unsigned long __sleep_us = (sleep_us); \
  38. ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
  39. might_sleep_if((__sleep_us) != 0); \
  40. if (sleep_before_read && __sleep_us) \
  41. usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
  42. for (;;) { \
  43. (val) = op(args); \
  44. if (cond) \
  45. break; \
  46. if (__timeout_us && \
  47. ktime_compare(ktime_get(), __timeout) > 0) { \
  48. (val) = op(args); \
  49. break; \
  50. } \
  51. if (__sleep_us) \
  52. usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
  53. } \
  54. (cond) ? 0 : -ETIMEDOUT; \
  55. })
  56. /**
  57. * read_poll_timeout_atomic - Periodically poll an address until a condition is
  58. * met or a timeout occurs
  59. * @op: accessor function (takes @args as its arguments)
  60. * @val: Variable to read the value into
  61. * @cond: Break condition (usually involving @val)
  62. * @delay_us: Time to udelay between reads in us (0 tight-loops). Should
  63. * be less than ~10us since udelay is used (see
  64. * Documentation/timers/timers-howto.rst).
  65. * @timeout_us: Timeout in us, 0 means never timeout
  66. * @delay_before_read: if it is true, delay @delay_us before read.
  67. * @args: arguments for @op poll
  68. *
  69. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  70. * case, the last read value at @args is stored in @val.
  71. *
  72. * When available, you'll probably want to use one of the specialized
  73. * macros defined below rather than this macro directly.
  74. */
  75. #define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
  76. delay_before_read, args...) \
  77. ({ \
  78. u64 __timeout_us = (timeout_us); \
  79. unsigned long __delay_us = (delay_us); \
  80. ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
  81. if (delay_before_read && __delay_us) \
  82. udelay(__delay_us); \
  83. for (;;) { \
  84. (val) = op(args); \
  85. if (cond) \
  86. break; \
  87. if (__timeout_us && \
  88. ktime_compare(ktime_get(), __timeout) > 0) { \
  89. (val) = op(args); \
  90. break; \
  91. } \
  92. if (__delay_us) \
  93. udelay(__delay_us); \
  94. } \
  95. (cond) ? 0 : -ETIMEDOUT; \
  96. })
  97. /**
  98. * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
  99. * @op: accessor function (takes @addr as its only argument)
  100. * @addr: Address to poll
  101. * @val: Variable to read the value into
  102. * @cond: Break condition (usually involving @val)
  103. * @sleep_us: Maximum time to sleep between reads in us (0
  104. * tight-loops). Should be less than ~20ms since usleep_range
  105. * is used (see Documentation/timers/timers-howto.rst).
  106. * @timeout_us: Timeout in us, 0 means never timeout
  107. *
  108. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  109. * case, the last read value at @addr is stored in @val. Must not
  110. * be called from atomic context if sleep_us or timeout_us are used.
  111. *
  112. * When available, you'll probably want to use one of the specialized
  113. * macros defined below rather than this macro directly.
  114. */
  115. #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
  116. read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
  117. /**
  118. * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
  119. * @op: accessor function (takes @addr as its only argument)
  120. * @addr: Address to poll
  121. * @val: Variable to read the value into
  122. * @cond: Break condition (usually involving @val)
  123. * @delay_us: Time to udelay between reads in us (0 tight-loops). Should
  124. * be less than ~10us since udelay is used (see
  125. * Documentation/timers/timers-howto.rst).
  126. * @timeout_us: Timeout in us, 0 means never timeout
  127. *
  128. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  129. * case, the last read value at @addr is stored in @val.
  130. *
  131. * When available, you'll probably want to use one of the specialized
  132. * macros defined below rather than this macro directly.
  133. */
  134. #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
  135. read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
  136. #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  137. readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
  138. #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  139. readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
  140. #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  141. readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
  142. #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  143. readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
  144. #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  145. readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
  146. #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  147. readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
  148. #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  149. readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
  150. #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  151. readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
  152. #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  153. readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
  154. #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  155. readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
  156. #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  157. readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
  158. #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  159. readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
  160. #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  161. readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
  162. #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  163. readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
  164. #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  165. readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
  166. #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  167. readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
  168. #endif /* _LINUX_IOPOLL_H */