Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / include / linux / iopoll.h
blob91324c331a4b2b50526a1c379135aad0c2970d46
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
4 */
6 #ifndef _LINUX_IOPOLL_H
7 #define _LINUX_IOPOLL_H
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>
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 tight-loops). Please
23 * read usleep_range() function description for details and
24 * limitations.
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
29 * When available, you'll probably want to use one of the specialized
30 * macros defined below rather than this macro directly.
32 * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
33 * case, the last read value at @args is stored in @val. Must not
34 * be called from atomic context if sleep_us or timeout_us are used.
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 cpu_relax(); \
57 } \
58 (cond) ? 0 : -ETIMEDOUT; \
61 /**
62 * read_poll_timeout_atomic - Periodically poll an address until a condition is
63 * met or a timeout occurs
64 * @op: accessor function (takes @args as its arguments)
65 * @val: Variable to read the value into
66 * @cond: Break condition (usually involving @val)
67 * @delay_us: Time to udelay between reads in us (0 tight-loops). Please
68 * read udelay() function description for details and
69 * limitations.
70 * @timeout_us: Timeout in us, 0 means never timeout
71 * @delay_before_read: if it is true, delay @delay_us before read.
72 * @args: arguments for @op poll
74 * This macro does not rely on timekeeping. Hence it is safe to call even when
75 * timekeeping is suspended, at the expense of an underestimation of wall clock
76 * time, which is rather minimal with a non-zero delay_us.
78 * When available, you'll probably want to use one of the specialized
79 * macros defined below rather than this macro directly.
81 * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
82 * case, the last read value at @args is stored in @val.
84 #define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
85 delay_before_read, args...) \
86 ({ \
87 u64 __timeout_us = (timeout_us); \
88 s64 __left_ns = __timeout_us * NSEC_PER_USEC; \
89 unsigned long __delay_us = (delay_us); \
90 u64 __delay_ns = __delay_us * NSEC_PER_USEC; \
91 if (delay_before_read && __delay_us) { \
92 udelay(__delay_us); \
93 if (__timeout_us) \
94 __left_ns -= __delay_ns; \
95 } \
96 for (;;) { \
97 (val) = op(args); \
98 if (cond) \
99 break; \
100 if (__timeout_us && __left_ns < 0) { \
101 (val) = op(args); \
102 break; \
104 if (__delay_us) { \
105 udelay(__delay_us); \
106 if (__timeout_us) \
107 __left_ns -= __delay_ns; \
109 cpu_relax(); \
110 if (__timeout_us) \
111 __left_ns--; \
113 (cond) ? 0 : -ETIMEDOUT; \
117 * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
118 * @op: accessor function (takes @addr as its only argument)
119 * @addr: Address to poll
120 * @val: Variable to read the value into
121 * @cond: Break condition (usually involving @val)
122 * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops). Please
123 * read usleep_range() function description for details and
124 * limitations.
125 * @timeout_us: Timeout in us, 0 means never timeout
127 * When available, you'll probably want to use one of the specialized
128 * macros defined below rather than this macro directly.
130 * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
131 * case, the last read value at @addr is stored in @val. Must not
132 * be called from atomic context if sleep_us or timeout_us are used.
134 #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
135 read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
138 * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
139 * @op: accessor function (takes @addr as its only argument)
140 * @addr: Address to poll
141 * @val: Variable to read the value into
142 * @cond: Break condition (usually involving @val)
143 * @delay_us: Time to udelay between reads in us (0 tight-loops). Please
144 * read udelay() function description for details and
145 * limitations.
146 * @timeout_us: Timeout in us, 0 means never timeout
148 * When available, you'll probably want to use one of the specialized
149 * macros defined below rather than this macro directly.
151 * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
152 * case, the last read value at @addr is stored in @val.
154 #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
155 read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
157 #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
158 readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
160 #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
161 readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
163 #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
164 readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
166 #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
167 readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
169 #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
170 readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
172 #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
173 readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
175 #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
176 readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
178 #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
179 readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
181 #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
182 readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
184 #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
185 readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
187 #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
188 readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
190 #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
191 readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
193 #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
194 readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
196 #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
197 readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
199 #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
200 readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
202 #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
203 readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
205 #endif /* _LINUX_IOPOLL_H */