xref: /linux/include/linux/iopoll.h (revision ec2e0fb07d789976c601bec19ecced7a501c3705)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
4  */
5 
6 #ifndef _LINUX_IOPOLL_H
7 #define _LINUX_IOPOLL_H
8 
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/ktime.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/io.h>
15 
16 /**
17  * poll_timeout_us - Periodically poll and perform an operation until
18  *                   a condition is met or a timeout occurs
19  *
20  * @op: Operation
21  * @cond: Break condition
22  * @sleep_us: Maximum time to sleep between operations in us (0 tight-loops).
23  *            Please read usleep_range() function description for details and
24  *            limitations.
25  * @timeout_us: Timeout in us, 0 means never timeout
26  * @sleep_before_op: if it is true, sleep @sleep_us before operation.
27  *
28  * When available, you'll probably want to use one of the specialized
29  * macros defined below rather than this macro directly.
30  *
31  * Returns: 0 on success and -ETIMEDOUT upon a timeout. Must not
32  * be called from atomic context if sleep_us or timeout_us are used.
33  */
34 #define poll_timeout_us(op, cond, sleep_us, timeout_us, sleep_before_op) \
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 	int ___ret; \
40 	might_sleep_if((__sleep_us) != 0); \
41 	if ((sleep_before_op) && __sleep_us) \
42 		usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
43 	for (;;) { \
44 		bool __expired = __timeout_us && \
45 			ktime_compare(ktime_get(), __timeout) > 0; \
46 		/* guarantee 'op' and 'cond' are evaluated after timeout expired */ \
47 		barrier(); \
48 		op; \
49 		if (cond) { \
50 			___ret = 0; \
51 			break; \
52 		} \
53 		if (__expired) { \
54 			___ret = -ETIMEDOUT; \
55 			break; \
56 		} \
57 		if (__sleep_us) \
58 			usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
59 		cpu_relax(); \
60 	} \
61 	___ret; \
62 })
63 
64 /**
65  * poll_timeout_us_atomic - Periodically poll and perform an operation until
66  *                          a condition is met or a timeout occurs
67  *
68  * @op: Operation
69  * @cond: Break condition
70  * @delay_us: Time to udelay between operations in us (0 tight-loops).
71  *            Please read udelay() function description for details and
72  *            limitations.
73  * @timeout_us: Timeout in us, 0 means never timeout
74  * @delay_before_op: if it is true, delay @delay_us before operation.
75  *
76  * This macro does not rely on timekeeping.  Hence it is safe to call even when
77  * timekeeping is suspended, at the expense of an underestimation of wall clock
78  * time, which is rather minimal with a non-zero delay_us.
79  *
80  * When available, you'll probably want to use one of the specialized
81  * macros defined below rather than this macro directly.
82  *
83  * Returns: 0 on success and -ETIMEDOUT upon a timeout.
84  */
85 #define poll_timeout_us_atomic(op, cond, delay_us, timeout_us, \
86 			       delay_before_op) \
87 ({ \
88 	u64 __timeout_us = (timeout_us); \
89 	s64 __left_ns = __timeout_us * NSEC_PER_USEC; \
90 	unsigned long __delay_us = (delay_us); \
91 	u64 __delay_ns = __delay_us * NSEC_PER_USEC; \
92 	int ___ret; \
93 	if ((delay_before_op) && __delay_us) { \
94 		udelay(__delay_us); \
95 		if (__timeout_us) \
96 			__left_ns -= __delay_ns; \
97 	} \
98 	for (;;) { \
99 		bool __expired = __timeout_us && __left_ns < 0; \
100 		/* guarantee 'op' and 'cond' are evaluated after timeout expired */ \
101 		barrier(); \
102 		op; \
103 		if (cond) { \
104 			___ret = 0; \
105 			break; \
106 		} \
107 		if (__expired) { \
108 			___ret = -ETIMEDOUT; \
109 			break; \
110 		} \
111 		if (__delay_us) { \
112 			udelay(__delay_us); \
113 			if (__timeout_us) \
114 				__left_ns -= __delay_ns; \
115 		} \
116 		cpu_relax(); \
117 		if (__timeout_us) \
118 			__left_ns--; \
119 	} \
120 	___ret; \
121 })
122 
123 /**
124  * read_poll_timeout - Periodically poll an address until a condition is
125  *                     met or a timeout occurs
126  * @op: accessor function (takes @args as its arguments)
127  * @val: Variable to read the value into
128  * @cond: Break condition (usually involving @val)
129  * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops). Please
130  *            read usleep_range() function description for details and
131  *            limitations.
132  * @timeout_us: Timeout in us, 0 means never timeout
133  * @sleep_before_read: if it is true, sleep @sleep_us before read.
134  * @args: arguments for @op poll
135  *
136  * When available, you'll probably want to use one of the specialized
137  * macros defined below rather than this macro directly.
138  *
139  * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
140  * case, the last read value at @args is stored in @val. Must not
141  * be called from atomic context if sleep_us or timeout_us are used.
142  */
143 #define read_poll_timeout(op, val, cond, sleep_us, timeout_us, \
144 			  sleep_before_read, args...) \
145 	poll_timeout_us((val) = op(args), cond, sleep_us, timeout_us, sleep_before_read)
146 
147 /**
148  * read_poll_timeout_atomic - Periodically poll an address until a condition is
149  *                            met or a timeout occurs
150  * @op: accessor function (takes @args as its arguments)
151  * @val: Variable to read the value into
152  * @cond: Break condition (usually involving @val)
153  * @delay_us: Time to udelay between reads in us (0 tight-loops). Please
154  *            read udelay() function description for details and
155  *            limitations.
156  * @timeout_us: Timeout in us, 0 means never timeout
157  * @delay_before_read: if it is true, delay @delay_us before read.
158  * @args: arguments for @op poll
159  *
160  * This macro does not rely on timekeeping.  Hence it is safe to call even when
161  * timekeeping is suspended, at the expense of an underestimation of wall clock
162  * time, which is rather minimal with a non-zero delay_us.
163  *
164  * When available, you'll probably want to use one of the specialized
165  * macros defined below rather than this macro directly.
166  *
167  * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
168  * case, the last read value at @args is stored in @val.
169  */
170 #define read_poll_timeout_atomic(op, val, cond, sleep_us, timeout_us, \
171 				 sleep_before_read, args...) \
172 	poll_timeout_us_atomic((val) = op(args), cond, sleep_us, timeout_us, sleep_before_read)
173 
174 /**
175  * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
176  * @op: accessor function (takes @addr as its only argument)
177  * @addr: Address to poll
178  * @val: Variable to read the value into
179  * @cond: Break condition (usually involving @val)
180  * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops). Please
181  *            read usleep_range() function description for details and
182  *            limitations.
183  * @timeout_us: Timeout in us, 0 means never timeout
184  *
185  * When available, you'll probably want to use one of the specialized
186  * macros defined below rather than this macro directly.
187  *
188  * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
189  * case, the last read value at @addr is stored in @val. Must not
190  * be called from atomic context if sleep_us or timeout_us are used.
191  */
192 #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us)	\
193 	read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
194 
195 /**
196  * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
197  * @op: accessor function (takes @addr as its only argument)
198  * @addr: Address to poll
199  * @val: Variable to read the value into
200  * @cond: Break condition (usually involving @val)
201  * @delay_us: Time to udelay between reads in us (0 tight-loops). Please
202  *            read udelay() function description for details and
203  *            limitations.
204  * @timeout_us: Timeout in us, 0 means never timeout
205  *
206  * When available, you'll probably want to use one of the specialized
207  * macros defined below rather than this macro directly.
208  *
209  * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
210  * case, the last read value at @addr is stored in @val.
211  */
212 #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
213 	read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
214 
215 #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
216 	readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
217 
218 #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
219 	readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
220 
221 #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
222 	readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
223 
224 #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
225 	readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
226 
227 #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
228 	readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
229 
230 #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
231 	readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
232 
233 #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
234 	readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
235 
236 #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
237 	readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
238 
239 #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
240 	readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
241 
242 #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
243 	readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
244 
245 #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
246 	readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
247 
248 #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
249 	readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
250 
251 #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
252 	readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
253 
254 #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
255 	readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
256 
257 #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
258 	readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
259 
260 #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
261 	readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
262 
263 #endif /* _LINUX_IOPOLL_H */
264