xref: /linux/drivers/gpu/drm/i915/i915_utils.h (revision 0868b1ce8772058da85786caac4baa61abb4f93a)
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__); \
105520f8350SMatthew Auld 	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 
111520f8350SMatthew Auld #define range_overflows_end(start, size, max) ({ \
112520f8350SMatthew Auld 	typeof(start) start__ = (start); \
113520f8350SMatthew Auld 	typeof(size) size__ = (size); \
114520f8350SMatthew Auld 	typeof(max) max__ = (max); \
115520f8350SMatthew Auld 	(void)(&start__ == &size__); \
116520f8350SMatthew Auld 	(void)(&start__ == &max__); \
117520f8350SMatthew Auld 	start__ > max__ || size__ > max__ - start__; \
118520f8350SMatthew Auld })
119520f8350SMatthew Auld 
120520f8350SMatthew Auld #define range_overflows_end_t(type, start, size, max) \
121520f8350SMatthew Auld 	range_overflows_end((type)(start), (type)(size), (type)(max))
122520f8350SMatthew Auld 
12340b326eeSChris Wilson /* Note we don't consider signbits :| */
12440b326eeSChris Wilson #define overflows_type(x, T) \
12574f6e183SChris Wilson 	(sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T))
12640b326eeSChris Wilson 
127976b55f0SChris Wilson static inline bool
128976b55f0SChris Wilson __check_struct_size(size_t base, size_t arr, size_t count, size_t *size)
129976b55f0SChris Wilson {
130976b55f0SChris Wilson 	size_t sz;
131976b55f0SChris Wilson 
132976b55f0SChris Wilson 	if (check_mul_overflow(count, arr, &sz))
133976b55f0SChris Wilson 		return false;
134976b55f0SChris Wilson 
135976b55f0SChris Wilson 	if (check_add_overflow(sz, base, &sz))
136976b55f0SChris Wilson 		return false;
137976b55f0SChris Wilson 
138976b55f0SChris Wilson 	*size = sz;
139976b55f0SChris Wilson 	return true;
140976b55f0SChris Wilson }
141976b55f0SChris Wilson 
142976b55f0SChris Wilson /**
143976b55f0SChris Wilson  * check_struct_size() - Calculate size of structure with trailing array.
144976b55f0SChris Wilson  * @p: Pointer to the structure.
145976b55f0SChris Wilson  * @member: Name of the array member.
146976b55f0SChris Wilson  * @n: Number of elements in the array.
147976b55f0SChris Wilson  * @sz: Total size of structure and array
148976b55f0SChris Wilson  *
149976b55f0SChris Wilson  * Calculates size of memory needed for structure @p followed by an
150976b55f0SChris Wilson  * array of @n @member elements, like struct_size() but reports
151976b55f0SChris Wilson  * whether it overflowed, and the resultant size in @sz
152976b55f0SChris Wilson  *
153976b55f0SChris Wilson  * Return: false if the calculation overflowed.
154976b55f0SChris Wilson  */
155976b55f0SChris Wilson #define check_struct_size(p, member, n, sz) \
156976b55f0SChris Wilson 	likely(__check_struct_size(sizeof(*(p)), \
157976b55f0SChris Wilson 				   sizeof(*(p)->member) + __must_be_array((p)->member), \
158976b55f0SChris Wilson 				   n, sz))
159976b55f0SChris Wilson 
1600ce81788SChris Wilson #define ptr_mask_bits(ptr, n) ({					\
161b7163936SChris Wilson 	unsigned long __v = (unsigned long)(ptr);			\
1620ce81788SChris Wilson 	(typeof(ptr))(__v & -BIT(n));					\
163b7163936SChris Wilson })
164b7163936SChris Wilson 
1650ce81788SChris Wilson #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
1660ce81788SChris Wilson 
1670ce81788SChris Wilson #define ptr_unpack_bits(ptr, bits, n) ({				\
168b7163936SChris Wilson 	unsigned long __v = (unsigned long)(ptr);			\
1690ce81788SChris Wilson 	*(bits) = __v & (BIT(n) - 1);					\
1700ce81788SChris Wilson 	(typeof(ptr))(__v & -BIT(n));					\
171b7163936SChris Wilson })
172b7163936SChris Wilson 
1732d751415STvrtko Ursulin #define ptr_pack_bits(ptr, bits, n) ({					\
1742d751415STvrtko Ursulin 	unsigned long __bits = (bits);					\
1752d751415STvrtko Ursulin 	GEM_BUG_ON(__bits & -BIT(n));					\
1762d751415STvrtko Ursulin 	((typeof(ptr))((unsigned long)(ptr) | __bits));			\
1772d751415STvrtko Ursulin })
178b7163936SChris Wilson 
179df403069SChris Wilson #define ptr_dec(ptr) ({							\
180df403069SChris Wilson 	unsigned long __v = (unsigned long)(ptr);			\
181df403069SChris Wilson 	(typeof(ptr))(__v - 1);						\
182df403069SChris Wilson })
18322b7a426SChris Wilson 
184df403069SChris Wilson #define ptr_inc(ptr) ({							\
185df403069SChris Wilson 	unsigned long __v = (unsigned long)(ptr);			\
186df403069SChris Wilson 	(typeof(ptr))(__v + 1);						\
187df403069SChris Wilson })
18822b7a426SChris Wilson 
1890ce81788SChris Wilson #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
1900ce81788SChris Wilson #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
1910ce81788SChris Wilson #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
1920ce81788SChris Wilson #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
1930ce81788SChris Wilson 
1944547c255SImre Deak #define struct_member(T, member) (((T *)0)->member)
1954547c255SImre Deak 
19616f11f46SMichal Wajdeczko #define ptr_offset(ptr, member) offsetof(typeof(*(ptr)), member)
19716f11f46SMichal Wajdeczko 
198b7163936SChris Wilson #define fetch_and_zero(ptr) ({						\
199b7163936SChris Wilson 	typeof(*ptr) __T = *(ptr);					\
200b7163936SChris Wilson 	*(ptr) = (typeof(*ptr))0;					\
201b7163936SChris Wilson 	__T;								\
202b7163936SChris Wilson })
203b7163936SChris Wilson 
2049d1305efSChris Wilson /*
2059d1305efSChris Wilson  * container_of_user: Extract the superclass from a pointer to a member.
2069d1305efSChris Wilson  *
2079d1305efSChris Wilson  * Exactly like container_of() with the exception that it plays nicely
2089d1305efSChris Wilson  * with sparse for __user @ptr.
2099d1305efSChris Wilson  */
2109d1305efSChris Wilson #define container_of_user(ptr, type, member) ({				\
2119d1305efSChris Wilson 	void __user *__mptr = (void __user *)(ptr);			\
2124547c255SImre Deak 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), struct_member(type, member)) && \
2139d1305efSChris Wilson 			 !__same_type(*(ptr), void),			\
2149d1305efSChris Wilson 			 "pointer type mismatch in container_of()");	\
2159d1305efSChris Wilson 	((type __user *)(__mptr - offsetof(type, member))); })
2169d1305efSChris Wilson 
2179d1305efSChris Wilson /*
2189d1305efSChris Wilson  * check_user_mbz: Check that a user value exists and is zero
2199d1305efSChris Wilson  *
2209d1305efSChris Wilson  * Frequently in our uABI we reserve space for future extensions, and
2219d1305efSChris Wilson  * two ensure that userspace is prepared we enforce that space must
2229d1305efSChris Wilson  * be zero. (Then any future extension can safely assume a default value
2239d1305efSChris Wilson  * of 0.)
2249d1305efSChris Wilson  *
2259d1305efSChris Wilson  * check_user_mbz() combines checking that the user pointer is accessible
2269d1305efSChris Wilson  * and that the contained value is zero.
2279d1305efSChris Wilson  *
2289d1305efSChris Wilson  * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
2299d1305efSChris Wilson  */
2309d1305efSChris Wilson #define check_user_mbz(U) ({						\
2319d1305efSChris Wilson 	typeof(*(U)) mbz__;						\
2329d1305efSChris Wilson 	get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0;		\
2339d1305efSChris Wilson })
2349d1305efSChris Wilson 
235bb8920f5SMichal Wajdeczko static inline u64 ptr_to_u64(const void *ptr)
236bb8920f5SMichal Wajdeczko {
237bb8920f5SMichal Wajdeczko 	return (uintptr_t)ptr;
238bb8920f5SMichal Wajdeczko }
239bb8920f5SMichal Wajdeczko 
2404ff4b44cSChris Wilson #define u64_to_ptr(T, x) ({						\
2414ff4b44cSChris Wilson 	typecheck(u64, x);						\
2424ff4b44cSChris Wilson 	(T *)(uintptr_t)(x);						\
2434ff4b44cSChris Wilson })
2444ff4b44cSChris Wilson 
24516586fcdSMichal Wajdeczko #define __mask_next_bit(mask) ({					\
24616586fcdSMichal Wajdeczko 	int __idx = ffs(mask) - 1;					\
24716586fcdSMichal Wajdeczko 	mask &= ~BIT(__idx);						\
24816586fcdSMichal Wajdeczko 	__idx;								\
24916586fcdSMichal Wajdeczko })
25016586fcdSMichal Wajdeczko 
2512920516bSMatthew Auld static inline bool is_power_of_2_u64(u64 n)
2522920516bSMatthew Auld {
2532920516bSMatthew Auld 	return (n != 0 && ((n & (n - 1)) == 0));
2542920516bSMatthew Auld }
2552920516bSMatthew Auld 
2566c067579SChris Wilson static inline void __list_del_many(struct list_head *head,
2576c067579SChris Wilson 				   struct list_head *first)
2586c067579SChris Wilson {
2596c067579SChris Wilson 	first->prev = head;
2606c067579SChris Wilson 	WRITE_ONCE(head->next, first);
2616c067579SChris Wilson }
2626c067579SChris Wilson 
263875c3b4bSChris Wilson static inline int list_is_last_rcu(const struct list_head *list,
264875c3b4bSChris Wilson 				   const struct list_head *head)
265875c3b4bSChris Wilson {
266875c3b4bSChris Wilson 	return READ_ONCE(list->next) == head;
267875c3b4bSChris Wilson }
268875c3b4bSChris Wilson 
269b30ed4ccSJani Nikula static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
270b30ed4ccSJani Nikula {
271b30ed4ccSJani Nikula 	unsigned long j = msecs_to_jiffies(m);
272b30ed4ccSJani Nikula 
273b30ed4ccSJani Nikula 	return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
274b30ed4ccSJani Nikula }
275b30ed4ccSJani Nikula 
276b30ed4ccSJani Nikula /*
277b30ed4ccSJani Nikula  * If you need to wait X milliseconds between events A and B, but event B
278b30ed4ccSJani Nikula  * doesn't happen exactly after event A, you record the timestamp (jiffies) of
279b30ed4ccSJani Nikula  * when event A happened, then just before event B you call this function and
280b30ed4ccSJani Nikula  * pass the timestamp as the first argument, and X as the second argument.
281b30ed4ccSJani Nikula  */
282b30ed4ccSJani Nikula static inline void
283b30ed4ccSJani Nikula wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
284b30ed4ccSJani Nikula {
285b30ed4ccSJani Nikula 	unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
286b30ed4ccSJani Nikula 
287b30ed4ccSJani Nikula 	/*
288b30ed4ccSJani Nikula 	 * Don't re-read the value of "jiffies" every time since it may change
289b30ed4ccSJani Nikula 	 * behind our back and break the math.
290b30ed4ccSJani Nikula 	 */
291b30ed4ccSJani Nikula 	tmp_jiffies = jiffies;
292b30ed4ccSJani Nikula 	target_jiffies = timestamp_jiffies +
293b30ed4ccSJani Nikula 			 msecs_to_jiffies_timeout(to_wait_ms);
294b30ed4ccSJani Nikula 
295b30ed4ccSJani Nikula 	if (time_after(target_jiffies, tmp_jiffies)) {
296b30ed4ccSJani Nikula 		remaining_jiffies = target_jiffies - tmp_jiffies;
297b30ed4ccSJani Nikula 		while (remaining_jiffies)
298b30ed4ccSJani Nikula 			remaining_jiffies =
299b30ed4ccSJani Nikula 			    schedule_timeout_uninterruptible(remaining_jiffies);
300b30ed4ccSJani Nikula 	}
301b30ed4ccSJani Nikula }
302b30ed4ccSJani Nikula 
303b30ed4ccSJani Nikula /**
304b30ed4ccSJani Nikula  * __wait_for - magic wait macro
305b30ed4ccSJani Nikula  *
306b30ed4ccSJani Nikula  * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
307b30ed4ccSJani Nikula  * important that we check the condition again after having timed out, since the
308b30ed4ccSJani Nikula  * timeout could be due to preemption or similar and we've never had a chance to
309b30ed4ccSJani Nikula  * check the condition before the timeout.
310b30ed4ccSJani Nikula  */
311b30ed4ccSJani Nikula #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
312b30ed4ccSJani Nikula 	const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
313b30ed4ccSJani Nikula 	long wait__ = (Wmin); /* recommended min for usleep is 10 us */	\
314b30ed4ccSJani Nikula 	int ret__;							\
315b30ed4ccSJani Nikula 	might_sleep();							\
316b30ed4ccSJani Nikula 	for (;;) {							\
317b30ed4ccSJani Nikula 		const bool expired__ = ktime_after(ktime_get_raw(), end__); \
318b30ed4ccSJani Nikula 		OP;							\
319b30ed4ccSJani Nikula 		/* Guarantee COND check prior to timeout */		\
320b30ed4ccSJani Nikula 		barrier();						\
321b30ed4ccSJani Nikula 		if (COND) {						\
322b30ed4ccSJani Nikula 			ret__ = 0;					\
323b30ed4ccSJani Nikula 			break;						\
324b30ed4ccSJani Nikula 		}							\
325b30ed4ccSJani Nikula 		if (expired__) {					\
326b30ed4ccSJani Nikula 			ret__ = -ETIMEDOUT;				\
327b30ed4ccSJani Nikula 			break;						\
328b30ed4ccSJani Nikula 		}							\
329b30ed4ccSJani Nikula 		usleep_range(wait__, wait__ * 2);			\
330b30ed4ccSJani Nikula 		if (wait__ < (Wmax))					\
331b30ed4ccSJani Nikula 			wait__ <<= 1;					\
332b30ed4ccSJani Nikula 	}								\
333b30ed4ccSJani Nikula 	ret__;								\
334b30ed4ccSJani Nikula })
335b30ed4ccSJani Nikula 
336b30ed4ccSJani Nikula #define _wait_for(COND, US, Wmin, Wmax)	__wait_for(, (COND), (US), (Wmin), \
337b30ed4ccSJani Nikula 						   (Wmax))
338b30ed4ccSJani Nikula #define wait_for(COND, MS)		_wait_for((COND), (MS) * 1000, 10, 1000)
339b30ed4ccSJani Nikula 
340b30ed4ccSJani Nikula /* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
341b30ed4ccSJani Nikula #if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
342b30ed4ccSJani Nikula # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
343b30ed4ccSJani Nikula #else
344b30ed4ccSJani Nikula # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
345b30ed4ccSJani Nikula #endif
346b30ed4ccSJani Nikula 
347b30ed4ccSJani Nikula #define _wait_for_atomic(COND, US, ATOMIC) \
348b30ed4ccSJani Nikula ({ \
349b30ed4ccSJani Nikula 	int cpu, ret, timeout = (US) * 1000; \
350b30ed4ccSJani Nikula 	u64 base; \
351b30ed4ccSJani Nikula 	_WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
352b30ed4ccSJani Nikula 	if (!(ATOMIC)) { \
353b30ed4ccSJani Nikula 		preempt_disable(); \
354b30ed4ccSJani Nikula 		cpu = smp_processor_id(); \
355b30ed4ccSJani Nikula 	} \
356b30ed4ccSJani Nikula 	base = local_clock(); \
357b30ed4ccSJani Nikula 	for (;;) { \
358b30ed4ccSJani Nikula 		u64 now = local_clock(); \
359b30ed4ccSJani Nikula 		if (!(ATOMIC)) \
360b30ed4ccSJani Nikula 			preempt_enable(); \
361b30ed4ccSJani Nikula 		/* Guarantee COND check prior to timeout */ \
362b30ed4ccSJani Nikula 		barrier(); \
363b30ed4ccSJani Nikula 		if (COND) { \
364b30ed4ccSJani Nikula 			ret = 0; \
365b30ed4ccSJani Nikula 			break; \
366b30ed4ccSJani Nikula 		} \
367b30ed4ccSJani Nikula 		if (now - base >= timeout) { \
368b30ed4ccSJani Nikula 			ret = -ETIMEDOUT; \
369b30ed4ccSJani Nikula 			break; \
370b30ed4ccSJani Nikula 		} \
371b30ed4ccSJani Nikula 		cpu_relax(); \
372b30ed4ccSJani Nikula 		if (!(ATOMIC)) { \
373b30ed4ccSJani Nikula 			preempt_disable(); \
374b30ed4ccSJani Nikula 			if (unlikely(cpu != smp_processor_id())) { \
375b30ed4ccSJani Nikula 				timeout -= now - base; \
376b30ed4ccSJani Nikula 				cpu = smp_processor_id(); \
377b30ed4ccSJani Nikula 				base = local_clock(); \
378b30ed4ccSJani Nikula 			} \
379b30ed4ccSJani Nikula 		} \
380b30ed4ccSJani Nikula 	} \
381b30ed4ccSJani Nikula 	ret; \
382b30ed4ccSJani Nikula })
383b30ed4ccSJani Nikula 
384b30ed4ccSJani Nikula #define wait_for_us(COND, US) \
385b30ed4ccSJani Nikula ({ \
386b30ed4ccSJani Nikula 	int ret__; \
387b30ed4ccSJani Nikula 	BUILD_BUG_ON(!__builtin_constant_p(US)); \
388b30ed4ccSJani Nikula 	if ((US) > 10) \
389b30ed4ccSJani Nikula 		ret__ = _wait_for((COND), (US), 10, 10); \
390b30ed4ccSJani Nikula 	else \
391b30ed4ccSJani Nikula 		ret__ = _wait_for_atomic((COND), (US), 0); \
392b30ed4ccSJani Nikula 	ret__; \
393b30ed4ccSJani Nikula })
394b30ed4ccSJani Nikula 
395b30ed4ccSJani Nikula #define wait_for_atomic_us(COND, US) \
396b30ed4ccSJani Nikula ({ \
397b30ed4ccSJani Nikula 	BUILD_BUG_ON(!__builtin_constant_p(US)); \
398b30ed4ccSJani Nikula 	BUILD_BUG_ON((US) > 50000); \
399b30ed4ccSJani Nikula 	_wait_for_atomic((COND), (US), 1); \
400b30ed4ccSJani Nikula })
401b30ed4ccSJani Nikula 
402b30ed4ccSJani Nikula #define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
403b30ed4ccSJani Nikula 
404b30ed4ccSJani Nikula #define KHz(x) (1000 * (x))
405b30ed4ccSJani Nikula #define MHz(x) KHz(1000 * (x))
406b30ed4ccSJani Nikula 
407b30ed4ccSJani Nikula #define KBps(x) (1000 * (x))
408b30ed4ccSJani Nikula #define MBps(x) KBps(1000 * (x))
409b30ed4ccSJani Nikula #define GBps(x) ((u64)1000 * MBps((x)))
410b30ed4ccSJani Nikula 
411b74eeeb6SMichal Wajdeczko static inline const char *yesno(bool v)
412b74eeeb6SMichal Wajdeczko {
413b74eeeb6SMichal Wajdeczko 	return v ? "yes" : "no";
414b74eeeb6SMichal Wajdeczko }
415b74eeeb6SMichal Wajdeczko 
416b74eeeb6SMichal Wajdeczko static inline const char *onoff(bool v)
417b74eeeb6SMichal Wajdeczko {
418b74eeeb6SMichal Wajdeczko 	return v ? "on" : "off";
419b74eeeb6SMichal Wajdeczko }
420b74eeeb6SMichal Wajdeczko 
421*0868b1ceSVille Syrjälä static inline const char *enabledisable(bool v)
422*0868b1ceSVille Syrjälä {
423*0868b1ceSVille Syrjälä 	return v ? "enable" : "disable";
424*0868b1ceSVille Syrjälä }
425*0868b1ceSVille Syrjälä 
426b74eeeb6SMichal Wajdeczko static inline const char *enableddisabled(bool v)
427b74eeeb6SMichal Wajdeczko {
428b74eeeb6SMichal Wajdeczko 	return v ? "enabled" : "disabled";
429b74eeeb6SMichal Wajdeczko }
430b74eeeb6SMichal Wajdeczko 
43165706203SMichał Winiarski void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
43265706203SMichał Winiarski static inline void __add_taint_for_CI(unsigned int taint)
433e3adffe8SJani Nikula {
434e3adffe8SJani Nikula 	/*
435e3adffe8SJani Nikula 	 * The system is "ok", just about surviving for the user, but
436e3adffe8SJani Nikula 	 * CI results are now unreliable as the HW is very suspect.
437e3adffe8SJani Nikula 	 * CI checks the taint state after every test and will reboot
438e3adffe8SJani Nikula 	 * the machine if the kernel is tainted.
439e3adffe8SJani Nikula 	 */
440e3adffe8SJani Nikula 	add_taint(taint, LOCKDEP_STILL_OK);
441e3adffe8SJani Nikula }
442e3adffe8SJani Nikula 
4433a7a92abSChris Wilson void cancel_timer(struct timer_list *t);
4443a7a92abSChris Wilson void set_timer_ms(struct timer_list *t, unsigned long timeout);
4453a7a92abSChris Wilson 
446c185a16eSChris Wilson static inline bool timer_active(const struct timer_list *t)
447c185a16eSChris Wilson {
448c185a16eSChris Wilson 	return READ_ONCE(t->expires);
449c185a16eSChris Wilson }
450c185a16eSChris Wilson 
4513a7a92abSChris Wilson static inline bool timer_expired(const struct timer_list *t)
4523a7a92abSChris Wilson {
453c185a16eSChris Wilson 	return timer_active(t) && !timer_pending(t);
4543a7a92abSChris Wilson }
4553a7a92abSChris Wilson 
456babaab2fSChris Wilson /*
457babaab2fSChris Wilson  * This is a lookalike for IS_ENABLED() that takes a kconfig value,
458babaab2fSChris Wilson  * e.g. CONFIG_DRM_I915_SPIN_REQUEST, and evaluates whether it is non-zero
459babaab2fSChris Wilson  * i.e. whether the configuration is active. Wrapping up the config inside
460babaab2fSChris Wilson  * a boolean context prevents clang and smatch from complaining about potential
461babaab2fSChris Wilson  * issues in confusing logical-&& with bitwise-& for constants.
462babaab2fSChris Wilson  *
463babaab2fSChris Wilson  * Sadly IS_ENABLED() itself does not work with kconfig values.
464babaab2fSChris Wilson  *
465babaab2fSChris Wilson  * Returns 0 if @config is 0, 1 if set to any value.
466babaab2fSChris Wilson  */
467babaab2fSChris Wilson #define IS_ACTIVE(config) ((config) != 0)
468babaab2fSChris Wilson 
46940b326eeSChris Wilson #endif /* !__I915_UTILS_H */
470