Blame view

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