]>
Commit | Line | Data |
---|---|---|
97fb5e8d | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
54c52312 MW |
2 | /* |
3 | * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved. | |
54c52312 MW |
4 | */ |
5 | ||
6 | #ifndef _LINUX_IOPOLL_H | |
7 | #define _LINUX_IOPOLL_H | |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/types.h> | |
16f4e319 | 11 | #include <linux/ktime.h> |
54c52312 MW |
12 | #include <linux/delay.h> |
13 | #include <linux/errno.h> | |
14 | #include <linux/io.h> | |
15 | ||
5f5323a1 DZ |
16 | /** |
17 | * read_poll_timeout - Periodically poll an address until a condition is | |
18 | * met or a timeout occurs | |
19 | * @op: accessor function (takes @args as its arguments) | |
20 | * @val: Variable to read the value into | |
21 | * @cond: Break condition (usually involving @val) | |
22 | * @sleep_us: Maximum time to sleep between reads in us (0 | |
23 | * tight-loops). Should be less than ~20ms since usleep_range | |
24 | * is used (see Documentation/timers/timers-howto.rst). | |
25 | * @timeout_us: Timeout in us, 0 means never timeout | |
26 | * @sleep_before_read: if it is true, sleep @sleep_us before read. | |
27 | * @args: arguments for @op poll | |
28 | * | |
29 | * Returns 0 on success and -ETIMEDOUT upon a timeout. In either | |
30 | * case, the last read value at @args is stored in @val. Must not | |
31 | * be called from atomic context if sleep_us or timeout_us are used. | |
32 | * | |
33 | * When available, you'll probably want to use one of the specialized | |
34 | * macros defined below rather than this macro directly. | |
35 | */ | |
36 | #define read_poll_timeout(op, val, cond, sleep_us, timeout_us, \ | |
37 | sleep_before_read, args...) \ | |
38 | ({ \ | |
39 | u64 __timeout_us = (timeout_us); \ | |
40 | unsigned long __sleep_us = (sleep_us); \ | |
41 | ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ | |
42 | might_sleep_if((__sleep_us) != 0); \ | |
43 | if (sleep_before_read && __sleep_us) \ | |
44 | usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ | |
45 | for (;;) { \ | |
46 | (val) = op(args); \ | |
47 | if (cond) \ | |
48 | break; \ | |
49 | if (__timeout_us && \ | |
50 | ktime_compare(ktime_get(), __timeout) > 0) { \ | |
51 | (val) = op(args); \ | |
52 | break; \ | |
53 | } \ | |
54 | if (__sleep_us) \ | |
55 | usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ | |
56 | } \ | |
57 | (cond) ? 0 : -ETIMEDOUT; \ | |
58 | }) | |
59 | ||
54c52312 MW |
60 | /** |
61 | * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs | |
62 | * @op: accessor function (takes @addr as its only argument) | |
63 | * @addr: Address to poll | |
64 | * @val: Variable to read the value into | |
65 | * @cond: Break condition (usually involving @val) | |
66 | * @sleep_us: Maximum time to sleep between reads in us (0 | |
67 | * tight-loops). Should be less than ~20ms since usleep_range | |
458f69ef | 68 | * is used (see Documentation/timers/timers-howto.rst). |
54c52312 MW |
69 | * @timeout_us: Timeout in us, 0 means never timeout |
70 | * | |
71 | * Returns 0 on success and -ETIMEDOUT upon a timeout. In either | |
72 | * case, the last read value at @addr is stored in @val. Must not | |
73 | * be called from atomic context if sleep_us or timeout_us are used. | |
74 | * | |
75 | * When available, you'll probably want to use one of the specialized | |
76 | * macros defined below rather than this macro directly. | |
77 | */ | |
78 | #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \ | |
79 | ({ \ | |
d15809f3 AB |
80 | u64 __timeout_us = (timeout_us); \ |
81 | unsigned long __sleep_us = (sleep_us); \ | |
82 | ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ | |
83 | might_sleep_if((__sleep_us) != 0); \ | |
54c52312 MW |
84 | for (;;) { \ |
85 | (val) = op(addr); \ | |
86 | if (cond) \ | |
87 | break; \ | |
d15809f3 AB |
88 | if (__timeout_us && \ |
89 | ktime_compare(ktime_get(), __timeout) > 0) { \ | |
54c52312 MW |
90 | (val) = op(addr); \ |
91 | break; \ | |
92 | } \ | |
d15809f3 AB |
93 | if (__sleep_us) \ |
94 | usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ | |
54c52312 MW |
95 | } \ |
96 | (cond) ? 0 : -ETIMEDOUT; \ | |
97 | }) | |
98 | ||
99 | /** | |
100 | * readx_poll_timeout_atomic - 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 | * @delay_us: Time to udelay between reads in us (0 tight-loops). Should | |
106 | * be less than ~10us since udelay is used (see | |
458f69ef | 107 | * Documentation/timers/timers-howto.rst). |
54c52312 MW |
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. | |
112 | * | |
113 | * When available, you'll probably want to use one of the specialized | |
114 | * macros defined below rather than this macro directly. | |
115 | */ | |
116 | #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \ | |
117 | ({ \ | |
d15809f3 AB |
118 | u64 __timeout_us = (timeout_us); \ |
119 | unsigned long __delay_us = (delay_us); \ | |
120 | ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ | |
54c52312 MW |
121 | for (;;) { \ |
122 | (val) = op(addr); \ | |
123 | if (cond) \ | |
124 | break; \ | |
d15809f3 AB |
125 | if (__timeout_us && \ |
126 | ktime_compare(ktime_get(), __timeout) > 0) { \ | |
54c52312 MW |
127 | (val) = op(addr); \ |
128 | break; \ | |
129 | } \ | |
d15809f3 AB |
130 | if (__delay_us) \ |
131 | udelay(__delay_us); \ | |
54c52312 MW |
132 | } \ |
133 | (cond) ? 0 : -ETIMEDOUT; \ | |
134 | }) | |
135 | ||
136 | ||
137 | #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \ | |
138 | readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us) | |
139 | ||
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 | ||
143 | #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \ | |
144 | readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us) | |
145 | ||
146 | #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ | |
147 | readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us) | |
148 | ||
149 | #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \ | |
150 | readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us) | |
151 | ||
152 | #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ | |
153 | readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us) | |
154 | ||
155 | #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \ | |
156 | readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us) | |
157 | ||
158 | #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ | |
159 | readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us) | |
160 | ||
161 | #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \ | |
162 | readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us) | |
163 | ||
164 | #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ | |
165 | readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us) | |
166 | ||
167 | #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \ | |
168 | readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us) | |
169 | ||
170 | #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ | |
171 | readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us) | |
172 | ||
173 | #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \ | |
174 | readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us) | |
175 | ||
176 | #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ | |
177 | readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us) | |
178 | ||
179 | #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \ | |
180 | readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us) | |
181 | ||
182 | #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ | |
183 | readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us) | |
184 | ||
185 | #endif /* _LINUX_IOPOLL_H */ |