Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
0004  */
0005 
0006 #ifndef _LINUX_IOPOLL_H
0007 #define _LINUX_IOPOLL_H
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/types.h>
0011 #include <linux/ktime.h>
0012 #include <linux/delay.h>
0013 #include <linux/errno.h>
0014 #include <linux/io.h>
0015 
0016 /**
0017  * read_poll_timeout - Periodically poll an address until a condition is
0018  *          met or a timeout occurs
0019  * @op: accessor function (takes @args as its arguments)
0020  * @val: Variable to read the value into
0021  * @cond: Break condition (usually involving @val)
0022  * @sleep_us: Maximum time to sleep between reads in us (0
0023  *            tight-loops).  Should be less than ~20ms since usleep_range
0024  *            is used (see Documentation/timers/timers-howto.rst).
0025  * @timeout_us: Timeout in us, 0 means never timeout
0026  * @sleep_before_read: if it is true, sleep @sleep_us before read.
0027  * @args: arguments for @op poll
0028  *
0029  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
0030  * case, the last read value at @args is stored in @val. Must not
0031  * be called from atomic context if sleep_us or timeout_us are used.
0032  *
0033  * When available, you'll probably want to use one of the specialized
0034  * macros defined below rather than this macro directly.
0035  */
0036 #define read_poll_timeout(op, val, cond, sleep_us, timeout_us, \
0037                 sleep_before_read, args...) \
0038 ({ \
0039     u64 __timeout_us = (timeout_us); \
0040     unsigned long __sleep_us = (sleep_us); \
0041     ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
0042     might_sleep_if((__sleep_us) != 0); \
0043     if (sleep_before_read && __sleep_us) \
0044         usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
0045     for (;;) { \
0046         (val) = op(args); \
0047         if (cond) \
0048             break; \
0049         if (__timeout_us && \
0050             ktime_compare(ktime_get(), __timeout) > 0) { \
0051             (val) = op(args); \
0052             break; \
0053         } \
0054         if (__sleep_us) \
0055             usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
0056     } \
0057     (cond) ? 0 : -ETIMEDOUT; \
0058 })
0059 
0060 /**
0061  * read_poll_timeout_atomic - Periodically poll an address until a condition is
0062  *              met or a timeout occurs
0063  * @op: accessor function (takes @args as its arguments)
0064  * @val: Variable to read the value into
0065  * @cond: Break condition (usually involving @val)
0066  * @delay_us: Time to udelay between reads in us (0 tight-loops).  Should
0067  *            be less than ~10us since udelay is used (see
0068  *            Documentation/timers/timers-howto.rst).
0069  * @timeout_us: Timeout in us, 0 means never timeout
0070  * @delay_before_read: if it is true, delay @delay_us before read.
0071  * @args: arguments for @op poll
0072  *
0073  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
0074  * case, the last read value at @args is stored in @val.
0075  *
0076  * When available, you'll probably want to use one of the specialized
0077  * macros defined below rather than this macro directly.
0078  */
0079 #define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
0080                     delay_before_read, args...) \
0081 ({ \
0082     u64 __timeout_us = (timeout_us); \
0083     unsigned long __delay_us = (delay_us); \
0084     ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
0085     if (delay_before_read && __delay_us) \
0086         udelay(__delay_us); \
0087     for (;;) { \
0088         (val) = op(args); \
0089         if (cond) \
0090             break; \
0091         if (__timeout_us && \
0092             ktime_compare(ktime_get(), __timeout) > 0) { \
0093             (val) = op(args); \
0094             break; \
0095         } \
0096         if (__delay_us) \
0097             udelay(__delay_us); \
0098     } \
0099     (cond) ? 0 : -ETIMEDOUT; \
0100 })
0101 
0102 /**
0103  * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
0104  * @op: accessor function (takes @addr as its only argument)
0105  * @addr: Address to poll
0106  * @val: Variable to read the value into
0107  * @cond: Break condition (usually involving @val)
0108  * @sleep_us: Maximum time to sleep between reads in us (0
0109  *            tight-loops).  Should be less than ~20ms since usleep_range
0110  *            is used (see Documentation/timers/timers-howto.rst).
0111  * @timeout_us: Timeout in us, 0 means never timeout
0112  *
0113  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
0114  * case, the last read value at @addr is stored in @val. Must not
0115  * be called from atomic context if sleep_us or timeout_us are used.
0116  *
0117  * When available, you'll probably want to use one of the specialized
0118  * macros defined below rather than this macro directly.
0119  */
0120 #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us)   \
0121     read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
0122 
0123 /**
0124  * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
0125  * @op: accessor function (takes @addr as its only argument)
0126  * @addr: Address to poll
0127  * @val: Variable to read the value into
0128  * @cond: Break condition (usually involving @val)
0129  * @delay_us: Time to udelay between reads in us (0 tight-loops).  Should
0130  *            be less than ~10us since udelay is used (see
0131  *            Documentation/timers/timers-howto.rst).
0132  * @timeout_us: Timeout in us, 0 means never timeout
0133  *
0134  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
0135  * case, the last read value at @addr is stored in @val.
0136  *
0137  * When available, you'll probably want to use one of the specialized
0138  * macros defined below rather than this macro directly.
0139  */
0140 #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
0141     read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
0142 
0143 #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
0144     readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
0145 
0146 #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
0147     readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
0148 
0149 #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
0150     readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
0151 
0152 #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
0153     readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
0154 
0155 #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
0156     readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
0157 
0158 #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
0159     readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
0160 
0161 #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
0162     readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
0163 
0164 #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
0165     readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
0166 
0167 #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
0168     readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
0169 
0170 #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
0171     readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
0172 
0173 #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
0174     readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
0175 
0176 #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
0177     readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
0178 
0179 #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
0180     readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
0181 
0182 #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
0183     readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
0184 
0185 #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
0186     readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
0187 
0188 #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
0189     readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
0190 
0191 #endif /* _LINUX_IOPOLL_H */