Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iopoll: Generalize read_poll_timeout() into poll_timeout_us()

While read_poll_timeout() & co. were originally introduced just
for simple I/O usage scenarios they have since been generalized to
be useful in more cases.

However the interface is very cumbersome to use in the general case.
Attempt to make it more flexible by combining the 'op', 'var' and
'args' parameter into just a single 'op' that the caller can fully
specify.

For example i915 has one case where one might currently
have to write something like:
ret = read_poll_timeout(drm_dp_dpcd_read_byte, err,
err || (status & mask),
0 * 1000, 200 * 1000, false,
aux, DP_FEC_STATUS, &status);
which is practically illegible, but with the adjusted macro
we do:
ret = poll_timeout_us(err = drm_dp_dpcd_read_byte(aux, DP_FEC_STATUS, &status),
err || (status & mask),
0 * 1000, 200 * 1000, false);
which much easier to understand.

One could even combine the 'op' and 'cond' parameters into
one, but that might make the caller a bit too unwieldly with
assignments and checks being done on the same statement.

This makes poll_timeout_us() closer to the i915 __wait_for()
macro, with the main difference being that __wait_for() uses
expenential backoff as opposed to the fixed polling interval
used by poll_timeout_us(). Eventually we might be able to switch
(at least most of) i915 to use poll_timeout_us().

v2: Fix typos (Jani)
Fix delay_us docs for poll_timeout_us_atomic() (Jani)

Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Cc: Dibin Moolakadan Subrahmanian <dibin.moolakadan.subrahmanian@intel.com>
Cc: Imre Deak <imre.deak@intel.com>
Cc: David Laight <david.laight.linux@gmail.com>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Matt Wagantall <mattw@codeaurora.org>
Cc: Dejin Zheng <zhengdejin5@gmail.com>
Cc: intel-gfx@lists.freedesktop.org
Cc: intel-xe@lists.freedesktop.org
Cc: linux-kernel@vger.kernel.org
Reviewed-by: Jani Nikula <jani.nikula@intel.com>
Acked-by: Simona Vetter <simona.vetter@ffwll.ch>
Signed-off-by: Ville Syrjälä <ville.syrjala@linux.intel.com>
Link: https://lore.kernel.org/r/20250826121859.15497-1-ville.syrjala@linux.intel.com
Signed-off-by: Jani Nikula <jani.nikula@intel.com>

authored by

Ville Syrjälä and committed by
Jani Nikula
9df8043a 61a0ef59

