blob: e22703785d089904365eebb4ce3765af1d4c2553 [file] [log] [blame]
developer263303b2022-08-05 15:15:42 +08001diff --git a/include/linux/iopoll.h b/include/linux/iopoll.h
2index 35e15df..2c8860e 100644
3--- a/include/linux/iopoll.h
4+++ b/include/linux/iopoll.h
5@@ -14,36 +14,41 @@
6 #include <linux/io.h>
7
8 /**
9- * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
10- * @op: accessor function (takes @addr as its only argument)
11- * @addr: Address to poll
12+ * read_poll_timeout - Periodically poll an address until a condition is
13+ * met or a timeout occurs
14+ * @op: accessor function (takes @args as its arguments)
15 * @val: Variable to read the value into
16 * @cond: Break condition (usually involving @val)
17 * @sleep_us: Maximum time to sleep between reads in us (0
18 * tight-loops). Should be less than ~20ms since usleep_range
19 * is used (see Documentation/timers/timers-howto.rst).
20 * @timeout_us: Timeout in us, 0 means never timeout
21+ * @sleep_before_read: if it is true, sleep @sleep_us before read.
22+ * @args: arguments for @op poll
23 *
24 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
25- * case, the last read value at @addr is stored in @val. Must not
26+ * case, the last read value at @args is stored in @val. Must not
27 * be called from atomic context if sleep_us or timeout_us are used.
28 *
29 * When available, you'll probably want to use one of the specialized
30 * macros defined below rather than this macro directly.
31 */
32-#define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
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(addr); \
44+ (val) = op(args); \
45 if (cond) \
46 break; \
47 if (__timeout_us && \
48 ktime_compare(ktime_get(), __timeout) > 0) { \
49- (val) = op(addr); \
50+ (val) = op(args); \
51 break; \
52 } \
53 if (__sleep_us) \
54@@ -53,42 +58,87 @@
55 })
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+ * read_poll_timeout_atomic - Periodically poll an address until a condition is
62+ * met or a timeout occurs
63+ * @op: accessor function (takes @args as its arguments)
64 * @val: Variable to read the value into
65 * @cond: Break condition (usually involving @val)
66 * @delay_us: Time to udelay between reads in us (0 tight-loops). Should
67 * be less than ~10us since udelay is used (see
68 * Documentation/timers/timers-howto.rst).
69 * @timeout_us: Timeout in us, 0 means never timeout
70+ * @delay_before_read: if it is true, delay @delay_us before read.
71+ * @args: arguments for @op poll
72 *
73 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
74- * case, the last read value at @addr is stored in @val.
75+ * case, the last read value at @args is stored in @val.
76 *
77 * When available, you'll probably want to use one of the specialized
78 * macros defined below rather than this macro directly.
79 */
80-#define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
81+#define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
82+ delay_before_read, args...) \
83 ({ \
84 u64 __timeout_us = (timeout_us); \
85 unsigned long __delay_us = (delay_us); \
86 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
87+ if (delay_before_read && __delay_us) \
88+ udelay(__delay_us); \
89 for (;;) { \
90- (val) = op(addr); \
91+ (val) = op(args); \
92 if (cond) \
93 break; \
94 if (__timeout_us && \
95 ktime_compare(ktime_get(), __timeout) > 0) { \
96- (val) = op(addr); \
97+ (val) = op(args); \
98 break; \
99 } \
100 if (__delay_us) \
101- udelay(__delay_us); \
102+ udelay(__delay_us); \
103 } \
104 (cond) ? 0 : -ETIMEDOUT; \
105 })
106
107+/**
108+ * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
109+ * @op: accessor function (takes @addr as its only argument)
110+ * @addr: Address to poll
111+ * @val: Variable to read the value into
112+ * @cond: Break condition (usually involving @val)
113+ * @sleep_us: Maximum time to sleep between reads in us (0
114+ * tight-loops). Should be less than ~20ms since usleep_range
115+ * is used (see Documentation/timers/timers-howto.rst).
116+ * @timeout_us: Timeout in us, 0 means never timeout
117+ *
118+ * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
119+ * case, the last read value at @addr is stored in @val. Must not
120+ * be called from atomic context if sleep_us or timeout_us are used.
121+ *
122+ * When available, you'll probably want to use one of the specialized
123+ * macros defined below rather than this macro directly.
124+ */
125+#define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
126+ read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
127+
128+/**
129+ * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
130+ * @op: accessor function (takes @addr as its only argument)
131+ * @addr: Address to poll
132+ * @val: Variable to read the value into
133+ * @cond: Break condition (usually involving @val)
134+ * @delay_us: Time to udelay between reads in us (0 tight-loops). Should
135+ * be less than ~10us since udelay is used (see
136+ * Documentation/timers/timers-howto.rst).
137+ * @timeout_us: Timeout in us, 0 means never timeout
138+ *
139+ * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
140+ * case, the last read value at @addr is stored in @val.
141+ *
142+ * When available, you'll probably want to use one of the specialized
143+ * macros defined below rather than this macro directly.
144+ */
145+#define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
146+ read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
147
148 #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
149 readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)