/* SPDX-License-Identifier: GPL-2.0 */1#ifndef __ASM_GENERIC_DELAY_H2#define __ASM_GENERIC_DELAY_H34#include <linux/math.h>5#include <vdso/time64.h>67/* Undefined functions to get compile-time errors */8extern void __bad_udelay(void);9extern void __bad_ndelay(void);1011extern void __udelay(unsigned long usecs);12extern void __ndelay(unsigned long nsecs);13extern void __const_udelay(unsigned long xloops);14extern void __delay(unsigned long loops);1516/*17* The microseconds/nanosecond delay multiplicators are used to convert a18* constant microseconds/nanoseconds value to a value which can be used by the19* architectures specific implementation to transform it into loops.20*/21#define UDELAY_CONST_MULT ((unsigned long)DIV_ROUND_UP(1ULL << 32, USEC_PER_SEC))22#define NDELAY_CONST_MULT ((unsigned long)DIV_ROUND_UP(1ULL << 32, NSEC_PER_SEC))2324/*25* The maximum constant udelay/ndelay value picked out of thin air to prevent26* too long constant udelays/ndelays.27*/28#define DELAY_CONST_MAX 200002930/**31* udelay - Inserting a delay based on microseconds with busy waiting32* @usec: requested delay in microseconds33*34* When delaying in an atomic context ndelay(), udelay() and mdelay() are the35* only valid variants of delaying/sleeping to go with.36*37* When inserting delays in non atomic context which are shorter than the time38* which is required to queue e.g. an hrtimer and to enter then the scheduler,39* it is also valuable to use udelay(). But it is not simple to specify a40* generic threshold for this which will fit for all systems. An approximation41* is a threshold for all delays up to 10 microseconds.42*43* When having a delay which is larger than the architecture specific44* %MAX_UDELAY_MS value, please make sure mdelay() is used. Otherwise a overflow45* risk is given.46*47* Please note that ndelay(), udelay() and mdelay() may return early for several48* reasons (https://lists.openwall.net/linux-kernel/2011/01/09/56):49*50* #. computed loops_per_jiffy too low (due to the time taken to execute the51* timer interrupt.)52* #. cache behaviour affecting the time it takes to execute the loop function.53* #. CPU clock rate changes.54*/55static __always_inline void udelay(unsigned long usec)56{57if (__builtin_constant_p(usec)) {58if (usec >= DELAY_CONST_MAX)59__bad_udelay();60else61__const_udelay(usec * UDELAY_CONST_MULT);62} else {63__udelay(usec);64}65}6667/**68* ndelay - Inserting a delay based on nanoseconds with busy waiting69* @nsec: requested delay in nanoseconds70*71* See udelay() for basic information about ndelay() and it's variants.72*/73static __always_inline void ndelay(unsigned long nsec)74{75if (__builtin_constant_p(nsec)) {76if (nsec >= DELAY_CONST_MAX)77__bad_ndelay();78else79__const_udelay(nsec * NDELAY_CONST_MULT);80} else {81__ndelay(nsec);82}83}84#define ndelay(x) ndelay(x)8586#endif /* __ASM_GENERIC_DELAY_H */878889