+102 -56
+102 -56
include/linux/iopoll.h
··· 14 14 #include <linux/io.h> 15 15 16 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 + might_sleep_if((__sleep_us) != 0); \ 40 + if ((sleep_before_op) && __sleep_us) \ 41 + usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ 42 + for (;;) { \ 43 + op; \ 44 + if (cond) \ 45 + break; \ 46 + if (__timeout_us && \ 47 + ktime_compare(ktime_get(), __timeout) > 0) { \ 48 + op; \ 49 + break; \ 50 + } \ 51 + if (__sleep_us) \ 52 + usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ 53 + cpu_relax(); \ 54 + } \ 55 + (cond) ? 0 : -ETIMEDOUT; \ 56 + }) 57 + 58 + /** 59 + * poll_timeout_us_atomic - Periodically poll and perform an operation until 60 + * a condition is met or a timeout occurs 61 + * 62 + * @op: Operation 63 + * @cond: Break condition 64 + * @delay_us: Time to udelay between operations in us (0 tight-loops). 65 + * Please read udelay() function description for details and 66 + * limitations. 67 + * @timeout_us: Timeout in us, 0 means never timeout 68 + * @delay_before_op: if it is true, delay @delay_us before operation. 69 + * 70 + * This macro does not rely on timekeeping. Hence it is safe to call even when 71 + * timekeeping is suspended, at the expense of an underestimation of wall clock 72 + * time, which is rather minimal with a non-zero delay_us. 73 + * 74 + * When available, you'll probably want to use one of the specialized 75 + * macros defined below rather than this macro directly. 76 + * 77 + * Returns: 0 on success and -ETIMEDOUT upon a timeout. 78 + */ 79 + #define poll_timeout_us_atomic(op, cond, delay_us, timeout_us, \ 80 + delay_before_op) \ 81 + ({ \ 82 + u64 __timeout_us = (timeout_us); \ 83 + s64 __left_ns = __timeout_us * NSEC_PER_USEC; \ 84 + unsigned long __delay_us = (delay_us); \ 85 + u64 __delay_ns = __delay_us * NSEC_PER_USEC; \ 86 + if ((delay_before_op) && __delay_us) { \ 87 + udelay(__delay_us); \ 88 + if (__timeout_us) \ 89 + __left_ns -= __delay_ns; \ 90 + } \ 91 + for (;;) { \ 92 + op; \ 93 + if (cond) \ 94 + break; \ 95 + if (__timeout_us && __left_ns < 0) { \ 96 + op; \ 97 + break; \ 98 + } \ 99 + if (__delay_us) { \ 100 + udelay(__delay_us); \ 101 + if (__timeout_us) \ 102 + __left_ns -= __delay_ns; \ 103 + } \ 104 + cpu_relax(); \ 105 + if (__timeout_us) \ 106 + __left_ns--; \ 107 + } \ 108 + (cond) ? 0 : -ETIMEDOUT; \ 109 + }) 110 + 111 + /** 17 112 * read_poll_timeout - Periodically poll an address until a condition is 18 - * met or a timeout occurs 113 + * met or a timeout occurs 19 114 * @op: accessor function (takes @args as its arguments) 20 115 * @val: Variable to read the value into 21 116 * @cond: Break condition (usually involving @val) ··· 129 34 * be called from atomic context if sleep_us or timeout_us are used. 130 35 */ 131 36 #define read_poll_timeout(op, val, cond, sleep_us, timeout_us, \ 132 - sleep_before_read, args...) \ 133 - ({ \ 134 - u64 __timeout_us = (timeout_us); \ 135 - unsigned long __sleep_us = (sleep_us); \ 136 - ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ 137 - might_sleep_if((__sleep_us) != 0); \ 138 - if (sleep_before_read && __sleep_us) \ 139 - usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ 140 - for (;;) { \ 141 - (val) = op(args); \ 142 - if (cond) \ 143 - break; \ 144 - if (__timeout_us && \ 145 - ktime_compare(ktime_get(), __timeout) > 0) { \ 146 - (val) = op(args); \ 147 - break; \ 148 - } \ 149 - if (__sleep_us) \ 150 - usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ 151 - cpu_relax(); \ 152 - } \ 153 - (cond) ? 0 : -ETIMEDOUT; \ 154 - }) 37 + sleep_before_read, args...) \ 38 + poll_timeout_us((val) = op(args), cond, sleep_us, timeout_us, sleep_before_read) 155 39 156 40 /** 157 41 * read_poll_timeout_atomic - Periodically poll an address until a condition is 158 - * met or a timeout occurs 42 + * met or a timeout occurs 159 43 * @op: accessor function (takes @args as its arguments) 160 44 * @val: Variable to read the value into 161 45 * @cond: Break condition (usually involving @val) ··· 155 81 * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either 156 82 * case, the last read value at @args is stored in @val. 157 83 */ 158 - #define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \ 159 - delay_before_read, args...) \ 160 - ({ \ 161 - u64 __timeout_us = (timeout_us); \ 162 - s64 __left_ns = __timeout_us * NSEC_PER_USEC; \ 163 - unsigned long __delay_us = (delay_us); \ 164 - u64 __delay_ns = __delay_us * NSEC_PER_USEC; \ 165 - if (delay_before_read && __delay_us) { \ 166 - udelay(__delay_us); \ 167 - if (__timeout_us) \ 168 - __left_ns -= __delay_ns; \ 169 - } \ 170 - for (;;) { \ 171 - (val) = op(args); \ 172 - if (cond) \ 173 - break; \ 174 - if (__timeout_us && __left_ns < 0) { \ 175 - (val) = op(args); \ 176 - break; \ 177 - } \ 178 - if (__delay_us) { \ 179 - udelay(__delay_us); \ 180 - if (__timeout_us) \ 181 - __left_ns -= __delay_ns; \ 182 - } \ 183 - cpu_relax(); \ 184 - if (__timeout_us) \ 185 - __left_ns--; \ 186 - } \ 187 - (cond) ? 0 : -ETIMEDOUT; \ 188 - }) 84 + #define read_poll_timeout_atomic(op, val, cond, sleep_us, timeout_us, \ 85 + sleep_before_read, args...) \ 86 + poll_timeout_us_atomic((val) = op(args), cond, sleep_us, timeout_us, sleep_before_read) 189 87 190 88 /** 191 89 * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs