18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright © 2016 Intel Corporation
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next
128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
138c2ecf20Sopenharmony_ci * Software.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
208c2ecf20Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
218c2ecf20Sopenharmony_ci * IN THE SOFTWARE.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#ifndef __I915_UTILS_H
268c2ecf20Sopenharmony_ci#define __I915_UTILS_H
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/list.h>
298c2ecf20Sopenharmony_ci#include <linux/overflow.h>
308c2ecf20Sopenharmony_ci#include <linux/sched.h>
318c2ecf20Sopenharmony_ci#include <linux/types.h>
328c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct drm_i915_private;
358c2ecf20Sopenharmony_cistruct timer_list;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define FDO_BUG_URL "https://gitlab.freedesktop.org/drm/intel/-/wikis/How-to-file-i915-bugs"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#undef WARN_ON
408c2ecf20Sopenharmony_ci/* Many gcc seem to no see through this and fall over :( */
418c2ecf20Sopenharmony_ci#if 0
428c2ecf20Sopenharmony_ci#define WARN_ON(x) ({ \
438c2ecf20Sopenharmony_ci	bool __i915_warn_cond = (x); \
448c2ecf20Sopenharmony_ci	if (__builtin_constant_p(__i915_warn_cond)) \
458c2ecf20Sopenharmony_ci		BUILD_BUG_ON(__i915_warn_cond); \
468c2ecf20Sopenharmony_ci	WARN(__i915_warn_cond, "WARN_ON(" #x ")"); })
478c2ecf20Sopenharmony_ci#else
488c2ecf20Sopenharmony_ci#define WARN_ON(x) WARN((x), "%s", "WARN_ON(" __stringify(x) ")")
498c2ecf20Sopenharmony_ci#endif
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#undef WARN_ON_ONCE
528c2ecf20Sopenharmony_ci#define WARN_ON_ONCE(x) WARN_ONCE((x), "%s", "WARN_ON_ONCE(" __stringify(x) ")")
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
558c2ecf20Sopenharmony_ci			     __stringify(x), (long)(x))
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_civoid __printf(3, 4)
588c2ecf20Sopenharmony_ci__i915_printk(struct drm_i915_private *dev_priv, const char *level,
598c2ecf20Sopenharmony_ci	      const char *fmt, ...);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define i915_report_error(dev_priv, fmt, ...)				   \
628c2ecf20Sopenharmony_ci	__i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciint __i915_inject_probe_error(struct drm_i915_private *i915, int err,
678c2ecf20Sopenharmony_ci			      const char *func, int line);
688c2ecf20Sopenharmony_ci#define i915_inject_probe_error(_i915, _err) \
698c2ecf20Sopenharmony_ci	__i915_inject_probe_error((_i915), (_err), __func__, __LINE__)
708c2ecf20Sopenharmony_cibool i915_error_injected(void);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#else
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; })
758c2ecf20Sopenharmony_ci#define i915_error_injected() false
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#endif
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV)
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define i915_probe_error(i915, fmt, ...)				   \
828c2ecf20Sopenharmony_ci	__i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \
838c2ecf20Sopenharmony_ci		      fmt, ##__VA_ARGS__)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#if defined(GCC_VERSION) && GCC_VERSION >= 70000
868c2ecf20Sopenharmony_ci#define add_overflows_t(T, A, B) \
878c2ecf20Sopenharmony_ci	__builtin_add_overflow_p((A), (B), (T)0)
888c2ecf20Sopenharmony_ci#else
898c2ecf20Sopenharmony_ci#define add_overflows_t(T, A, B) ({ \
908c2ecf20Sopenharmony_ci	typeof(A) a = (A); \
918c2ecf20Sopenharmony_ci	typeof(B) b = (B); \
928c2ecf20Sopenharmony_ci	(T)(a + b) < a; \
938c2ecf20Sopenharmony_ci})
948c2ecf20Sopenharmony_ci#endif
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define add_overflows(A, B) \
978c2ecf20Sopenharmony_ci	add_overflows_t(typeof((A) + (B)), (A), (B))
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define range_overflows(start, size, max) ({ \
1008c2ecf20Sopenharmony_ci	typeof(start) start__ = (start); \
1018c2ecf20Sopenharmony_ci	typeof(size) size__ = (size); \
1028c2ecf20Sopenharmony_ci	typeof(max) max__ = (max); \
1038c2ecf20Sopenharmony_ci	(void)(&start__ == &size__); \
1048c2ecf20Sopenharmony_ci	(void)(&start__ == &max__); \
1058c2ecf20Sopenharmony_ci	start__ >= max__ || size__ > max__ - start__; \
1068c2ecf20Sopenharmony_ci})
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define range_overflows_t(type, start, size, max) \
1098c2ecf20Sopenharmony_ci	range_overflows((type)(start), (type)(size), (type)(max))
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define range_overflows_end(start, size, max) ({ \
1128c2ecf20Sopenharmony_ci	typeof(start) start__ = (start); \
1138c2ecf20Sopenharmony_ci	typeof(size) size__ = (size); \
1148c2ecf20Sopenharmony_ci	typeof(max) max__ = (max); \
1158c2ecf20Sopenharmony_ci	(void)(&start__ == &size__); \
1168c2ecf20Sopenharmony_ci	(void)(&start__ == &max__); \
1178c2ecf20Sopenharmony_ci	start__ > max__ || size__ > max__ - start__; \
1188c2ecf20Sopenharmony_ci})
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define range_overflows_end_t(type, start, size, max) \
1218c2ecf20Sopenharmony_ci	range_overflows_end((type)(start), (type)(size), (type)(max))
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* Note we don't consider signbits :| */
1248c2ecf20Sopenharmony_ci#define overflows_type(x, T) \
1258c2ecf20Sopenharmony_ci	(sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T))
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic inline bool
1288c2ecf20Sopenharmony_ci__check_struct_size(size_t base, size_t arr, size_t count, size_t *size)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	size_t sz;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (check_mul_overflow(count, arr, &sz))
1338c2ecf20Sopenharmony_ci		return false;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (check_add_overflow(sz, base, &sz))
1368c2ecf20Sopenharmony_ci		return false;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	*size = sz;
1398c2ecf20Sopenharmony_ci	return true;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * check_struct_size() - Calculate size of structure with trailing array.
1448c2ecf20Sopenharmony_ci * @p: Pointer to the structure.
1458c2ecf20Sopenharmony_ci * @member: Name of the array member.
1468c2ecf20Sopenharmony_ci * @n: Number of elements in the array.
1478c2ecf20Sopenharmony_ci * @sz: Total size of structure and array
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci * Calculates size of memory needed for structure @p followed by an
1508c2ecf20Sopenharmony_ci * array of @n @member elements, like struct_size() but reports
1518c2ecf20Sopenharmony_ci * whether it overflowed, and the resultant size in @sz
1528c2ecf20Sopenharmony_ci *
1538c2ecf20Sopenharmony_ci * Return: false if the calculation overflowed.
1548c2ecf20Sopenharmony_ci */
1558c2ecf20Sopenharmony_ci#define check_struct_size(p, member, n, sz) \
1568c2ecf20Sopenharmony_ci	likely(__check_struct_size(sizeof(*(p)), \
1578c2ecf20Sopenharmony_ci				   sizeof(*(p)->member) + __must_be_array((p)->member), \
1588c2ecf20Sopenharmony_ci				   n, sz))
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define ptr_mask_bits(ptr, n) ({					\
1618c2ecf20Sopenharmony_ci	unsigned long __v = (unsigned long)(ptr);			\
1628c2ecf20Sopenharmony_ci	(typeof(ptr))(__v & -BIT(n));					\
1638c2ecf20Sopenharmony_ci})
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci#define ptr_unpack_bits(ptr, bits, n) ({				\
1688c2ecf20Sopenharmony_ci	unsigned long __v = (unsigned long)(ptr);			\
1698c2ecf20Sopenharmony_ci	*(bits) = __v & (BIT(n) - 1);					\
1708c2ecf20Sopenharmony_ci	(typeof(ptr))(__v & -BIT(n));					\
1718c2ecf20Sopenharmony_ci})
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#define ptr_pack_bits(ptr, bits, n) ({					\
1748c2ecf20Sopenharmony_ci	unsigned long __bits = (bits);					\
1758c2ecf20Sopenharmony_ci	GEM_BUG_ON(__bits & -BIT(n));					\
1768c2ecf20Sopenharmony_ci	((typeof(ptr))((unsigned long)(ptr) | __bits));			\
1778c2ecf20Sopenharmony_ci})
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#define ptr_dec(ptr) ({							\
1808c2ecf20Sopenharmony_ci	unsigned long __v = (unsigned long)(ptr);			\
1818c2ecf20Sopenharmony_ci	(typeof(ptr))(__v - 1);						\
1828c2ecf20Sopenharmony_ci})
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci#define ptr_inc(ptr) ({							\
1858c2ecf20Sopenharmony_ci	unsigned long __v = (unsigned long)(ptr);			\
1868c2ecf20Sopenharmony_ci	(typeof(ptr))(__v + 1);						\
1878c2ecf20Sopenharmony_ci})
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci#define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
1908c2ecf20Sopenharmony_ci#define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
1918c2ecf20Sopenharmony_ci#define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
1928c2ecf20Sopenharmony_ci#define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci#define struct_member(T, member) (((T *)0)->member)
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#define ptr_offset(ptr, member) offsetof(typeof(*(ptr)), member)
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci#define fetch_and_zero(ptr) ({						\
1998c2ecf20Sopenharmony_ci	typeof(*ptr) __T = *(ptr);					\
2008c2ecf20Sopenharmony_ci	*(ptr) = (typeof(*ptr))0;					\
2018c2ecf20Sopenharmony_ci	__T;								\
2028c2ecf20Sopenharmony_ci})
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/*
2058c2ecf20Sopenharmony_ci * container_of_user: Extract the superclass from a pointer to a member.
2068c2ecf20Sopenharmony_ci *
2078c2ecf20Sopenharmony_ci * Exactly like container_of() with the exception that it plays nicely
2088c2ecf20Sopenharmony_ci * with sparse for __user @ptr.
2098c2ecf20Sopenharmony_ci */
2108c2ecf20Sopenharmony_ci#define container_of_user(ptr, type, member) ({				\
2118c2ecf20Sopenharmony_ci	void __user *__mptr = (void __user *)(ptr);			\
2128c2ecf20Sopenharmony_ci	BUILD_BUG_ON_MSG(!__same_type(*(ptr), struct_member(type, member)) && \
2138c2ecf20Sopenharmony_ci			 !__same_type(*(ptr), void),			\
2148c2ecf20Sopenharmony_ci			 "pointer type mismatch in container_of()");	\
2158c2ecf20Sopenharmony_ci	((type __user *)(__mptr - offsetof(type, member))); })
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/*
2188c2ecf20Sopenharmony_ci * check_user_mbz: Check that a user value exists and is zero
2198c2ecf20Sopenharmony_ci *
2208c2ecf20Sopenharmony_ci * Frequently in our uABI we reserve space for future extensions, and
2218c2ecf20Sopenharmony_ci * two ensure that userspace is prepared we enforce that space must
2228c2ecf20Sopenharmony_ci * be zero. (Then any future extension can safely assume a default value
2238c2ecf20Sopenharmony_ci * of 0.)
2248c2ecf20Sopenharmony_ci *
2258c2ecf20Sopenharmony_ci * check_user_mbz() combines checking that the user pointer is accessible
2268c2ecf20Sopenharmony_ci * and that the contained value is zero.
2278c2ecf20Sopenharmony_ci *
2288c2ecf20Sopenharmony_ci * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
2298c2ecf20Sopenharmony_ci */
2308c2ecf20Sopenharmony_ci#define check_user_mbz(U) ({						\
2318c2ecf20Sopenharmony_ci	typeof(*(U)) mbz__;						\
2328c2ecf20Sopenharmony_ci	get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0;		\
2338c2ecf20Sopenharmony_ci})
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic inline u64 ptr_to_u64(const void *ptr)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	return (uintptr_t)ptr;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci#define u64_to_ptr(T, x) ({						\
2418c2ecf20Sopenharmony_ci	typecheck(u64, x);						\
2428c2ecf20Sopenharmony_ci	(T *)(uintptr_t)(x);						\
2438c2ecf20Sopenharmony_ci})
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci#define __mask_next_bit(mask) ({					\
2468c2ecf20Sopenharmony_ci	int __idx = ffs(mask) - 1;					\
2478c2ecf20Sopenharmony_ci	mask &= ~BIT(__idx);						\
2488c2ecf20Sopenharmony_ci	__idx;								\
2498c2ecf20Sopenharmony_ci})
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic inline bool is_power_of_2_u64(u64 n)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	return (n != 0 && ((n & (n - 1)) == 0));
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic inline void __list_del_many(struct list_head *head,
2578c2ecf20Sopenharmony_ci				   struct list_head *first)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	first->prev = head;
2608c2ecf20Sopenharmony_ci	WRITE_ONCE(head->next, first);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic inline int list_is_last_rcu(const struct list_head *list,
2648c2ecf20Sopenharmony_ci				   const struct list_head *head)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	return READ_ONCE(list->next) == head;
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	unsigned long j = msecs_to_jiffies(m);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci/*
2778c2ecf20Sopenharmony_ci * If you need to wait X milliseconds between events A and B, but event B
2788c2ecf20Sopenharmony_ci * doesn't happen exactly after event A, you record the timestamp (jiffies) of
2798c2ecf20Sopenharmony_ci * when event A happened, then just before event B you call this function and
2808c2ecf20Sopenharmony_ci * pass the timestamp as the first argument, and X as the second argument.
2818c2ecf20Sopenharmony_ci */
2828c2ecf20Sopenharmony_cistatic inline void
2838c2ecf20Sopenharmony_ciwait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/*
2888c2ecf20Sopenharmony_ci	 * Don't re-read the value of "jiffies" every time since it may change
2898c2ecf20Sopenharmony_ci	 * behind our back and break the math.
2908c2ecf20Sopenharmony_ci	 */
2918c2ecf20Sopenharmony_ci	tmp_jiffies = jiffies;
2928c2ecf20Sopenharmony_ci	target_jiffies = timestamp_jiffies +
2938c2ecf20Sopenharmony_ci			 msecs_to_jiffies_timeout(to_wait_ms);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (time_after(target_jiffies, tmp_jiffies)) {
2968c2ecf20Sopenharmony_ci		remaining_jiffies = target_jiffies - tmp_jiffies;
2978c2ecf20Sopenharmony_ci		while (remaining_jiffies)
2988c2ecf20Sopenharmony_ci			remaining_jiffies =
2998c2ecf20Sopenharmony_ci			    schedule_timeout_uninterruptible(remaining_jiffies);
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci/**
3048c2ecf20Sopenharmony_ci * __wait_for - magic wait macro
3058c2ecf20Sopenharmony_ci *
3068c2ecf20Sopenharmony_ci * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
3078c2ecf20Sopenharmony_ci * important that we check the condition again after having timed out, since the
3088c2ecf20Sopenharmony_ci * timeout could be due to preemption or similar and we've never had a chance to
3098c2ecf20Sopenharmony_ci * check the condition before the timeout.
3108c2ecf20Sopenharmony_ci */
3118c2ecf20Sopenharmony_ci#define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
3128c2ecf20Sopenharmony_ci	const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
3138c2ecf20Sopenharmony_ci	long wait__ = (Wmin); /* recommended min for usleep is 10 us */	\
3148c2ecf20Sopenharmony_ci	int ret__;							\
3158c2ecf20Sopenharmony_ci	might_sleep();							\
3168c2ecf20Sopenharmony_ci	for (;;) {							\
3178c2ecf20Sopenharmony_ci		const bool expired__ = ktime_after(ktime_get_raw(), end__); \
3188c2ecf20Sopenharmony_ci		OP;							\
3198c2ecf20Sopenharmony_ci		/* Guarantee COND check prior to timeout */		\
3208c2ecf20Sopenharmony_ci		barrier();						\
3218c2ecf20Sopenharmony_ci		if (COND) {						\
3228c2ecf20Sopenharmony_ci			ret__ = 0;					\
3238c2ecf20Sopenharmony_ci			break;						\
3248c2ecf20Sopenharmony_ci		}							\
3258c2ecf20Sopenharmony_ci		if (expired__) {					\
3268c2ecf20Sopenharmony_ci			ret__ = -ETIMEDOUT;				\
3278c2ecf20Sopenharmony_ci			break;						\
3288c2ecf20Sopenharmony_ci		}							\
3298c2ecf20Sopenharmony_ci		usleep_range(wait__, wait__ * 2);			\
3308c2ecf20Sopenharmony_ci		if (wait__ < (Wmax))					\
3318c2ecf20Sopenharmony_ci			wait__ <<= 1;					\
3328c2ecf20Sopenharmony_ci	}								\
3338c2ecf20Sopenharmony_ci	ret__;								\
3348c2ecf20Sopenharmony_ci})
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci#define _wait_for(COND, US, Wmin, Wmax)	__wait_for(, (COND), (US), (Wmin), \
3378c2ecf20Sopenharmony_ci						   (Wmax))
3388c2ecf20Sopenharmony_ci#define wait_for(COND, MS)		_wait_for((COND), (MS) * 1000, 10, 1000)
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci/* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
3418c2ecf20Sopenharmony_ci#if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
3428c2ecf20Sopenharmony_ci# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
3438c2ecf20Sopenharmony_ci#else
3448c2ecf20Sopenharmony_ci# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
3458c2ecf20Sopenharmony_ci#endif
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci#define _wait_for_atomic(COND, US, ATOMIC) \
3488c2ecf20Sopenharmony_ci({ \
3498c2ecf20Sopenharmony_ci	int cpu, ret, timeout = (US) * 1000; \
3508c2ecf20Sopenharmony_ci	u64 base; \
3518c2ecf20Sopenharmony_ci	_WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
3528c2ecf20Sopenharmony_ci	if (!(ATOMIC)) { \
3538c2ecf20Sopenharmony_ci		preempt_disable(); \
3548c2ecf20Sopenharmony_ci		cpu = smp_processor_id(); \
3558c2ecf20Sopenharmony_ci	} \
3568c2ecf20Sopenharmony_ci	base = local_clock(); \
3578c2ecf20Sopenharmony_ci	for (;;) { \
3588c2ecf20Sopenharmony_ci		u64 now = local_clock(); \
3598c2ecf20Sopenharmony_ci		if (!(ATOMIC)) \
3608c2ecf20Sopenharmony_ci			preempt_enable(); \
3618c2ecf20Sopenharmony_ci		/* Guarantee COND check prior to timeout */ \
3628c2ecf20Sopenharmony_ci		barrier(); \
3638c2ecf20Sopenharmony_ci		if (COND) { \
3648c2ecf20Sopenharmony_ci			ret = 0; \
3658c2ecf20Sopenharmony_ci			break; \
3668c2ecf20Sopenharmony_ci		} \
3678c2ecf20Sopenharmony_ci		if (now - base >= timeout) { \
3688c2ecf20Sopenharmony_ci			ret = -ETIMEDOUT; \
3698c2ecf20Sopenharmony_ci			break; \
3708c2ecf20Sopenharmony_ci		} \
3718c2ecf20Sopenharmony_ci		cpu_relax(); \
3728c2ecf20Sopenharmony_ci		if (!(ATOMIC)) { \
3738c2ecf20Sopenharmony_ci			preempt_disable(); \
3748c2ecf20Sopenharmony_ci			if (unlikely(cpu != smp_processor_id())) { \
3758c2ecf20Sopenharmony_ci				timeout -= now - base; \
3768c2ecf20Sopenharmony_ci				cpu = smp_processor_id(); \
3778c2ecf20Sopenharmony_ci				base = local_clock(); \
3788c2ecf20Sopenharmony_ci			} \
3798c2ecf20Sopenharmony_ci		} \
3808c2ecf20Sopenharmony_ci	} \
3818c2ecf20Sopenharmony_ci	ret; \
3828c2ecf20Sopenharmony_ci})
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci#define wait_for_us(COND, US) \
3858c2ecf20Sopenharmony_ci({ \
3868c2ecf20Sopenharmony_ci	int ret__; \
3878c2ecf20Sopenharmony_ci	BUILD_BUG_ON(!__builtin_constant_p(US)); \
3888c2ecf20Sopenharmony_ci	if ((US) > 10) \
3898c2ecf20Sopenharmony_ci		ret__ = _wait_for((COND), (US), 10, 10); \
3908c2ecf20Sopenharmony_ci	else \
3918c2ecf20Sopenharmony_ci		ret__ = _wait_for_atomic((COND), (US), 0); \
3928c2ecf20Sopenharmony_ci	ret__; \
3938c2ecf20Sopenharmony_ci})
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci#define wait_for_atomic_us(COND, US) \
3968c2ecf20Sopenharmony_ci({ \
3978c2ecf20Sopenharmony_ci	BUILD_BUG_ON(!__builtin_constant_p(US)); \
3988c2ecf20Sopenharmony_ci	BUILD_BUG_ON((US) > 50000); \
3998c2ecf20Sopenharmony_ci	_wait_for_atomic((COND), (US), 1); \
4008c2ecf20Sopenharmony_ci})
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci#define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci#define KHz(x) (1000 * (x))
4058c2ecf20Sopenharmony_ci#define MHz(x) KHz(1000 * (x))
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci#define KBps(x) (1000 * (x))
4088c2ecf20Sopenharmony_ci#define MBps(x) KBps(1000 * (x))
4098c2ecf20Sopenharmony_ci#define GBps(x) ((u64)1000 * MBps((x)))
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic inline const char *yesno(bool v)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	return v ? "yes" : "no";
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic inline const char *onoff(bool v)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	return v ? "on" : "off";
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic inline const char *enableddisabled(bool v)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	return v ? "enabled" : "disabled";
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_civoid add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
4278c2ecf20Sopenharmony_cistatic inline void __add_taint_for_CI(unsigned int taint)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	/*
4308c2ecf20Sopenharmony_ci	 * The system is "ok", just about surviving for the user, but
4318c2ecf20Sopenharmony_ci	 * CI results are now unreliable as the HW is very suspect.
4328c2ecf20Sopenharmony_ci	 * CI checks the taint state after every test and will reboot
4338c2ecf20Sopenharmony_ci	 * the machine if the kernel is tainted.
4348c2ecf20Sopenharmony_ci	 */
4358c2ecf20Sopenharmony_ci	add_taint(taint, LOCKDEP_STILL_OK);
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_civoid cancel_timer(struct timer_list *t);
4398c2ecf20Sopenharmony_civoid set_timer_ms(struct timer_list *t, unsigned long timeout);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistatic inline bool timer_expired(const struct timer_list *t)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	return READ_ONCE(t->expires) && !timer_pending(t);
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci/*
4478c2ecf20Sopenharmony_ci * This is a lookalike for IS_ENABLED() that takes a kconfig value,
4488c2ecf20Sopenharmony_ci * e.g. CONFIG_DRM_I915_SPIN_REQUEST, and evaluates whether it is non-zero
4498c2ecf20Sopenharmony_ci * i.e. whether the configuration is active. Wrapping up the config inside
4508c2ecf20Sopenharmony_ci * a boolean context prevents clang and smatch from complaining about potential
4518c2ecf20Sopenharmony_ci * issues in confusing logical-&& with bitwise-& for constants.
4528c2ecf20Sopenharmony_ci *
4538c2ecf20Sopenharmony_ci * Sadly IS_ENABLED() itself does not work with kconfig values.
4548c2ecf20Sopenharmony_ci *
4558c2ecf20Sopenharmony_ci * Returns 0 if @config is 0, 1 if set to any value.
4568c2ecf20Sopenharmony_ci */
4578c2ecf20Sopenharmony_ci#define IS_ACTIVE(config) ((config) != 0)
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci#endif /* !__I915_UTILS_H */
460