162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright © 2016 Intel Corporation
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the next
1262306a36Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
1362306a36Sopenharmony_ci * Software.
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1662306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1762306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1862306a36Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1962306a36Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2062306a36Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2162306a36Sopenharmony_ci * IN THE SOFTWARE.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#ifndef __I915_UTILS_H
2662306a36Sopenharmony_ci#define __I915_UTILS_H
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include <linux/list.h>
2962306a36Sopenharmony_ci#include <linux/overflow.h>
3062306a36Sopenharmony_ci#include <linux/sched.h>
3162306a36Sopenharmony_ci#include <linux/string_helpers.h>
3262306a36Sopenharmony_ci#include <linux/types.h>
3362306a36Sopenharmony_ci#include <linux/workqueue.h>
3462306a36Sopenharmony_ci#include <linux/sched/clock.h>
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#ifdef CONFIG_X86
3762306a36Sopenharmony_ci#include <asm/hypervisor.h>
3862306a36Sopenharmony_ci#endif
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistruct drm_i915_private;
4162306a36Sopenharmony_cistruct timer_list;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci#define FDO_BUG_URL "https://gitlab.freedesktop.org/drm/intel/-/wikis/How-to-file-i915-bugs"
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
4662306a36Sopenharmony_ci			     __stringify(x), (long)(x))
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_civoid __printf(3, 4)
4962306a36Sopenharmony_ci__i915_printk(struct drm_i915_private *dev_priv, const char *level,
5062306a36Sopenharmony_ci	      const char *fmt, ...);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define i915_report_error(dev_priv, fmt, ...)				   \
5362306a36Sopenharmony_ci	__i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciint __i915_inject_probe_error(struct drm_i915_private *i915, int err,
5862306a36Sopenharmony_ci			      const char *func, int line);
5962306a36Sopenharmony_ci#define i915_inject_probe_error(_i915, _err) \
6062306a36Sopenharmony_ci	__i915_inject_probe_error((_i915), (_err), __func__, __LINE__)
6162306a36Sopenharmony_cibool i915_error_injected(void);
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci#else
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci#define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; })
6662306a36Sopenharmony_ci#define i915_error_injected() false
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#endif
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV)
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define i915_probe_error(i915, fmt, ...)				   \
7362306a36Sopenharmony_ci	__i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \
7462306a36Sopenharmony_ci		      fmt, ##__VA_ARGS__)
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#if defined(GCC_VERSION) && GCC_VERSION >= 70000
7762306a36Sopenharmony_ci#define add_overflows_t(T, A, B) \
7862306a36Sopenharmony_ci	__builtin_add_overflow_p((A), (B), (T)0)
7962306a36Sopenharmony_ci#else
8062306a36Sopenharmony_ci#define add_overflows_t(T, A, B) ({ \
8162306a36Sopenharmony_ci	typeof(A) a = (A); \
8262306a36Sopenharmony_ci	typeof(B) b = (B); \
8362306a36Sopenharmony_ci	(T)(a + b) < a; \
8462306a36Sopenharmony_ci})
8562306a36Sopenharmony_ci#endif
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci#define add_overflows(A, B) \
8862306a36Sopenharmony_ci	add_overflows_t(typeof((A) + (B)), (A), (B))
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci#define range_overflows(start, size, max) ({ \
9162306a36Sopenharmony_ci	typeof(start) start__ = (start); \
9262306a36Sopenharmony_ci	typeof(size) size__ = (size); \
9362306a36Sopenharmony_ci	typeof(max) max__ = (max); \
9462306a36Sopenharmony_ci	(void)(&start__ == &size__); \
9562306a36Sopenharmony_ci	(void)(&start__ == &max__); \
9662306a36Sopenharmony_ci	start__ >= max__ || size__ > max__ - start__; \
9762306a36Sopenharmony_ci})
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci#define range_overflows_t(type, start, size, max) \
10062306a36Sopenharmony_ci	range_overflows((type)(start), (type)(size), (type)(max))
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#define range_overflows_end(start, size, max) ({ \
10362306a36Sopenharmony_ci	typeof(start) start__ = (start); \
10462306a36Sopenharmony_ci	typeof(size) size__ = (size); \
10562306a36Sopenharmony_ci	typeof(max) max__ = (max); \
10662306a36Sopenharmony_ci	(void)(&start__ == &size__); \
10762306a36Sopenharmony_ci	(void)(&start__ == &max__); \
10862306a36Sopenharmony_ci	start__ > max__ || size__ > max__ - start__; \
10962306a36Sopenharmony_ci})
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci#define range_overflows_end_t(type, start, size, max) \
11262306a36Sopenharmony_ci	range_overflows_end((type)(start), (type)(size), (type)(max))
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci#define ptr_mask_bits(ptr, n) ({					\
11562306a36Sopenharmony_ci	unsigned long __v = (unsigned long)(ptr);			\
11662306a36Sopenharmony_ci	(typeof(ptr))(__v & -BIT(n));					\
11762306a36Sopenharmony_ci})
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci#define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#define ptr_unpack_bits(ptr, bits, n) ({				\
12262306a36Sopenharmony_ci	unsigned long __v = (unsigned long)(ptr);			\
12362306a36Sopenharmony_ci	*(bits) = __v & (BIT(n) - 1);					\
12462306a36Sopenharmony_ci	(typeof(ptr))(__v & -BIT(n));					\
12562306a36Sopenharmony_ci})
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci#define ptr_pack_bits(ptr, bits, n) ({					\
12862306a36Sopenharmony_ci	unsigned long __bits = (bits);					\
12962306a36Sopenharmony_ci	GEM_BUG_ON(__bits & -BIT(n));					\
13062306a36Sopenharmony_ci	((typeof(ptr))((unsigned long)(ptr) | __bits));			\
13162306a36Sopenharmony_ci})
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci#define ptr_dec(ptr) ({							\
13462306a36Sopenharmony_ci	unsigned long __v = (unsigned long)(ptr);			\
13562306a36Sopenharmony_ci	(typeof(ptr))(__v - 1);						\
13662306a36Sopenharmony_ci})
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci#define ptr_inc(ptr) ({							\
13962306a36Sopenharmony_ci	unsigned long __v = (unsigned long)(ptr);			\
14062306a36Sopenharmony_ci	(typeof(ptr))(__v + 1);						\
14162306a36Sopenharmony_ci})
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci#define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
14462306a36Sopenharmony_ci#define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
14562306a36Sopenharmony_ci#define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
14662306a36Sopenharmony_ci#define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci#define fetch_and_zero(ptr) ({						\
14962306a36Sopenharmony_ci	typeof(*ptr) __T = *(ptr);					\
15062306a36Sopenharmony_ci	*(ptr) = (typeof(*ptr))0;					\
15162306a36Sopenharmony_ci	__T;								\
15262306a36Sopenharmony_ci})
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cistatic __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
15562306a36Sopenharmony_ci{
15662306a36Sopenharmony_ci	return a - b;
15762306a36Sopenharmony_ci}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci/*
16062306a36Sopenharmony_ci * container_of_user: Extract the superclass from a pointer to a member.
16162306a36Sopenharmony_ci *
16262306a36Sopenharmony_ci * Exactly like container_of() with the exception that it plays nicely
16362306a36Sopenharmony_ci * with sparse for __user @ptr.
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_ci#define container_of_user(ptr, type, member) ({				\
16662306a36Sopenharmony_ci	void __user *__mptr = (void __user *)(ptr);			\
16762306a36Sopenharmony_ci	BUILD_BUG_ON_MSG(!__same_type(*(ptr), typeof_member(type, member)) && \
16862306a36Sopenharmony_ci			 !__same_type(*(ptr), void),			\
16962306a36Sopenharmony_ci			 "pointer type mismatch in container_of()");	\
17062306a36Sopenharmony_ci	((type __user *)(__mptr - offsetof(type, member))); })
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/*
17362306a36Sopenharmony_ci * check_user_mbz: Check that a user value exists and is zero
17462306a36Sopenharmony_ci *
17562306a36Sopenharmony_ci * Frequently in our uABI we reserve space for future extensions, and
17662306a36Sopenharmony_ci * two ensure that userspace is prepared we enforce that space must
17762306a36Sopenharmony_ci * be zero. (Then any future extension can safely assume a default value
17862306a36Sopenharmony_ci * of 0.)
17962306a36Sopenharmony_ci *
18062306a36Sopenharmony_ci * check_user_mbz() combines checking that the user pointer is accessible
18162306a36Sopenharmony_ci * and that the contained value is zero.
18262306a36Sopenharmony_ci *
18362306a36Sopenharmony_ci * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
18462306a36Sopenharmony_ci */
18562306a36Sopenharmony_ci#define check_user_mbz(U) ({						\
18662306a36Sopenharmony_ci	typeof(*(U)) mbz__;						\
18762306a36Sopenharmony_ci	get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0;		\
18862306a36Sopenharmony_ci})
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci#define u64_to_ptr(T, x) ({						\
19162306a36Sopenharmony_ci	typecheck(u64, x);						\
19262306a36Sopenharmony_ci	(T *)(uintptr_t)(x);						\
19362306a36Sopenharmony_ci})
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci#define __mask_next_bit(mask) ({					\
19662306a36Sopenharmony_ci	int __idx = ffs(mask) - 1;					\
19762306a36Sopenharmony_ci	mask &= ~BIT(__idx);						\
19862306a36Sopenharmony_ci	__idx;								\
19962306a36Sopenharmony_ci})
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistatic inline bool is_power_of_2_u64(u64 n)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	return (n != 0 && ((n & (n - 1)) == 0));
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistatic inline void __list_del_many(struct list_head *head,
20762306a36Sopenharmony_ci				   struct list_head *first)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	first->prev = head;
21062306a36Sopenharmony_ci	WRITE_ONCE(head->next, first);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic inline int list_is_last_rcu(const struct list_head *list,
21462306a36Sopenharmony_ci				   const struct list_head *head)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	return READ_ONCE(list->next) == head;
21762306a36Sopenharmony_ci}
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_cistatic inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
22062306a36Sopenharmony_ci{
22162306a36Sopenharmony_ci	unsigned long j = msecs_to_jiffies(m);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
22462306a36Sopenharmony_ci}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci/*
22762306a36Sopenharmony_ci * If you need to wait X milliseconds between events A and B, but event B
22862306a36Sopenharmony_ci * doesn't happen exactly after event A, you record the timestamp (jiffies) of
22962306a36Sopenharmony_ci * when event A happened, then just before event B you call this function and
23062306a36Sopenharmony_ci * pass the timestamp as the first argument, and X as the second argument.
23162306a36Sopenharmony_ci */
23262306a36Sopenharmony_cistatic inline void
23362306a36Sopenharmony_ciwait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
23462306a36Sopenharmony_ci{
23562306a36Sopenharmony_ci	unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	/*
23862306a36Sopenharmony_ci	 * Don't re-read the value of "jiffies" every time since it may change
23962306a36Sopenharmony_ci	 * behind our back and break the math.
24062306a36Sopenharmony_ci	 */
24162306a36Sopenharmony_ci	tmp_jiffies = jiffies;
24262306a36Sopenharmony_ci	target_jiffies = timestamp_jiffies +
24362306a36Sopenharmony_ci			 msecs_to_jiffies_timeout(to_wait_ms);
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	if (time_after(target_jiffies, tmp_jiffies)) {
24662306a36Sopenharmony_ci		remaining_jiffies = target_jiffies - tmp_jiffies;
24762306a36Sopenharmony_ci		while (remaining_jiffies)
24862306a36Sopenharmony_ci			remaining_jiffies =
24962306a36Sopenharmony_ci			    schedule_timeout_uninterruptible(remaining_jiffies);
25062306a36Sopenharmony_ci	}
25162306a36Sopenharmony_ci}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci/*
25462306a36Sopenharmony_ci * __wait_for - magic wait macro
25562306a36Sopenharmony_ci *
25662306a36Sopenharmony_ci * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
25762306a36Sopenharmony_ci * important that we check the condition again after having timed out, since the
25862306a36Sopenharmony_ci * timeout could be due to preemption or similar and we've never had a chance to
25962306a36Sopenharmony_ci * check the condition before the timeout.
26062306a36Sopenharmony_ci */
26162306a36Sopenharmony_ci#define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
26262306a36Sopenharmony_ci	const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
26362306a36Sopenharmony_ci	long wait__ = (Wmin); /* recommended min for usleep is 10 us */	\
26462306a36Sopenharmony_ci	int ret__;							\
26562306a36Sopenharmony_ci	might_sleep();							\
26662306a36Sopenharmony_ci	for (;;) {							\
26762306a36Sopenharmony_ci		const bool expired__ = ktime_after(ktime_get_raw(), end__); \
26862306a36Sopenharmony_ci		OP;							\
26962306a36Sopenharmony_ci		/* Guarantee COND check prior to timeout */		\
27062306a36Sopenharmony_ci		barrier();						\
27162306a36Sopenharmony_ci		if (COND) {						\
27262306a36Sopenharmony_ci			ret__ = 0;					\
27362306a36Sopenharmony_ci			break;						\
27462306a36Sopenharmony_ci		}							\
27562306a36Sopenharmony_ci		if (expired__) {					\
27662306a36Sopenharmony_ci			ret__ = -ETIMEDOUT;				\
27762306a36Sopenharmony_ci			break;						\
27862306a36Sopenharmony_ci		}							\
27962306a36Sopenharmony_ci		usleep_range(wait__, wait__ * 2);			\
28062306a36Sopenharmony_ci		if (wait__ < (Wmax))					\
28162306a36Sopenharmony_ci			wait__ <<= 1;					\
28262306a36Sopenharmony_ci	}								\
28362306a36Sopenharmony_ci	ret__;								\
28462306a36Sopenharmony_ci})
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci#define _wait_for(COND, US, Wmin, Wmax)	__wait_for(, (COND), (US), (Wmin), \
28762306a36Sopenharmony_ci						   (Wmax))
28862306a36Sopenharmony_ci#define wait_for(COND, MS)		_wait_for((COND), (MS) * 1000, 10, 1000)
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci/* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
29162306a36Sopenharmony_ci#if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
29262306a36Sopenharmony_ci# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
29362306a36Sopenharmony_ci#else
29462306a36Sopenharmony_ci# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
29562306a36Sopenharmony_ci#endif
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci#define _wait_for_atomic(COND, US, ATOMIC) \
29862306a36Sopenharmony_ci({ \
29962306a36Sopenharmony_ci	int cpu, ret, timeout = (US) * 1000; \
30062306a36Sopenharmony_ci	u64 base; \
30162306a36Sopenharmony_ci	_WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
30262306a36Sopenharmony_ci	if (!(ATOMIC)) { \
30362306a36Sopenharmony_ci		preempt_disable(); \
30462306a36Sopenharmony_ci		cpu = smp_processor_id(); \
30562306a36Sopenharmony_ci	} \
30662306a36Sopenharmony_ci	base = local_clock(); \
30762306a36Sopenharmony_ci	for (;;) { \
30862306a36Sopenharmony_ci		u64 now = local_clock(); \
30962306a36Sopenharmony_ci		if (!(ATOMIC)) \
31062306a36Sopenharmony_ci			preempt_enable(); \
31162306a36Sopenharmony_ci		/* Guarantee COND check prior to timeout */ \
31262306a36Sopenharmony_ci		barrier(); \
31362306a36Sopenharmony_ci		if (COND) { \
31462306a36Sopenharmony_ci			ret = 0; \
31562306a36Sopenharmony_ci			break; \
31662306a36Sopenharmony_ci		} \
31762306a36Sopenharmony_ci		if (now - base >= timeout) { \
31862306a36Sopenharmony_ci			ret = -ETIMEDOUT; \
31962306a36Sopenharmony_ci			break; \
32062306a36Sopenharmony_ci		} \
32162306a36Sopenharmony_ci		cpu_relax(); \
32262306a36Sopenharmony_ci		if (!(ATOMIC)) { \
32362306a36Sopenharmony_ci			preempt_disable(); \
32462306a36Sopenharmony_ci			if (unlikely(cpu != smp_processor_id())) { \
32562306a36Sopenharmony_ci				timeout -= now - base; \
32662306a36Sopenharmony_ci				cpu = smp_processor_id(); \
32762306a36Sopenharmony_ci				base = local_clock(); \
32862306a36Sopenharmony_ci			} \
32962306a36Sopenharmony_ci		} \
33062306a36Sopenharmony_ci	} \
33162306a36Sopenharmony_ci	ret; \
33262306a36Sopenharmony_ci})
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci#define wait_for_us(COND, US) \
33562306a36Sopenharmony_ci({ \
33662306a36Sopenharmony_ci	int ret__; \
33762306a36Sopenharmony_ci	BUILD_BUG_ON(!__builtin_constant_p(US)); \
33862306a36Sopenharmony_ci	if ((US) > 10) \
33962306a36Sopenharmony_ci		ret__ = _wait_for((COND), (US), 10, 10); \
34062306a36Sopenharmony_ci	else \
34162306a36Sopenharmony_ci		ret__ = _wait_for_atomic((COND), (US), 0); \
34262306a36Sopenharmony_ci	ret__; \
34362306a36Sopenharmony_ci})
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci#define wait_for_atomic_us(COND, US) \
34662306a36Sopenharmony_ci({ \
34762306a36Sopenharmony_ci	BUILD_BUG_ON(!__builtin_constant_p(US)); \
34862306a36Sopenharmony_ci	BUILD_BUG_ON((US) > 50000); \
34962306a36Sopenharmony_ci	_wait_for_atomic((COND), (US), 1); \
35062306a36Sopenharmony_ci})
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci#define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci#define KHz(x) (1000 * (x))
35562306a36Sopenharmony_ci#define MHz(x) KHz(1000 * (x))
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_civoid add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
35862306a36Sopenharmony_cistatic inline void __add_taint_for_CI(unsigned int taint)
35962306a36Sopenharmony_ci{
36062306a36Sopenharmony_ci	/*
36162306a36Sopenharmony_ci	 * The system is "ok", just about surviving for the user, but
36262306a36Sopenharmony_ci	 * CI results are now unreliable as the HW is very suspect.
36362306a36Sopenharmony_ci	 * CI checks the taint state after every test and will reboot
36462306a36Sopenharmony_ci	 * the machine if the kernel is tainted.
36562306a36Sopenharmony_ci	 */
36662306a36Sopenharmony_ci	add_taint(taint, LOCKDEP_STILL_OK);
36762306a36Sopenharmony_ci}
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_civoid cancel_timer(struct timer_list *t);
37062306a36Sopenharmony_civoid set_timer_ms(struct timer_list *t, unsigned long timeout);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_cistatic inline bool timer_active(const struct timer_list *t)
37362306a36Sopenharmony_ci{
37462306a36Sopenharmony_ci	return READ_ONCE(t->expires);
37562306a36Sopenharmony_ci}
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_cistatic inline bool timer_expired(const struct timer_list *t)
37862306a36Sopenharmony_ci{
37962306a36Sopenharmony_ci	return timer_active(t) && !timer_pending(t);
38062306a36Sopenharmony_ci}
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_cistatic inline bool i915_run_as_guest(void)
38362306a36Sopenharmony_ci{
38462306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_X86)
38562306a36Sopenharmony_ci	return !hypervisor_is_type(X86_HYPER_NATIVE);
38662306a36Sopenharmony_ci#else
38762306a36Sopenharmony_ci	/* Not supported yet */
38862306a36Sopenharmony_ci	return false;
38962306a36Sopenharmony_ci#endif
39062306a36Sopenharmony_ci}
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_cibool i915_vtd_active(struct drm_i915_private *i915);
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci#endif /* !__I915_UTILS_H */
395