xref: /openbmc/linux/include/linux/iopoll.h (revision 7349a69c)
197fb5e8dSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
254c52312SMatt Wagantall /*
354c52312SMatt Wagantall  * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
454c52312SMatt Wagantall  */
554c52312SMatt Wagantall 
654c52312SMatt Wagantall #ifndef _LINUX_IOPOLL_H
754c52312SMatt Wagantall #define _LINUX_IOPOLL_H
854c52312SMatt Wagantall 
954c52312SMatt Wagantall #include <linux/kernel.h>
1054c52312SMatt Wagantall #include <linux/types.h>
1116f4e319SMasahiro Yamada #include <linux/ktime.h>
1254c52312SMatt Wagantall #include <linux/delay.h>
1354c52312SMatt Wagantall #include <linux/errno.h>
1454c52312SMatt Wagantall #include <linux/io.h>
1554c52312SMatt Wagantall 
1654c52312SMatt Wagantall /**
175f5323a1SDejin Zheng  * read_poll_timeout - Periodically poll an address until a condition is
185f5323a1SDejin Zheng  *			met or a timeout occurs
195f5323a1SDejin Zheng  * @op: accessor function (takes @args as its arguments)
205f5323a1SDejin Zheng  * @val: Variable to read the value into
215f5323a1SDejin Zheng  * @cond: Break condition (usually involving @val)
225f5323a1SDejin Zheng  * @sleep_us: Maximum time to sleep between reads in us (0
235f5323a1SDejin Zheng  *            tight-loops).  Should be less than ~20ms since usleep_range
245f5323a1SDejin Zheng  *            is used (see Documentation/timers/timers-howto.rst).
255f5323a1SDejin Zheng  * @timeout_us: Timeout in us, 0 means never timeout
265f5323a1SDejin Zheng  * @sleep_before_read: if it is true, sleep @sleep_us before read.
275f5323a1SDejin Zheng  * @args: arguments for @op poll
285f5323a1SDejin Zheng  *
295f5323a1SDejin Zheng  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
305f5323a1SDejin Zheng  * case, the last read value at @args is stored in @val. Must not
315f5323a1SDejin Zheng  * be called from atomic context if sleep_us or timeout_us are used.
325f5323a1SDejin Zheng  *
335f5323a1SDejin Zheng  * When available, you'll probably want to use one of the specialized
345f5323a1SDejin Zheng  * macros defined below rather than this macro directly.
355f5323a1SDejin Zheng  */
365f5323a1SDejin Zheng #define read_poll_timeout(op, val, cond, sleep_us, timeout_us, \
375f5323a1SDejin Zheng 				sleep_before_read, args...) \
385f5323a1SDejin Zheng ({ \
395f5323a1SDejin Zheng 	u64 __timeout_us = (timeout_us); \
405f5323a1SDejin Zheng 	unsigned long __sleep_us = (sleep_us); \
415f5323a1SDejin Zheng 	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
425f5323a1SDejin Zheng 	might_sleep_if((__sleep_us) != 0); \
435f5323a1SDejin Zheng 	if (sleep_before_read && __sleep_us) \
445f5323a1SDejin Zheng 		usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
455f5323a1SDejin Zheng 	for (;;) { \
465f5323a1SDejin Zheng 		(val) = op(args); \
475f5323a1SDejin Zheng 		if (cond) \
485f5323a1SDejin Zheng 			break; \
495f5323a1SDejin Zheng 		if (__timeout_us && \
505f5323a1SDejin Zheng 		    ktime_compare(ktime_get(), __timeout) > 0) { \
515f5323a1SDejin Zheng 			(val) = op(args); \
525f5323a1SDejin Zheng 			break; \
535f5323a1SDejin Zheng 		} \
545f5323a1SDejin Zheng 		if (__sleep_us) \
555f5323a1SDejin Zheng 			usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
56b407460eSGeert Uytterhoeven 		cpu_relax(); \
575f5323a1SDejin Zheng 	} \
585f5323a1SDejin Zheng 	(cond) ? 0 : -ETIMEDOUT; \
595f5323a1SDejin Zheng })
605f5323a1SDejin Zheng 
615f5323a1SDejin Zheng /**
6257a29df3SKai-Heng Feng  * read_poll_timeout_atomic - Periodically poll an address until a condition is
6357a29df3SKai-Heng Feng  * 				met or a timeout occurs
6434d8f7a4SChunfeng Yun  * @op: accessor function (takes @args as its arguments)
6557a29df3SKai-Heng Feng  * @val: Variable to read the value into
6657a29df3SKai-Heng Feng  * @cond: Break condition (usually involving @val)
6757a29df3SKai-Heng Feng  * @delay_us: Time to udelay between reads in us (0 tight-loops).  Should
6857a29df3SKai-Heng Feng  *            be less than ~10us since udelay is used (see
6957a29df3SKai-Heng Feng  *            Documentation/timers/timers-howto.rst).
7057a29df3SKai-Heng Feng  * @timeout_us: Timeout in us, 0 means never timeout
7157a29df3SKai-Heng Feng  * @delay_before_read: if it is true, delay @delay_us before read.
7234d8f7a4SChunfeng Yun  * @args: arguments for @op poll
7357a29df3SKai-Heng Feng  *
7457a29df3SKai-Heng Feng  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
7557a29df3SKai-Heng Feng  * case, the last read value at @args is stored in @val.
7657a29df3SKai-Heng Feng  *
77*7349a69cSGeert Uytterhoeven  * This macro does not rely on timekeeping.  Hence it is safe to call even when
78*7349a69cSGeert Uytterhoeven  * timekeeping is suspended, at the expense of an underestimation of wall clock
79*7349a69cSGeert Uytterhoeven  * time, which is rather minimal with a non-zero delay_us.
80*7349a69cSGeert Uytterhoeven  *
8157a29df3SKai-Heng Feng  * When available, you'll probably want to use one of the specialized
8257a29df3SKai-Heng Feng  * macros defined below rather than this macro directly.
8357a29df3SKai-Heng Feng  */
8457a29df3SKai-Heng Feng #define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
8557a29df3SKai-Heng Feng 					delay_before_read, args...) \
8657a29df3SKai-Heng Feng ({ \
8757a29df3SKai-Heng Feng 	u64 __timeout_us = (timeout_us); \
88*7349a69cSGeert Uytterhoeven 	s64 __left_ns = __timeout_us * NSEC_PER_USEC; \
8957a29df3SKai-Heng Feng 	unsigned long __delay_us = (delay_us); \
90*7349a69cSGeert Uytterhoeven 	u64 __delay_ns = __delay_us * NSEC_PER_USEC; \
91*7349a69cSGeert Uytterhoeven 	if (delay_before_read && __delay_us) { \
9257a29df3SKai-Heng Feng 		udelay(__delay_us); \
93*7349a69cSGeert Uytterhoeven 		if (__timeout_us) \
94*7349a69cSGeert Uytterhoeven 			__left_ns -= __delay_ns; \
95*7349a69cSGeert Uytterhoeven 	} \
9657a29df3SKai-Heng Feng 	for (;;) { \
9757a29df3SKai-Heng Feng 		(val) = op(args); \
9857a29df3SKai-Heng Feng 		if (cond) \
9957a29df3SKai-Heng Feng 			break; \
100*7349a69cSGeert Uytterhoeven 		if (__timeout_us && __left_ns < 0) { \
10157a29df3SKai-Heng Feng 			(val) = op(args); \
10257a29df3SKai-Heng Feng 			break; \
10357a29df3SKai-Heng Feng 		} \
104*7349a69cSGeert Uytterhoeven 		if (__delay_us) { \
10557a29df3SKai-Heng Feng 			udelay(__delay_us); \
106*7349a69cSGeert Uytterhoeven 			if (__timeout_us) \
107*7349a69cSGeert Uytterhoeven 				__left_ns -= __delay_ns; \
108*7349a69cSGeert Uytterhoeven 		} \
109b407460eSGeert Uytterhoeven 		cpu_relax(); \
110*7349a69cSGeert Uytterhoeven 		if (__timeout_us) \
111*7349a69cSGeert Uytterhoeven 			__left_ns--; \
11257a29df3SKai-Heng Feng 	} \
11357a29df3SKai-Heng Feng 	(cond) ? 0 : -ETIMEDOUT; \
11457a29df3SKai-Heng Feng })
11557a29df3SKai-Heng Feng 
11657a29df3SKai-Heng Feng /**
11754c52312SMatt Wagantall  * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
11854c52312SMatt Wagantall  * @op: accessor function (takes @addr as its only argument)
11954c52312SMatt Wagantall  * @addr: Address to poll
12054c52312SMatt Wagantall  * @val: Variable to read the value into
12154c52312SMatt Wagantall  * @cond: Break condition (usually involving @val)
12254c52312SMatt Wagantall  * @sleep_us: Maximum time to sleep between reads in us (0
12354c52312SMatt Wagantall  *            tight-loops).  Should be less than ~20ms since usleep_range
124458f69efSMauro Carvalho Chehab  *            is used (see Documentation/timers/timers-howto.rst).
12554c52312SMatt Wagantall  * @timeout_us: Timeout in us, 0 means never timeout
12654c52312SMatt Wagantall  *
12754c52312SMatt Wagantall  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
12854c52312SMatt Wagantall  * case, the last read value at @addr is stored in @val. Must not
12954c52312SMatt Wagantall  * be called from atomic context if sleep_us or timeout_us are used.
13054c52312SMatt Wagantall  *
13154c52312SMatt Wagantall  * When available, you'll probably want to use one of the specialized
13254c52312SMatt Wagantall  * macros defined below rather than this macro directly.
13354c52312SMatt Wagantall  */
13454c52312SMatt Wagantall #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us)	\
135eaa6b010SDejin Zheng 	read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
13654c52312SMatt Wagantall 
13754c52312SMatt Wagantall /**
13854c52312SMatt Wagantall  * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
13954c52312SMatt Wagantall  * @op: accessor function (takes @addr as its only argument)
14054c52312SMatt Wagantall  * @addr: Address to poll
14154c52312SMatt Wagantall  * @val: Variable to read the value into
14254c52312SMatt Wagantall  * @cond: Break condition (usually involving @val)
14354c52312SMatt Wagantall  * @delay_us: Time to udelay between reads in us (0 tight-loops).  Should
14454c52312SMatt Wagantall  *            be less than ~10us since udelay is used (see
145458f69efSMauro Carvalho Chehab  *            Documentation/timers/timers-howto.rst).
14654c52312SMatt Wagantall  * @timeout_us: Timeout in us, 0 means never timeout
14754c52312SMatt Wagantall  *
14854c52312SMatt Wagantall  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
14954c52312SMatt Wagantall  * case, the last read value at @addr is stored in @val.
15054c52312SMatt Wagantall  *
15154c52312SMatt Wagantall  * When available, you'll probably want to use one of the specialized
15254c52312SMatt Wagantall  * macros defined below rather than this macro directly.
15354c52312SMatt Wagantall  */
15454c52312SMatt Wagantall #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
15557a29df3SKai-Heng Feng 	read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
15654c52312SMatt Wagantall 
15754c52312SMatt Wagantall #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
15854c52312SMatt Wagantall 	readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
15954c52312SMatt Wagantall 
16054c52312SMatt Wagantall #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
16154c52312SMatt Wagantall 	readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
16254c52312SMatt Wagantall 
16354c52312SMatt Wagantall #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
16454c52312SMatt Wagantall 	readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
16554c52312SMatt Wagantall 
16654c52312SMatt Wagantall #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
16754c52312SMatt Wagantall 	readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
16854c52312SMatt Wagantall 
16954c52312SMatt Wagantall #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
17054c52312SMatt Wagantall 	readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
17154c52312SMatt Wagantall 
17254c52312SMatt Wagantall #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
17354c52312SMatt Wagantall 	readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
17454c52312SMatt Wagantall 
17554c52312SMatt Wagantall #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
17654c52312SMatt Wagantall 	readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
17754c52312SMatt Wagantall 
17854c52312SMatt Wagantall #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
17954c52312SMatt Wagantall 	readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
18054c52312SMatt Wagantall 
18154c52312SMatt Wagantall #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
18254c52312SMatt Wagantall 	readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
18354c52312SMatt Wagantall 
18454c52312SMatt Wagantall #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
18554c52312SMatt Wagantall 	readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
18654c52312SMatt Wagantall 
18754c52312SMatt Wagantall #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
18854c52312SMatt Wagantall 	readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
18954c52312SMatt Wagantall 
19054c52312SMatt Wagantall #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
19154c52312SMatt Wagantall 	readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
19254c52312SMatt Wagantall 
19354c52312SMatt Wagantall #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
19454c52312SMatt Wagantall 	readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
19554c52312SMatt Wagantall 
19654c52312SMatt Wagantall #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
19754c52312SMatt Wagantall 	readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
19854c52312SMatt Wagantall 
19954c52312SMatt Wagantall #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
20054c52312SMatt Wagantall 	readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
20154c52312SMatt Wagantall 
20254c52312SMatt Wagantall #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
20354c52312SMatt Wagantall 	readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
20454c52312SMatt Wagantall 
20554c52312SMatt Wagantall #endif /* _LINUX_IOPOLL_H */
206