iopoll.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. cpu_relax(); \
  54. } \
  55. (cond) ? 0 : -ETIMEDOUT; \
  56. })
  57. /**
  58. * read_poll_timeout_atomic - Periodically poll an address until a condition is
  59. * met or a timeout occurs
  60. * @op: accessor function (takes @args as its arguments)
  61. * @val: Variable to read the value into
  62. * @cond: Break condition (usually involving @val)
  63. * @delay_us: Time to udelay between reads in us (0 tight-loops). Should
  64. * be less than ~10us since udelay is used (see
  65. * Documentation/timers/timers-howto.rst).
  66. * @timeout_us: Timeout in us, 0 means never timeout
  67. * @delay_before_read: if it is true, delay @delay_us before read.
  68. * @args: arguments for @op poll
  69. *
  70. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  71. * case, the last read value at @args is stored in @val.
  72. *
  73. * When available, you'll probably want to use one of the specialized
  74. * macros defined below rather than this macro directly.
  75. */
  76. #define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
  77. delay_before_read, args...) \
  78. ({ \
  79. u64 __timeout_us = (timeout_us); \
  80. unsigned long __delay_us = (delay_us); \
  81. ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
  82. if (delay_before_read && __delay_us) \
  83. udelay(__delay_us); \
  84. for (;;) { \
  85. (val) = op(args); \
  86. if (cond) \
  87. break; \
  88. if (__timeout_us && \
  89. ktime_compare(ktime_get(), __timeout) > 0) { \
  90. (val) = op(args); \
  91. break; \
  92. } \
  93. if (__delay_us) \
  94. udelay(__delay_us); \
  95. cpu_relax(); \
  96. } \
  97. (cond) ? 0 : -ETIMEDOUT; \
  98. })
  99. /**
  100. * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
  101. * @op: accessor function (takes @addr as its only argument)
  102. * @addr: Address to poll
  103. * @val: Variable to read the value into
  104. * @cond: Break condition (usually involving @val)
  105. * @sleep_us: Maximum time to sleep between reads in us (0
  106. * tight-loops). Should be less than ~20ms since usleep_range
  107. * is used (see Documentation/timers/timers-howto.rst).
  108. * @timeout_us: Timeout in us, 0 means never timeout
  109. *
  110. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  111. * case, the last read value at @addr is stored in @val. Must not
  112. * be called from atomic context if sleep_us or timeout_us are used.
  113. *
  114. * When available, you'll probably want to use one of the specialized
  115. * macros defined below rather than this macro directly.
  116. */
  117. #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
  118. read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
  119. /**
  120. * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
  121. * @op: accessor function (takes @addr as its only argument)
  122. * @addr: Address to poll
  123. * @val: Variable to read the value into
  124. * @cond: Break condition (usually involving @val)
  125. * @delay_us: Time to udelay between reads in us (0 tight-loops). Should
  126. * be less than ~10us since udelay is used (see
  127. * Documentation/timers/timers-howto.rst).
  128. * @timeout_us: Timeout in us, 0 means never timeout
  129. *
  130. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  131. * case, the last read value at @addr is stored in @val.
  132. *
  133. * When available, you'll probably want to use one of the specialized
  134. * macros defined below rather than this macro directly.
  135. */
  136. #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
  137. read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
  138. #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  139. readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
  140. #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  141. readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
  142. #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  143. readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
  144. #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  145. readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
  146. #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  147. readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
  148. #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  149. readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
  150. #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  151. readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
  152. #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  153. readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
  154. #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  155. readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
  156. #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  157. readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
  158. #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  159. readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
  160. #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  161. readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
  162. #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  163. readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
  164. #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  165. readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
  166. #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  167. readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
  168. #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  169. readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
  170. #endif /* _LINUX_IOPOLL_H */