iopoll.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. */
  6. #ifndef _LINUX_IOPOLL_H
  7. #define _LINUX_IOPOLL_H
  8. #include <linux/errno.h>
  9. #include <linux/io.h>
  10. #include <time.h>
  11. /**
  12. * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
  13. * @op: accessor function (takes @addr as its only argument)
  14. * @addr: Address to poll
  15. * @val: Variable to read the value into
  16. * @cond: Break condition (usually involving @val)
  17. * @timeout_us: Timeout in us, 0 means never timeout
  18. *
  19. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  20. * case, the last read value at @addr is stored in @val.
  21. *
  22. * When available, you'll probably want to use one of the specialized
  23. * macros defined below rather than this macro directly.
  24. */
  25. #define readx_poll_timeout(op, addr, val, cond, timeout_us) \
  26. ({ \
  27. unsigned long timeout = timer_get_us() + timeout_us; \
  28. for (;;) { \
  29. (val) = op(addr); \
  30. if (cond) \
  31. break; \
  32. if (timeout_us && time_after(timer_get_us(), timeout)) { \
  33. (val) = op(addr); \
  34. break; \
  35. } \
  36. } \
  37. (cond) ? 0 : -ETIMEDOUT; \
  38. })
  39. #define readb_poll_timeout(addr, val, cond, timeout_us) \
  40. readx_poll_timeout(readb, addr, val, cond, timeout_us)
  41. #define readw_poll_timeout(addr, val, cond, timeout_us) \
  42. readx_poll_timeout(readw, addr, val, cond, timeout_us)
  43. #define readl_poll_timeout(addr, val, cond, timeout_us) \
  44. readx_poll_timeout(readl, addr, val, cond, timeout_us)
  45. #define readq_poll_timeout(addr, val, cond, timeout_us) \
  46. readx_poll_timeout(readq, addr, val, cond, timeout_us)
  47. #define readb_relaxed_poll_timeout(addr, val, cond, timeout_us) \
  48. readx_poll_timeout(readb_relaxed, addr, val, cond, timeout_us)
  49. #define readw_relaxed_poll_timeout(addr, val, cond, timeout_us) \
  50. readx_poll_timeout(readw_relaxed, addr, val, cond, timeout_us)
  51. #define readl_relaxed_poll_timeout(addr, val, cond, timeout_us) \
  52. readx_poll_timeout(readl_relaxed, addr, val, cond, timeout_us)
  53. #define readq_relaxed_poll_timeout(addr, val, cond, timeout_us) \
  54. readx_poll_timeout(readq_relaxed, addr, val, cond, timeout_us)
  55. #endif /* _LINUX_IOPOLL_H */