blob: 35e15dfd41557d361ef5b64436957852b6b9192f [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#ifndef _LINUX_IOPOLL_H
7#define _LINUX_IOPOLL_H
8
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>
15
16/**
17 * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
18 * @op: accessor function (takes @addr as its only argument)
19 * @addr: Address to poll
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
David Brazdil0f672f62019-12-10 10:32:29 +000024 * is used (see Documentation/timers/timers-howto.rst).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025 * @timeout_us: Timeout in us, 0 means never timeout
26 *
27 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
28 * case, the last read value at @addr is stored in @val. Must not
29 * be called from atomic context if sleep_us or timeout_us are used.
30 *
31 * When available, you'll probably want to use one of the specialized
32 * macros defined below rather than this macro directly.
33 */
34#define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
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 for (;;) { \
41 (val) = op(addr); \
42 if (cond) \
43 break; \
44 if (__timeout_us && \
45 ktime_compare(ktime_get(), __timeout) > 0) { \
46 (val) = op(addr); \
47 break; \
48 } \
49 if (__sleep_us) \
50 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
51 } \
52 (cond) ? 0 : -ETIMEDOUT; \
53})
54
55/**
56 * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
57 * @op: accessor function (takes @addr as its only argument)
58 * @addr: Address to poll
59 * @val: Variable to read the value into
60 * @cond: Break condition (usually involving @val)
61 * @delay_us: Time to udelay between reads in us (0 tight-loops). Should
62 * be less than ~10us since udelay is used (see
David Brazdil0f672f62019-12-10 10:32:29 +000063 * Documentation/timers/timers-howto.rst).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064 * @timeout_us: Timeout in us, 0 means never timeout
65 *
66 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
67 * case, the last read value at @addr is stored in @val.
68 *
69 * When available, you'll probably want to use one of the specialized
70 * macros defined below rather than this macro directly.
71 */
72#define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
73({ \
74 u64 __timeout_us = (timeout_us); \
75 unsigned long __delay_us = (delay_us); \
76 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
77 for (;;) { \
78 (val) = op(addr); \
79 if (cond) \
80 break; \
81 if (__timeout_us && \
82 ktime_compare(ktime_get(), __timeout) > 0) { \
83 (val) = op(addr); \
84 break; \
85 } \
86 if (__delay_us) \
87 udelay(__delay_us); \
88 } \
89 (cond) ? 0 : -ETIMEDOUT; \
90})
91
92
93#define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
94 readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
95
96#define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
97 readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
98
99#define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
100 readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
101
102#define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
103 readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
104
105#define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
106 readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
107
108#define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
109 readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
110
111#define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
112 readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
113
114#define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
115 readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
116
117#define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
118 readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
119
120#define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
121 readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
122
123#define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
124 readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
125
126#define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
127 readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
128
129#define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
130 readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
131
132#define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
133 readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
134
135#define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
136 readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
137
138#define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
139 readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
140
141#endif /* _LINUX_IOPOLL_H */