xref: /linux/drivers/gpu/drm/i915/i915_utils.h (revision 2920516b2f719546f55079bc39a7fe409d9e80ab)
140b326eeSChris Wilson /*
240b326eeSChris Wilson  * Copyright © 2016 Intel Corporation
340b326eeSChris Wilson  *
440b326eeSChris Wilson  * Permission is hereby granted, free of charge, to any person obtaining a
540b326eeSChris Wilson  * copy of this software and associated documentation files (the "Software"),
640b326eeSChris Wilson  * to deal in the Software without restriction, including without limitation
740b326eeSChris Wilson  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
840b326eeSChris Wilson  * and/or sell copies of the Software, and to permit persons to whom the
940b326eeSChris Wilson  * Software is furnished to do so, subject to the following conditions:
1040b326eeSChris Wilson  *
1140b326eeSChris Wilson  * The above copyright notice and this permission notice (including the next
1240b326eeSChris Wilson  * paragraph) shall be included in all copies or substantial portions of the
1340b326eeSChris Wilson  * Software.
1440b326eeSChris Wilson  *
1540b326eeSChris Wilson  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1640b326eeSChris Wilson  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1740b326eeSChris Wilson  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1840b326eeSChris Wilson  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1940b326eeSChris Wilson  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2040b326eeSChris Wilson  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2140b326eeSChris Wilson  * IN THE SOFTWARE.
2240b326eeSChris Wilson  *
2340b326eeSChris Wilson  */
2440b326eeSChris Wilson 
2540b326eeSChris Wilson #ifndef __I915_UTILS_H
2640b326eeSChris Wilson #define __I915_UTILS_H
2740b326eeSChris Wilson 
28cb363304SJani Nikula #include <linux/list.h>
29976b55f0SChris Wilson #include <linux/overflow.h>
30b30ed4ccSJani Nikula #include <linux/sched.h>
31cb363304SJani Nikula #include <linux/types.h>
32cb363304SJani Nikula #include <linux/workqueue.h>
33cb363304SJani Nikula 
34358c855cSJani Nikula struct drm_i915_private;
353a7a92abSChris Wilson struct timer_list;
36358c855cSJani Nikula 
3717f5d579SLyude Paul #define FDO_BUG_URL "https://gitlab.freedesktop.org/drm/intel/-/wikis/How-to-file-i915-bugs"
3817f5d579SLyude Paul 
39f0d66153SMichal Wajdeczko #undef WARN_ON
40f0d66153SMichal Wajdeczko /* Many gcc seem to no see through this and fall over :( */
41f0d66153SMichal Wajdeczko #if 0
42f0d66153SMichal Wajdeczko #define WARN_ON(x) ({ \
43f0d66153SMichal Wajdeczko 	bool __i915_warn_cond = (x); \
44f0d66153SMichal Wajdeczko 	if (__builtin_constant_p(__i915_warn_cond)) \
45f0d66153SMichal Wajdeczko 		BUILD_BUG_ON(__i915_warn_cond); \
46f0d66153SMichal Wajdeczko 	WARN(__i915_warn_cond, "WARN_ON(" #x ")"); })
47f0d66153SMichal Wajdeczko #else
48f0d66153SMichal Wajdeczko #define WARN_ON(x) WARN((x), "%s", "WARN_ON(" __stringify(x) ")")
49f0d66153SMichal Wajdeczko #endif
50f0d66153SMichal Wajdeczko 
51f0d66153SMichal Wajdeczko #undef WARN_ON_ONCE
52f0d66153SMichal Wajdeczko #define WARN_ON_ONCE(x) WARN_ONCE((x), "%s", "WARN_ON_ONCE(" __stringify(x) ")")
53f0d66153SMichal Wajdeczko 
5457bdff48SLucas De Marchi #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
5557bdff48SLucas De Marchi 			     __stringify(x), (long)(x))
56f0d66153SMichal Wajdeczko 
57358c855cSJani Nikula void __printf(3, 4)
58358c855cSJani Nikula __i915_printk(struct drm_i915_private *dev_priv, const char *level,
59358c855cSJani Nikula 	      const char *fmt, ...);
60358c855cSJani Nikula 
61358c855cSJani Nikula #define i915_report_error(dev_priv, fmt, ...)				   \
62358c855cSJani Nikula 	__i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__)
63358c855cSJani Nikula 
64358c855cSJani Nikula #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
65358c855cSJani Nikula 
66dd6e38dfSJanusz Krzysztofik int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
67358c855cSJani Nikula 			      const char *func, int line);
68dd6e38dfSJanusz Krzysztofik #define i915_inject_probe_error(_i915, _err) \
69dd6e38dfSJanusz Krzysztofik 	__i915_inject_probe_error((_i915), (_err), __func__, __LINE__)
70358c855cSJani Nikula bool i915_error_injected(void);
71358c855cSJani Nikula 
72358c855cSJani Nikula #else
73358c855cSJani Nikula 
74b58a8813SMichal Wajdeczko #define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; })
75358c855cSJani Nikula #define i915_error_injected() false
76358c855cSJani Nikula 
77358c855cSJani Nikula #endif
78358c855cSJani Nikula 
79dd6e38dfSJanusz Krzysztofik #define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV)
80358c855cSJani Nikula 
81358c855cSJani Nikula #define i915_probe_error(i915, fmt, ...)				   \
82358c855cSJani Nikula 	__i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \
83358c855cSJani Nikula 		      fmt, ##__VA_ARGS__)
84358c855cSJani Nikula 
85815f0ddbSNick Desaulniers #if defined(GCC_VERSION) && GCC_VERSION >= 70000
8670bbe53cSVille Syrjälä #define add_overflows_t(T, A, B) \
8770bbe53cSVille Syrjälä 	__builtin_add_overflow_p((A), (B), (T)0)
881692cd60SChris Wilson #else
8970bbe53cSVille Syrjälä #define add_overflows_t(T, A, B) ({ \
901692cd60SChris Wilson 	typeof(A) a = (A); \
911692cd60SChris Wilson 	typeof(B) b = (B); \
9270bbe53cSVille Syrjälä 	(T)(a + b) < a; \
931692cd60SChris Wilson })
941692cd60SChris Wilson #endif
951692cd60SChris Wilson 
9670bbe53cSVille Syrjälä #define add_overflows(A, B) \
9770bbe53cSVille Syrjälä 	add_overflows_t(typeof((A) + (B)), (A), (B))
9870bbe53cSVille Syrjälä 
9940b326eeSChris Wilson #define range_overflows(start, size, max) ({ \
10040b326eeSChris Wilson 	typeof(start) start__ = (start); \
10140b326eeSChris Wilson 	typeof(size) size__ = (size); \
10240b326eeSChris Wilson 	typeof(max) max__ = (max); \
10340b326eeSChris Wilson 	(void)(&start__ == &size__); \
10440b326eeSChris Wilson 	(void)(&start__ == &max__); \
10540b326eeSChris Wilson 	start__ > max__ || size__ > max__ - start__; \
10640b326eeSChris Wilson })
10740b326eeSChris Wilson 
10840b326eeSChris Wilson #define range_overflows_t(type, start, size, max) \
10940b326eeSChris Wilson 	range_overflows((type)(start), (type)(size), (type)(max))
11040b326eeSChris Wilson 
11140b326eeSChris Wilson /* Note we don't consider signbits :| */
11240b326eeSChris Wilson #define overflows_type(x, T) \
11374f6e183SChris Wilson 	(sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T))
11440b326eeSChris Wilson 
115976b55f0SChris Wilson static inline bool
116976b55f0SChris Wilson __check_struct_size(size_t base, size_t arr, size_t count, size_t *size)
117976b55f0SChris Wilson {
118976b55f0SChris Wilson 	size_t sz;
119976b55f0SChris Wilson 
120976b55f0SChris Wilson 	if (check_mul_overflow(count, arr, &sz))
121976b55f0SChris Wilson 		return false;
122976b55f0SChris Wilson 
123976b55f0SChris Wilson 	if (check_add_overflow(sz, base, &sz))
124976b55f0SChris Wilson 		return false;
125976b55f0SChris Wilson 
126976b55f0SChris Wilson 	*size = sz;
127976b55f0SChris Wilson 	return true;
128976b55f0SChris Wilson }
129976b55f0SChris Wilson 
130976b55f0SChris Wilson /**
131976b55f0SChris Wilson  * check_struct_size() - Calculate size of structure with trailing array.
132976b55f0SChris Wilson  * @p: Pointer to the structure.
133976b55f0SChris Wilson  * @member: Name of the array member.
134976b55f0SChris Wilson  * @n: Number of elements in the array.
135976b55f0SChris Wilson  * @sz: Total size of structure and array
136976b55f0SChris Wilson  *
137976b55f0SChris Wilson  * Calculates size of memory needed for structure @p followed by an
138976b55f0SChris Wilson  * array of @n @member elements, like struct_size() but reports
139976b55f0SChris Wilson  * whether it overflowed, and the resultant size in @sz
140976b55f0SChris Wilson  *
141976b55f0SChris Wilson  * Return: false if the calculation overflowed.
142976b55f0SChris Wilson  */
143976b55f0SChris Wilson #define check_struct_size(p, member, n, sz) \
144976b55f0SChris Wilson 	likely(__check_struct_size(sizeof(*(p)), \
145976b55f0SChris Wilson 				   sizeof(*(p)->member) + __must_be_array((p)->member), \
146976b55f0SChris Wilson 				   n, sz))
147976b55f0SChris Wilson 
1480ce81788SChris Wilson #define ptr_mask_bits(ptr, n) ({					\
149b7163936SChris Wilson 	unsigned long __v = (unsigned long)(ptr);			\
1500ce81788SChris Wilson 	(typeof(ptr))(__v & -BIT(n));					\
151b7163936SChris Wilson })
152b7163936SChris Wilson 
1530ce81788SChris Wilson #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
1540ce81788SChris Wilson 
1550ce81788SChris Wilson #define ptr_unpack_bits(ptr, bits, n) ({				\
156b7163936SChris Wilson 	unsigned long __v = (unsigned long)(ptr);			\
1570ce81788SChris Wilson 	*(bits) = __v & (BIT(n) - 1);					\
1580ce81788SChris Wilson 	(typeof(ptr))(__v & -BIT(n));					\
159b7163936SChris Wilson })
160b7163936SChris Wilson 
1612d751415STvrtko Ursulin #define ptr_pack_bits(ptr, bits, n) ({					\
1622d751415STvrtko Ursulin 	unsigned long __bits = (bits);					\
1632d751415STvrtko Ursulin 	GEM_BUG_ON(__bits & -BIT(n));					\
1642d751415STvrtko Ursulin 	((typeof(ptr))((unsigned long)(ptr) | __bits));			\
1652d751415STvrtko Ursulin })
166b7163936SChris Wilson 
167df403069SChris Wilson #define ptr_dec(ptr) ({							\
168df403069SChris Wilson 	unsigned long __v = (unsigned long)(ptr);			\
169df403069SChris Wilson 	(typeof(ptr))(__v - 1);						\
170df403069SChris Wilson })
17122b7a426SChris Wilson 
172df403069SChris Wilson #define ptr_inc(ptr) ({							\
173df403069SChris Wilson 	unsigned long __v = (unsigned long)(ptr);			\
174df403069SChris Wilson 	(typeof(ptr))(__v + 1);						\
175df403069SChris Wilson })
17622b7a426SChris Wilson 
1770ce81788SChris Wilson #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
1780ce81788SChris Wilson #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
1790ce81788SChris Wilson #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
1800ce81788SChris Wilson #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
1810ce81788SChris Wilson 
1824547c255SImre Deak #define struct_member(T, member) (((T *)0)->member)
1834547c255SImre Deak 
18416f11f46SMichal Wajdeczko #define ptr_offset(ptr, member) offsetof(typeof(*(ptr)), member)
18516f11f46SMichal Wajdeczko 
186b7163936SChris Wilson #define fetch_and_zero(ptr) ({						\
187b7163936SChris Wilson 	typeof(*ptr) __T = *(ptr);					\
188b7163936SChris Wilson 	*(ptr) = (typeof(*ptr))0;					\
189b7163936SChris Wilson 	__T;								\
190b7163936SChris Wilson })
191b7163936SChris Wilson 
1929d1305efSChris Wilson /*
1939d1305efSChris Wilson  * container_of_user: Extract the superclass from a pointer to a member.
1949d1305efSChris Wilson  *
1959d1305efSChris Wilson  * Exactly like container_of() with the exception that it plays nicely
1969d1305efSChris Wilson  * with sparse for __user @ptr.
1979d1305efSChris Wilson  */
1989d1305efSChris Wilson #define container_of_user(ptr, type, member) ({				\
1999d1305efSChris Wilson 	void __user *__mptr = (void __user *)(ptr);			\
2004547c255SImre Deak 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), struct_member(type, member)) && \
2019d1305efSChris Wilson 			 !__same_type(*(ptr), void),			\
2029d1305efSChris Wilson 			 "pointer type mismatch in container_of()");	\
2039d1305efSChris Wilson 	((type __user *)(__mptr - offsetof(type, member))); })
2049d1305efSChris Wilson 
2059d1305efSChris Wilson /*
2069d1305efSChris Wilson  * check_user_mbz: Check that a user value exists and is zero
2079d1305efSChris Wilson  *
2089d1305efSChris Wilson  * Frequently in our uABI we reserve space for future extensions, and
2099d1305efSChris Wilson  * two ensure that userspace is prepared we enforce that space must
2109d1305efSChris Wilson  * be zero. (Then any future extension can safely assume a default value
2119d1305efSChris Wilson  * of 0.)
2129d1305efSChris Wilson  *
2139d1305efSChris Wilson  * check_user_mbz() combines checking that the user pointer is accessible
2149d1305efSChris Wilson  * and that the contained value is zero.
2159d1305efSChris Wilson  *
2169d1305efSChris Wilson  * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
2179d1305efSChris Wilson  */
2189d1305efSChris Wilson #define check_user_mbz(U) ({						\
2199d1305efSChris Wilson 	typeof(*(U)) mbz__;						\
2209d1305efSChris Wilson 	get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0;		\
2219d1305efSChris Wilson })
2229d1305efSChris Wilson 
223bb8920f5SMichal Wajdeczko static inline u64 ptr_to_u64(const void *ptr)
224bb8920f5SMichal Wajdeczko {
225bb8920f5SMichal Wajdeczko 	return (uintptr_t)ptr;
226bb8920f5SMichal Wajdeczko }
227bb8920f5SMichal Wajdeczko 
2284ff4b44cSChris Wilson #define u64_to_ptr(T, x) ({						\
2294ff4b44cSChris Wilson 	typecheck(u64, x);						\
2304ff4b44cSChris Wilson 	(T *)(uintptr_t)(x);						\
2314ff4b44cSChris Wilson })
2324ff4b44cSChris Wilson 
23316586fcdSMichal Wajdeczko #define __mask_next_bit(mask) ({					\
23416586fcdSMichal Wajdeczko 	int __idx = ffs(mask) - 1;					\
23516586fcdSMichal Wajdeczko 	mask &= ~BIT(__idx);						\
23616586fcdSMichal Wajdeczko 	__idx;								\
23716586fcdSMichal Wajdeczko })
23816586fcdSMichal Wajdeczko 
239*2920516bSMatthew Auld static inline bool is_power_of_2_u64(u64 n)
240*2920516bSMatthew Auld {
241*2920516bSMatthew Auld 	return (n != 0 && ((n & (n - 1)) == 0));
242*2920516bSMatthew Auld }
243*2920516bSMatthew Auld 
2446c067579SChris Wilson static inline void __list_del_many(struct list_head *head,
2456c067579SChris Wilson 				   struct list_head *first)
2466c067579SChris Wilson {
2476c067579SChris Wilson 	first->prev = head;
2486c067579SChris Wilson 	WRITE_ONCE(head->next, first);
2496c067579SChris Wilson }
2506c067579SChris Wilson 
2517c26240eSChris Wilson /*
2527c26240eSChris Wilson  * Wait until the work is finally complete, even if it tries to postpone
2537c26240eSChris Wilson  * by requeueing itself. Note, that if the worker never cancels itself,
2547c26240eSChris Wilson  * we will spin forever.
2557c26240eSChris Wilson  */
2567c26240eSChris Wilson static inline void drain_delayed_work(struct delayed_work *dw)
2577c26240eSChris Wilson {
2587c26240eSChris Wilson 	do {
2597c26240eSChris Wilson 		while (flush_delayed_work(dw))
2607c26240eSChris Wilson 			;
2617c26240eSChris Wilson 	} while (delayed_work_pending(dw));
2627c26240eSChris Wilson }
2637c26240eSChris Wilson 
264b30ed4ccSJani Nikula static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
265b30ed4ccSJani Nikula {
266b30ed4ccSJani Nikula 	unsigned long j = msecs_to_jiffies(m);
267b30ed4ccSJani Nikula 
268b30ed4ccSJani Nikula 	return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
269b30ed4ccSJani Nikula }
270b30ed4ccSJani Nikula 
271b30ed4ccSJani Nikula /*
272b30ed4ccSJani Nikula  * If you need to wait X milliseconds between events A and B, but event B
273b30ed4ccSJani Nikula  * doesn't happen exactly after event A, you record the timestamp (jiffies) of
274b30ed4ccSJani Nikula  * when event A happened, then just before event B you call this function and
275b30ed4ccSJani Nikula  * pass the timestamp as the first argument, and X as the second argument.
276b30ed4ccSJani Nikula  */
277b30ed4ccSJani Nikula static inline void
278b30ed4ccSJani Nikula wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
279b30ed4ccSJani Nikula {
280b30ed4ccSJani Nikula 	unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
281b30ed4ccSJani Nikula 
282b30ed4ccSJani Nikula 	/*
283b30ed4ccSJani Nikula 	 * Don't re-read the value of "jiffies" every time since it may change
284b30ed4ccSJani Nikula 	 * behind our back and break the math.
285b30ed4ccSJani Nikula 	 */
286b30ed4ccSJani Nikula 	tmp_jiffies = jiffies;
287b30ed4ccSJani Nikula 	target_jiffies = timestamp_jiffies +
288b30ed4ccSJani Nikula 			 msecs_to_jiffies_timeout(to_wait_ms);
289b30ed4ccSJani Nikula 
290b30ed4ccSJani Nikula 	if (time_after(target_jiffies, tmp_jiffies)) {
291b30ed4ccSJani Nikula 		remaining_jiffies = target_jiffies - tmp_jiffies;
292b30ed4ccSJani Nikula 		while (remaining_jiffies)
293b30ed4ccSJani Nikula 			remaining_jiffies =
294b30ed4ccSJani Nikula 			    schedule_timeout_uninterruptible(remaining_jiffies);
295b30ed4ccSJani Nikula 	}
296b30ed4ccSJani Nikula }
297b30ed4ccSJani Nikula 
298b30ed4ccSJani Nikula /**
299b30ed4ccSJani Nikula  * __wait_for - magic wait macro
300b30ed4ccSJani Nikula  *
301b30ed4ccSJani Nikula  * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
302b30ed4ccSJani Nikula  * important that we check the condition again after having timed out, since the
303b30ed4ccSJani Nikula  * timeout could be due to preemption or similar and we've never had a chance to
304b30ed4ccSJani Nikula  * check the condition before the timeout.
305b30ed4ccSJani Nikula  */
306b30ed4ccSJani Nikula #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
307b30ed4ccSJani Nikula 	const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
308b30ed4ccSJani Nikula 	long wait__ = (Wmin); /* recommended min for usleep is 10 us */	\
309b30ed4ccSJani Nikula 	int ret__;							\
310b30ed4ccSJani Nikula 	might_sleep();							\
311b30ed4ccSJani Nikula 	for (;;) {							\
312b30ed4ccSJani Nikula 		const bool expired__ = ktime_after(ktime_get_raw(), end__); \
313b30ed4ccSJani Nikula 		OP;							\
314b30ed4ccSJani Nikula 		/* Guarantee COND check prior to timeout */		\
315b30ed4ccSJani Nikula 		barrier();						\
316b30ed4ccSJani Nikula 		if (COND) {						\
317b30ed4ccSJani Nikula 			ret__ = 0;					\
318b30ed4ccSJani Nikula 			break;						\
319b30ed4ccSJani Nikula 		}							\
320b30ed4ccSJani Nikula 		if (expired__) {					\
321b30ed4ccSJani Nikula 			ret__ = -ETIMEDOUT;				\
322b30ed4ccSJani Nikula 			break;						\
323b30ed4ccSJani Nikula 		}							\
324b30ed4ccSJani Nikula 		usleep_range(wait__, wait__ * 2);			\
325b30ed4ccSJani Nikula 		if (wait__ < (Wmax))					\
326b30ed4ccSJani Nikula 			wait__ <<= 1;					\
327b30ed4ccSJani Nikula 	}								\
328b30ed4ccSJani Nikula 	ret__;								\
329b30ed4ccSJani Nikula })
330b30ed4ccSJani Nikula 
331b30ed4ccSJani Nikula #define _wait_for(COND, US, Wmin, Wmax)	__wait_for(, (COND), (US), (Wmin), \
332b30ed4ccSJani Nikula 						   (Wmax))
333b30ed4ccSJani Nikula #define wait_for(COND, MS)		_wait_for((COND), (MS) * 1000, 10, 1000)
334b30ed4ccSJani Nikula 
335b30ed4ccSJani Nikula /* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
336b30ed4ccSJani Nikula #if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
337b30ed4ccSJani Nikula # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
338b30ed4ccSJani Nikula #else
339b30ed4ccSJani Nikula # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
340b30ed4ccSJani Nikula #endif
341b30ed4ccSJani Nikula 
342b30ed4ccSJani Nikula #define _wait_for_atomic(COND, US, ATOMIC) \
343b30ed4ccSJani Nikula ({ \
344b30ed4ccSJani Nikula 	int cpu, ret, timeout = (US) * 1000; \
345b30ed4ccSJani Nikula 	u64 base; \
346b30ed4ccSJani Nikula 	_WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
347b30ed4ccSJani Nikula 	if (!(ATOMIC)) { \
348b30ed4ccSJani Nikula 		preempt_disable(); \
349b30ed4ccSJani Nikula 		cpu = smp_processor_id(); \
350b30ed4ccSJani Nikula 	} \
351b30ed4ccSJani Nikula 	base = local_clock(); \
352b30ed4ccSJani Nikula 	for (;;) { \
353b30ed4ccSJani Nikula 		u64 now = local_clock(); \
354b30ed4ccSJani Nikula 		if (!(ATOMIC)) \
355b30ed4ccSJani Nikula 			preempt_enable(); \
356b30ed4ccSJani Nikula 		/* Guarantee COND check prior to timeout */ \
357b30ed4ccSJani Nikula 		barrier(); \
358b30ed4ccSJani Nikula 		if (COND) { \
359b30ed4ccSJani Nikula 			ret = 0; \
360b30ed4ccSJani Nikula 			break; \
361b30ed4ccSJani Nikula 		} \
362b30ed4ccSJani Nikula 		if (now - base >= timeout) { \
363b30ed4ccSJani Nikula 			ret = -ETIMEDOUT; \
364b30ed4ccSJani Nikula 			break; \
365b30ed4ccSJani Nikula 		} \
366b30ed4ccSJani Nikula 		cpu_relax(); \
367b30ed4ccSJani Nikula 		if (!(ATOMIC)) { \
368b30ed4ccSJani Nikula 			preempt_disable(); \
369b30ed4ccSJani Nikula 			if (unlikely(cpu != smp_processor_id())) { \
370b30ed4ccSJani Nikula 				timeout -= now - base; \
371b30ed4ccSJani Nikula 				cpu = smp_processor_id(); \
372b30ed4ccSJani Nikula 				base = local_clock(); \
373b30ed4ccSJani Nikula 			} \
374b30ed4ccSJani Nikula 		} \
375b30ed4ccSJani Nikula 	} \
376b30ed4ccSJani Nikula 	ret; \
377b30ed4ccSJani Nikula })
378b30ed4ccSJani Nikula 
379b30ed4ccSJani Nikula #define wait_for_us(COND, US) \
380b30ed4ccSJani Nikula ({ \
381b30ed4ccSJani Nikula 	int ret__; \
382b30ed4ccSJani Nikula 	BUILD_BUG_ON(!__builtin_constant_p(US)); \
383b30ed4ccSJani Nikula 	if ((US) > 10) \
384b30ed4ccSJani Nikula 		ret__ = _wait_for((COND), (US), 10, 10); \
385b30ed4ccSJani Nikula 	else \
386b30ed4ccSJani Nikula 		ret__ = _wait_for_atomic((COND), (US), 0); \
387b30ed4ccSJani Nikula 	ret__; \
388b30ed4ccSJani Nikula })
389b30ed4ccSJani Nikula 
390b30ed4ccSJani Nikula #define wait_for_atomic_us(COND, US) \
391b30ed4ccSJani Nikula ({ \
392b30ed4ccSJani Nikula 	BUILD_BUG_ON(!__builtin_constant_p(US)); \
393b30ed4ccSJani Nikula 	BUILD_BUG_ON((US) > 50000); \
394b30ed4ccSJani Nikula 	_wait_for_atomic((COND), (US), 1); \
395b30ed4ccSJani Nikula })
396b30ed4ccSJani Nikula 
397b30ed4ccSJani Nikula #define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
398b30ed4ccSJani Nikula 
399b30ed4ccSJani Nikula #define KHz(x) (1000 * (x))
400b30ed4ccSJani Nikula #define MHz(x) KHz(1000 * (x))
401b30ed4ccSJani Nikula 
402b30ed4ccSJani Nikula #define KBps(x) (1000 * (x))
403b30ed4ccSJani Nikula #define MBps(x) KBps(1000 * (x))
404b30ed4ccSJani Nikula #define GBps(x) ((u64)1000 * MBps((x)))
405b30ed4ccSJani Nikula 
406b74eeeb6SMichal Wajdeczko static inline const char *yesno(bool v)
407b74eeeb6SMichal Wajdeczko {
408b74eeeb6SMichal Wajdeczko 	return v ? "yes" : "no";
409b74eeeb6SMichal Wajdeczko }
410b74eeeb6SMichal Wajdeczko 
411b74eeeb6SMichal Wajdeczko static inline const char *onoff(bool v)
412b74eeeb6SMichal Wajdeczko {
413b74eeeb6SMichal Wajdeczko 	return v ? "on" : "off";
414b74eeeb6SMichal Wajdeczko }
415b74eeeb6SMichal Wajdeczko 
416b74eeeb6SMichal Wajdeczko static inline const char *enableddisabled(bool v)
417b74eeeb6SMichal Wajdeczko {
418b74eeeb6SMichal Wajdeczko 	return v ? "enabled" : "disabled";
419b74eeeb6SMichal Wajdeczko }
420b74eeeb6SMichal Wajdeczko 
421e3adffe8SJani Nikula static inline void add_taint_for_CI(unsigned int taint)
422e3adffe8SJani Nikula {
423e3adffe8SJani Nikula 	/*
424e3adffe8SJani Nikula 	 * The system is "ok", just about surviving for the user, but
425e3adffe8SJani Nikula 	 * CI results are now unreliable as the HW is very suspect.
426e3adffe8SJani Nikula 	 * CI checks the taint state after every test and will reboot
427e3adffe8SJani Nikula 	 * the machine if the kernel is tainted.
428e3adffe8SJani Nikula 	 */
429e3adffe8SJani Nikula 	add_taint(taint, LOCKDEP_STILL_OK);
430e3adffe8SJani Nikula }
431e3adffe8SJani Nikula 
4323a7a92abSChris Wilson void cancel_timer(struct timer_list *t);
4333a7a92abSChris Wilson void set_timer_ms(struct timer_list *t, unsigned long timeout);
4343a7a92abSChris Wilson 
4353a7a92abSChris Wilson static inline bool timer_expired(const struct timer_list *t)
4363a7a92abSChris Wilson {
4373a7a92abSChris Wilson 	return READ_ONCE(t->expires) && !timer_pending(t);
4383a7a92abSChris Wilson }
4393a7a92abSChris Wilson 
440babaab2fSChris Wilson /*
441babaab2fSChris Wilson  * This is a lookalike for IS_ENABLED() that takes a kconfig value,
442babaab2fSChris Wilson  * e.g. CONFIG_DRM_I915_SPIN_REQUEST, and evaluates whether it is non-zero
443babaab2fSChris Wilson  * i.e. whether the configuration is active. Wrapping up the config inside
444babaab2fSChris Wilson  * a boolean context prevents clang and smatch from complaining about potential
445babaab2fSChris Wilson  * issues in confusing logical-&& with bitwise-& for constants.
446babaab2fSChris Wilson  *
447babaab2fSChris Wilson  * Sadly IS_ENABLED() itself does not work with kconfig values.
448babaab2fSChris Wilson  *
449babaab2fSChris Wilson  * Returns 0 if @config is 0, 1 if set to any value.
450babaab2fSChris Wilson  */
451babaab2fSChris Wilson #define IS_ACTIVE(config) ((config) != 0)
452babaab2fSChris Wilson 
45340b326eeSChris Wilson #endif /* !__I915_UTILS_H */
454