iopoll.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #ifndef _LINUX_IOPOLL_H
  15. #define _LINUX_IOPOLL_H
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/hrtimer.h>
  19. #include <linux/delay.h>
  20. #include <linux/errno.h>
  21. #include <linux/io.h>
  22. /**
  23. * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
  24. * @op: accessor function (takes @addr as its only argument)
  25. * @addr: Address to poll
  26. * @val: Variable to read the value into
  27. * @cond: Break condition (usually involving @val)
  28. * @sleep_us: Maximum time to sleep between reads in us (0
  29. * tight-loops). Should be less than ~20ms since usleep_range
  30. * is used (see Documentation/timers/timers-howto.txt).
  31. * @timeout_us: Timeout in us, 0 means never timeout
  32. *
  33. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  34. * case, the last read value at @addr is stored in @val. Must not
  35. * be called from atomic context if sleep_us or timeout_us are used.
  36. *
  37. * When available, you'll probably want to use one of the specialized
  38. * macros defined below rather than this macro directly.
  39. */
  40. #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
  41. ({ \
  42. ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \
  43. might_sleep_if(sleep_us); \
  44. for (;;) { \
  45. (val) = op(addr); \
  46. if (cond) \
  47. break; \
  48. if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
  49. (val) = op(addr); \
  50. break; \
  51. } \
  52. if (sleep_us) \
  53. usleep_range((sleep_us >> 2) + 1, sleep_us); \
  54. } \
  55. (cond) ? 0 : -ETIMEDOUT; \
  56. })
  57. /**
  58. * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
  59. * @op: accessor function (takes @addr as its only argument)
  60. * @addr: Address to poll
  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.txt).
  66. * @timeout_us: Timeout in us, 0 means never timeout
  67. *
  68. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  69. * case, the last read value at @addr is stored in @val.
  70. *
  71. * When available, you'll probably want to use one of the specialized
  72. * macros defined below rather than this macro directly.
  73. */
  74. #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
  75. ({ \
  76. ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \
  77. for (;;) { \
  78. (val) = op(addr); \
  79. if (cond) \
  80. break; \
  81. if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
  82. (val) = op(addr); \
  83. break; \
  84. } \
  85. if (delay_us) \
  86. udelay(delay_us); \
  87. } \
  88. (cond) ? 0 : -ETIMEDOUT; \
  89. })
  90. #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  91. readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
  92. #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  93. readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
  94. #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  95. readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
  96. #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  97. readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
  98. #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  99. readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
  100. #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  101. readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
  102. #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  103. readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
  104. #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  105. readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
  106. #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  107. readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
  108. #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  109. readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
  110. #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  111. readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
  112. #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  113. readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
  114. #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  115. readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
  116. #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  117. readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
  118. #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
  119. readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
  120. #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
  121. readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
  122. #endif /* _LINUX_IOPOLL_H */