1
2
3
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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79#define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
80 delay_before_read, args...) \
81({ \
82 u64 __timeout_us = (timeout_us); \
83 unsigned long __delay_us = (delay_us); \
84 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
85 if (delay_before_read && __delay_us) \
86 udelay(__delay_us); \
87 for (;;) { \
88 (val) = op(args); \
89 if (cond) \
90 break; \
91 if (__timeout_us && \
92 ktime_compare(ktime_get(), __timeout) > 0) { \
93 (val) = op(args); \
94 break; \
95 } \
96 if (__delay_us) \
97 udelay(__delay_us); \
98 } \
99 (cond) ? 0 : -ETIMEDOUT; \
100})
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120#define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
121 read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140#define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
141 read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
142
143#define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
144 readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
145
146#define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
147 readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
148
149#define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
150 readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
151
152#define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
153 readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
154
155#define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
156 readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
157
158#define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
159 readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
160
161#define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
162 readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
163
164#define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
165 readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
166
167#define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
168 readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
169
170#define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
171 readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
172
173#define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
174 readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
175
176#define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
177 readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
178
179#define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
180 readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
181
182#define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
183 readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
184
185#define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
186 readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
187
188#define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
189 readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
190
191#endif
192