xref: /dflybsd-src/sys/dev/drm/i915/i915_utils.h (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1a85cb24fSFrançois Tigeot /*
2a85cb24fSFrançois Tigeot  * Copyright © 2016 Intel Corporation
3a85cb24fSFrançois Tigeot  *
4a85cb24fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5a85cb24fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6a85cb24fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7a85cb24fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a85cb24fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9a85cb24fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10a85cb24fSFrançois Tigeot  *
11a85cb24fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12a85cb24fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13a85cb24fSFrançois Tigeot  * Software.
14a85cb24fSFrançois Tigeot  *
15a85cb24fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16a85cb24fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17a85cb24fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18a85cb24fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19a85cb24fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20a85cb24fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21a85cb24fSFrançois Tigeot  * IN THE SOFTWARE.
22a85cb24fSFrançois Tigeot  *
23a85cb24fSFrançois Tigeot  */
24a85cb24fSFrançois Tigeot 
25a85cb24fSFrançois Tigeot #ifndef __I915_UTILS_H
26a85cb24fSFrançois Tigeot #define __I915_UTILS_H
27a85cb24fSFrançois Tigeot 
28a85cb24fSFrançois Tigeot #undef WARN_ON
29a85cb24fSFrançois Tigeot /* Many gcc seem to no see through this and fall over :( */
30a85cb24fSFrançois Tigeot #if 0
31a85cb24fSFrançois Tigeot #define WARN_ON(x) ({ \
32a85cb24fSFrançois Tigeot 	bool __i915_warn_cond = (x); \
33a85cb24fSFrançois Tigeot 	if (__builtin_constant_p(__i915_warn_cond)) \
34a85cb24fSFrançois Tigeot 		BUILD_BUG_ON(__i915_warn_cond); \
35a85cb24fSFrançois Tigeot 	WARN(__i915_warn_cond, "WARN_ON(" #x ")"); })
36a85cb24fSFrançois Tigeot #else
37a85cb24fSFrançois Tigeot #define WARN_ON(x) WARN((x), "%s", "WARN_ON(" __stringify(x) ")")
38a85cb24fSFrançois Tigeot #endif
39a85cb24fSFrançois Tigeot 
40a85cb24fSFrançois Tigeot #undef WARN_ON_ONCE
41a85cb24fSFrançois Tigeot #define WARN_ON_ONCE(x) WARN_ONCE((x), "%s", "WARN_ON_ONCE(" __stringify(x) ")")
42a85cb24fSFrançois Tigeot 
43a85cb24fSFrançois Tigeot #define MISSING_CASE(x) WARN(1, "Missing switch case (%lu) in %s\n", \
44a85cb24fSFrançois Tigeot 			     (long)(x), __func__)
45a85cb24fSFrançois Tigeot 
46a85cb24fSFrançois Tigeot #if GCC_VERSION >= 70000
47a85cb24fSFrançois Tigeot #define add_overflows(A, B) \
48a85cb24fSFrançois Tigeot 	__builtin_add_overflow_p((A), (B), (typeof((A) + (B)))0)
49a85cb24fSFrançois Tigeot #else
50a85cb24fSFrançois Tigeot #define add_overflows(A, B) ({ \
51a85cb24fSFrançois Tigeot 	typeof(A) a = (A); \
52a85cb24fSFrançois Tigeot 	typeof(B) b = (B); \
53a85cb24fSFrançois Tigeot 	a + b < a; \
54a85cb24fSFrançois Tigeot })
55a85cb24fSFrançois Tigeot #endif
56a85cb24fSFrançois Tigeot 
57a85cb24fSFrançois Tigeot #define range_overflows(start, size, max) ({ \
58a85cb24fSFrançois Tigeot 	typeof(start) start__ = (start); \
59a85cb24fSFrançois Tigeot 	typeof(size) size__ = (size); \
60a85cb24fSFrançois Tigeot 	typeof(max) max__ = (max); \
61a85cb24fSFrançois Tigeot 	(void)(&start__ == &size__); \
62a85cb24fSFrançois Tigeot 	(void)(&start__ == &max__); \
63a85cb24fSFrançois Tigeot 	start__ > max__ || size__ > max__ - start__; \
64a85cb24fSFrançois Tigeot })
65a85cb24fSFrançois Tigeot 
66a85cb24fSFrançois Tigeot #define range_overflows_t(type, start, size, max) \
67a85cb24fSFrançois Tigeot 	range_overflows((type)(start), (type)(size), (type)(max))
68a85cb24fSFrançois Tigeot 
69a85cb24fSFrançois Tigeot /* Note we don't consider signbits :| */
70a85cb24fSFrançois Tigeot #define overflows_type(x, T) \
71a85cb24fSFrançois Tigeot 	(sizeof(x) > sizeof(T) && (x) >> (sizeof(T) * BITS_PER_BYTE))
72a85cb24fSFrançois Tigeot 
73*3f2dd94aSFrançois Tigeot #define ptr_mask_bits(ptr, n) ({					\
74a85cb24fSFrançois Tigeot 	unsigned long __v = (unsigned long)(ptr);			\
75*3f2dd94aSFrançois Tigeot 	(typeof(ptr))(__v & -BIT(n));					\
76a85cb24fSFrançois Tigeot })
77a85cb24fSFrançois Tigeot 
78*3f2dd94aSFrançois Tigeot #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
79*3f2dd94aSFrançois Tigeot 
80*3f2dd94aSFrançois Tigeot #define ptr_unpack_bits(ptr, bits, n) ({				\
81a85cb24fSFrançois Tigeot 	unsigned long __v = (unsigned long)(ptr);			\
82*3f2dd94aSFrançois Tigeot 	*(bits) = __v & (BIT(n) - 1);					\
83*3f2dd94aSFrançois Tigeot 	(typeof(ptr))(__v & -BIT(n));					\
84a85cb24fSFrançois Tigeot })
85a85cb24fSFrançois Tigeot 
86*3f2dd94aSFrançois Tigeot #define ptr_pack_bits(ptr, bits, n)					\
87a85cb24fSFrançois Tigeot 	((typeof(ptr))((unsigned long)(ptr) | (bits)))
88a85cb24fSFrançois Tigeot 
89*3f2dd94aSFrançois Tigeot #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
90*3f2dd94aSFrançois Tigeot #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
91*3f2dd94aSFrançois Tigeot #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
92*3f2dd94aSFrançois Tigeot #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
93*3f2dd94aSFrançois Tigeot 
94a85cb24fSFrançois Tigeot #define ptr_offset(ptr, member) offsetof(typeof(*(ptr)), member)
95a85cb24fSFrançois Tigeot 
96a85cb24fSFrançois Tigeot #define fetch_and_zero(ptr) ({						\
97a85cb24fSFrançois Tigeot 	typeof(*ptr) __T = *(ptr);					\
98a85cb24fSFrançois Tigeot 	*(ptr) = (typeof(*ptr))0;					\
99a85cb24fSFrançois Tigeot 	__T;								\
100a85cb24fSFrançois Tigeot })
101a85cb24fSFrançois Tigeot 
ptr_to_u64(const void * ptr)102*3f2dd94aSFrançois Tigeot static inline u64 ptr_to_u64(const void *ptr)
103*3f2dd94aSFrançois Tigeot {
104*3f2dd94aSFrançois Tigeot 	return (uintptr_t)ptr;
105*3f2dd94aSFrançois Tigeot }
106*3f2dd94aSFrançois Tigeot 
107*3f2dd94aSFrançois Tigeot #define u64_to_ptr(T, x) ({						\
108*3f2dd94aSFrançois Tigeot 	typecheck(u64, x);						\
109*3f2dd94aSFrançois Tigeot 	(T *)(uintptr_t)(x);						\
110*3f2dd94aSFrançois Tigeot })
111*3f2dd94aSFrançois Tigeot 
112*3f2dd94aSFrançois Tigeot #define __mask_next_bit(mask) ({					\
113*3f2dd94aSFrançois Tigeot 	int __idx = ffs(mask) - 1;					\
114*3f2dd94aSFrançois Tigeot 	mask &= ~BIT(__idx);						\
115*3f2dd94aSFrançois Tigeot 	__idx;								\
116*3f2dd94aSFrançois Tigeot })
117*3f2dd94aSFrançois Tigeot 
118*3f2dd94aSFrançois Tigeot #include <linux/list.h>
119*3f2dd94aSFrançois Tigeot 
__list_del_many(struct list_head * head,struct list_head * first)120*3f2dd94aSFrançois Tigeot static inline void __list_del_many(struct list_head *head,
121*3f2dd94aSFrançois Tigeot 				   struct list_head *first)
122*3f2dd94aSFrançois Tigeot {
123*3f2dd94aSFrançois Tigeot 	first->prev = head;
124*3f2dd94aSFrançois Tigeot 	WRITE_ONCE(head->next, first);
125*3f2dd94aSFrançois Tigeot }
126*3f2dd94aSFrançois Tigeot 
127*3f2dd94aSFrançois Tigeot /*
128*3f2dd94aSFrançois Tigeot  * Wait until the work is finally complete, even if it tries to postpone
129*3f2dd94aSFrançois Tigeot  * by requeueing itself. Note, that if the worker never cancels itself,
130*3f2dd94aSFrançois Tigeot  * we will spin forever.
131*3f2dd94aSFrançois Tigeot  */
drain_delayed_work(struct delayed_work * dw)132*3f2dd94aSFrançois Tigeot static inline void drain_delayed_work(struct delayed_work *dw)
133*3f2dd94aSFrançois Tigeot {
134*3f2dd94aSFrançois Tigeot 	do {
135*3f2dd94aSFrançois Tigeot 		while (flush_delayed_work(dw))
136*3f2dd94aSFrançois Tigeot 			;
137*3f2dd94aSFrançois Tigeot 	} while (delayed_work_pending(dw));
138*3f2dd94aSFrançois Tigeot }
139*3f2dd94aSFrançois Tigeot 
140a85cb24fSFrançois Tigeot #endif /* !__I915_UTILS_H */
141