.. | .. |
---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved. |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or modify |
---|
5 | | - * it under the terms of the GNU General Public License version 2 and |
---|
6 | | - * only version 2 as published by the Free Software Foundation. |
---|
7 | | - * |
---|
8 | | - * This program is distributed in the hope that it will be useful, |
---|
9 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | | - * GNU General Public License for more details. |
---|
12 | | - * |
---|
13 | 4 | */ |
---|
14 | 5 | |
---|
15 | 6 | #ifndef _LINUX_IOPOLL_H |
---|
.. | .. |
---|
23 | 14 | #include <linux/io.h> |
---|
24 | 15 | |
---|
25 | 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 | + cpu_relax(); \ |
---|
| 57 | + } \ |
---|
| 58 | + (cond) ? 0 : -ETIMEDOUT; \ |
---|
| 59 | +}) |
---|
| 60 | + |
---|
| 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). Should |
---|
| 68 | + * be less than ~10us since udelay is used (see |
---|
| 69 | + * Documentation/timers/timers-howto.rst). |
---|
| 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 |
---|
| 73 | + * |
---|
| 74 | + * Returns 0 on success and -ETIMEDOUT upon a timeout. In either |
---|
| 75 | + * case, the last read value at @args is stored in @val. |
---|
| 76 | + * |
---|
| 77 | + * When available, you'll probably want to use one of the specialized |
---|
| 78 | + * macros defined below rather than this macro directly. |
---|
| 79 | + */ |
---|
| 80 | +#define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \ |
---|
| 81 | + delay_before_read, args...) \ |
---|
| 82 | +({ \ |
---|
| 83 | + u64 __timeout_us = (timeout_us); \ |
---|
| 84 | + unsigned long __delay_us = (delay_us); \ |
---|
| 85 | + ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ |
---|
| 86 | + if (delay_before_read && __delay_us) \ |
---|
| 87 | + udelay(__delay_us); \ |
---|
| 88 | + for (;;) { \ |
---|
| 89 | + (val) = op(args); \ |
---|
| 90 | + if (cond) \ |
---|
| 91 | + break; \ |
---|
| 92 | + if (__timeout_us && \ |
---|
| 93 | + ktime_compare(ktime_get(), __timeout) > 0) { \ |
---|
| 94 | + (val) = op(args); \ |
---|
| 95 | + break; \ |
---|
| 96 | + } \ |
---|
| 97 | + if (__delay_us) \ |
---|
| 98 | + udelay(__delay_us); \ |
---|
| 99 | + cpu_relax(); \ |
---|
| 100 | + } \ |
---|
| 101 | + (cond) ? 0 : -ETIMEDOUT; \ |
---|
| 102 | +}) |
---|
| 103 | + |
---|
| 104 | +/** |
---|
26 | 105 | * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs |
---|
27 | 106 | * @op: accessor function (takes @addr as its only argument) |
---|
28 | 107 | * @addr: Address to poll |
---|
.. | .. |
---|
30 | 109 | * @cond: Break condition (usually involving @val) |
---|
31 | 110 | * @sleep_us: Maximum time to sleep between reads in us (0 |
---|
32 | 111 | * tight-loops). Should be less than ~20ms since usleep_range |
---|
33 | | - * is used (see Documentation/timers/timers-howto.txt). |
---|
| 112 | + * is used (see Documentation/timers/timers-howto.rst). |
---|
34 | 113 | * @timeout_us: Timeout in us, 0 means never timeout |
---|
35 | 114 | * |
---|
36 | 115 | * Returns 0 on success and -ETIMEDOUT upon a timeout. In either |
---|
.. | .. |
---|
41 | 120 | * macros defined below rather than this macro directly. |
---|
42 | 121 | */ |
---|
43 | 122 | #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \ |
---|
44 | | -({ \ |
---|
45 | | - u64 __timeout_us = (timeout_us); \ |
---|
46 | | - unsigned long __sleep_us = (sleep_us); \ |
---|
47 | | - ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ |
---|
48 | | - might_sleep_if((__sleep_us) != 0); \ |
---|
49 | | - for (;;) { \ |
---|
50 | | - (val) = op(addr); \ |
---|
51 | | - if (cond) \ |
---|
52 | | - break; \ |
---|
53 | | - if (__timeout_us && \ |
---|
54 | | - ktime_compare(ktime_get(), __timeout) > 0) { \ |
---|
55 | | - (val) = op(addr); \ |
---|
56 | | - break; \ |
---|
57 | | - } \ |
---|
58 | | - if (__sleep_us) \ |
---|
59 | | - usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ |
---|
60 | | - } \ |
---|
61 | | - (cond) ? 0 : -ETIMEDOUT; \ |
---|
62 | | -}) |
---|
| 123 | + read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr) |
---|
63 | 124 | |
---|
64 | 125 | /** |
---|
65 | 126 | * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs |
---|
.. | .. |
---|
69 | 130 | * @cond: Break condition (usually involving @val) |
---|
70 | 131 | * @delay_us: Time to udelay between reads in us (0 tight-loops). Should |
---|
71 | 132 | * be less than ~10us since udelay is used (see |
---|
72 | | - * Documentation/timers/timers-howto.txt). |
---|
| 133 | + * Documentation/timers/timers-howto.rst). |
---|
73 | 134 | * @timeout_us: Timeout in us, 0 means never timeout |
---|
74 | 135 | * |
---|
75 | 136 | * Returns 0 on success and -ETIMEDOUT upon a timeout. In either |
---|
.. | .. |
---|
79 | 140 | * macros defined below rather than this macro directly. |
---|
80 | 141 | */ |
---|
81 | 142 | #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \ |
---|
82 | | -({ \ |
---|
83 | | - u64 __timeout_us = (timeout_us); \ |
---|
84 | | - unsigned long __delay_us = (delay_us); \ |
---|
85 | | - ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ |
---|
86 | | - for (;;) { \ |
---|
87 | | - (val) = op(addr); \ |
---|
88 | | - if (cond) \ |
---|
89 | | - break; \ |
---|
90 | | - if (__timeout_us && \ |
---|
91 | | - ktime_compare(ktime_get(), __timeout) > 0) { \ |
---|
92 | | - (val) = op(addr); \ |
---|
93 | | - break; \ |
---|
94 | | - } \ |
---|
95 | | - if (__delay_us) \ |
---|
96 | | - udelay(__delay_us); \ |
---|
97 | | - } \ |
---|
98 | | - (cond) ? 0 : -ETIMEDOUT; \ |
---|
99 | | -}) |
---|
100 | | - |
---|
| 143 | + read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr) |
---|
101 | 144 | |
---|
102 | 145 | #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \ |
---|
103 | 146 | readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us) |
---|