Loading...
1/*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25#ifndef __I915_UTILS_H
26#define __I915_UTILS_H
27
28#include <linux/list.h>
29#include <linux/overflow.h>
30#include <linux/sched.h>
31#include <linux/string_helpers.h>
32#include <linux/types.h>
33#include <linux/workqueue.h>
34#include <linux/sched/clock.h>
35
36#ifdef CONFIG_X86
37#include <asm/hypervisor.h>
38#endif
39
40struct drm_i915_private;
41struct timer_list;
42
43#define FDO_BUG_URL "https://gitlab.freedesktop.org/drm/intel/-/wikis/How-to-file-i915-bugs"
44
45#define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
46 __stringify(x), (long)(x))
47
48void __printf(3, 4)
49__i915_printk(struct drm_i915_private *dev_priv, const char *level,
50 const char *fmt, ...);
51
52#define i915_report_error(dev_priv, fmt, ...) \
53 __i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__)
54
55#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
56
57int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
58 const char *func, int line);
59#define i915_inject_probe_error(_i915, _err) \
60 __i915_inject_probe_error((_i915), (_err), __func__, __LINE__)
61bool i915_error_injected(void);
62
63#else
64
65#define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; })
66#define i915_error_injected() false
67
68#endif
69
70#define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV)
71
72#define i915_probe_error(i915, fmt, ...) \
73 __i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \
74 fmt, ##__VA_ARGS__)
75
76#if defined(GCC_VERSION) && GCC_VERSION >= 70000
77#define add_overflows_t(T, A, B) \
78 __builtin_add_overflow_p((A), (B), (T)0)
79#else
80#define add_overflows_t(T, A, B) ({ \
81 typeof(A) a = (A); \
82 typeof(B) b = (B); \
83 (T)(a + b) < a; \
84})
85#endif
86
87#define add_overflows(A, B) \
88 add_overflows_t(typeof((A) + (B)), (A), (B))
89
90#define range_overflows(start, size, max) ({ \
91 typeof(start) start__ = (start); \
92 typeof(size) size__ = (size); \
93 typeof(max) max__ = (max); \
94 (void)(&start__ == &size__); \
95 (void)(&start__ == &max__); \
96 start__ >= max__ || size__ > max__ - start__; \
97})
98
99#define range_overflows_t(type, start, size, max) \
100 range_overflows((type)(start), (type)(size), (type)(max))
101
102#define range_overflows_end(start, size, max) ({ \
103 typeof(start) start__ = (start); \
104 typeof(size) size__ = (size); \
105 typeof(max) max__ = (max); \
106 (void)(&start__ == &size__); \
107 (void)(&start__ == &max__); \
108 start__ > max__ || size__ > max__ - start__; \
109})
110
111#define range_overflows_end_t(type, start, size, max) \
112 range_overflows_end((type)(start), (type)(size), (type)(max))
113
114#define ptr_mask_bits(ptr, n) ({ \
115 unsigned long __v = (unsigned long)(ptr); \
116 (typeof(ptr))(__v & -BIT(n)); \
117})
118
119#define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
120
121#define ptr_unpack_bits(ptr, bits, n) ({ \
122 unsigned long __v = (unsigned long)(ptr); \
123 *(bits) = __v & (BIT(n) - 1); \
124 (typeof(ptr))(__v & -BIT(n)); \
125})
126
127#define ptr_pack_bits(ptr, bits, n) ({ \
128 unsigned long __bits = (bits); \
129 GEM_BUG_ON(__bits & -BIT(n)); \
130 ((typeof(ptr))((unsigned long)(ptr) | __bits)); \
131})
132
133#define ptr_dec(ptr) ({ \
134 unsigned long __v = (unsigned long)(ptr); \
135 (typeof(ptr))(__v - 1); \
136})
137
138#define ptr_inc(ptr) ({ \
139 unsigned long __v = (unsigned long)(ptr); \
140 (typeof(ptr))(__v + 1); \
141})
142
143#define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
144#define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
145#define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
146#define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
147
148#define struct_member(T, member) (((T *)0)->member)
149
150#define fetch_and_zero(ptr) ({ \
151 typeof(*ptr) __T = *(ptr); \
152 *(ptr) = (typeof(*ptr))0; \
153 __T; \
154})
155
156static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
157{
158 return a - b;
159}
160
161/*
162 * container_of_user: Extract the superclass from a pointer to a member.
163 *
164 * Exactly like container_of() with the exception that it plays nicely
165 * with sparse for __user @ptr.
166 */
167#define container_of_user(ptr, type, member) ({ \
168 void __user *__mptr = (void __user *)(ptr); \
169 BUILD_BUG_ON_MSG(!__same_type(*(ptr), struct_member(type, member)) && \
170 !__same_type(*(ptr), void), \
171 "pointer type mismatch in container_of()"); \
172 ((type __user *)(__mptr - offsetof(type, member))); })
173
174/*
175 * check_user_mbz: Check that a user value exists and is zero
176 *
177 * Frequently in our uABI we reserve space for future extensions, and
178 * two ensure that userspace is prepared we enforce that space must
179 * be zero. (Then any future extension can safely assume a default value
180 * of 0.)
181 *
182 * check_user_mbz() combines checking that the user pointer is accessible
183 * and that the contained value is zero.
184 *
185 * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
186 */
187#define check_user_mbz(U) ({ \
188 typeof(*(U)) mbz__; \
189 get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \
190})
191
192#define u64_to_ptr(T, x) ({ \
193 typecheck(u64, x); \
194 (T *)(uintptr_t)(x); \
195})
196
197#define __mask_next_bit(mask) ({ \
198 int __idx = ffs(mask) - 1; \
199 mask &= ~BIT(__idx); \
200 __idx; \
201})
202
203static inline bool is_power_of_2_u64(u64 n)
204{
205 return (n != 0 && ((n & (n - 1)) == 0));
206}
207
208static inline void __list_del_many(struct list_head *head,
209 struct list_head *first)
210{
211 first->prev = head;
212 WRITE_ONCE(head->next, first);
213}
214
215static inline int list_is_last_rcu(const struct list_head *list,
216 const struct list_head *head)
217{
218 return READ_ONCE(list->next) == head;
219}
220
221static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
222{
223 unsigned long j = msecs_to_jiffies(m);
224
225 return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
226}
227
228/*
229 * If you need to wait X milliseconds between events A and B, but event B
230 * doesn't happen exactly after event A, you record the timestamp (jiffies) of
231 * when event A happened, then just before event B you call this function and
232 * pass the timestamp as the first argument, and X as the second argument.
233 */
234static inline void
235wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
236{
237 unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
238
239 /*
240 * Don't re-read the value of "jiffies" every time since it may change
241 * behind our back and break the math.
242 */
243 tmp_jiffies = jiffies;
244 target_jiffies = timestamp_jiffies +
245 msecs_to_jiffies_timeout(to_wait_ms);
246
247 if (time_after(target_jiffies, tmp_jiffies)) {
248 remaining_jiffies = target_jiffies - tmp_jiffies;
249 while (remaining_jiffies)
250 remaining_jiffies =
251 schedule_timeout_uninterruptible(remaining_jiffies);
252 }
253}
254
255/**
256 * __wait_for - magic wait macro
257 *
258 * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
259 * important that we check the condition again after having timed out, since the
260 * timeout could be due to preemption or similar and we've never had a chance to
261 * check the condition before the timeout.
262 */
263#define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
264 const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
265 long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
266 int ret__; \
267 might_sleep(); \
268 for (;;) { \
269 const bool expired__ = ktime_after(ktime_get_raw(), end__); \
270 OP; \
271 /* Guarantee COND check prior to timeout */ \
272 barrier(); \
273 if (COND) { \
274 ret__ = 0; \
275 break; \
276 } \
277 if (expired__) { \
278 ret__ = -ETIMEDOUT; \
279 break; \
280 } \
281 usleep_range(wait__, wait__ * 2); \
282 if (wait__ < (Wmax)) \
283 wait__ <<= 1; \
284 } \
285 ret__; \
286})
287
288#define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
289 (Wmax))
290#define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000)
291
292/* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
293#if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
294# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
295#else
296# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
297#endif
298
299#define _wait_for_atomic(COND, US, ATOMIC) \
300({ \
301 int cpu, ret, timeout = (US) * 1000; \
302 u64 base; \
303 _WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
304 if (!(ATOMIC)) { \
305 preempt_disable(); \
306 cpu = smp_processor_id(); \
307 } \
308 base = local_clock(); \
309 for (;;) { \
310 u64 now = local_clock(); \
311 if (!(ATOMIC)) \
312 preempt_enable(); \
313 /* Guarantee COND check prior to timeout */ \
314 barrier(); \
315 if (COND) { \
316 ret = 0; \
317 break; \
318 } \
319 if (now - base >= timeout) { \
320 ret = -ETIMEDOUT; \
321 break; \
322 } \
323 cpu_relax(); \
324 if (!(ATOMIC)) { \
325 preempt_disable(); \
326 if (unlikely(cpu != smp_processor_id())) { \
327 timeout -= now - base; \
328 cpu = smp_processor_id(); \
329 base = local_clock(); \
330 } \
331 } \
332 } \
333 ret; \
334})
335
336#define wait_for_us(COND, US) \
337({ \
338 int ret__; \
339 BUILD_BUG_ON(!__builtin_constant_p(US)); \
340 if ((US) > 10) \
341 ret__ = _wait_for((COND), (US), 10, 10); \
342 else \
343 ret__ = _wait_for_atomic((COND), (US), 0); \
344 ret__; \
345})
346
347#define wait_for_atomic_us(COND, US) \
348({ \
349 BUILD_BUG_ON(!__builtin_constant_p(US)); \
350 BUILD_BUG_ON((US) > 50000); \
351 _wait_for_atomic((COND), (US), 1); \
352})
353
354#define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
355
356#define KHz(x) (1000 * (x))
357#define MHz(x) KHz(1000 * (x))
358
359void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
360static inline void __add_taint_for_CI(unsigned int taint)
361{
362 /*
363 * The system is "ok", just about surviving for the user, but
364 * CI results are now unreliable as the HW is very suspect.
365 * CI checks the taint state after every test and will reboot
366 * the machine if the kernel is tainted.
367 */
368 add_taint(taint, LOCKDEP_STILL_OK);
369}
370
371void cancel_timer(struct timer_list *t);
372void set_timer_ms(struct timer_list *t, unsigned long timeout);
373
374static inline bool timer_active(const struct timer_list *t)
375{
376 return READ_ONCE(t->expires);
377}
378
379static inline bool timer_expired(const struct timer_list *t)
380{
381 return timer_active(t) && !timer_pending(t);
382}
383
384static inline bool i915_run_as_guest(void)
385{
386#if IS_ENABLED(CONFIG_X86)
387 return !hypervisor_is_type(X86_HYPER_NATIVE);
388#else
389 /* Not supported yet */
390 return false;
391#endif
392}
393
394bool i915_vtd_active(struct drm_i915_private *i915);
395
396#endif /* !__I915_UTILS_H */
1/*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25#ifndef __I915_UTILS_H
26#define __I915_UTILS_H
27
28#include <linux/list.h>
29#include <linux/overflow.h>
30#include <linux/sched.h>
31#include <linux/types.h>
32#include <linux/workqueue.h>
33
34struct drm_i915_private;
35struct timer_list;
36
37#define FDO_BUG_URL "https://gitlab.freedesktop.org/drm/intel/-/wikis/How-to-file-i915-bugs"
38
39#undef WARN_ON
40/* Many gcc seem to no see through this and fall over :( */
41#if 0
42#define WARN_ON(x) ({ \
43 bool __i915_warn_cond = (x); \
44 if (__builtin_constant_p(__i915_warn_cond)) \
45 BUILD_BUG_ON(__i915_warn_cond); \
46 WARN(__i915_warn_cond, "WARN_ON(" #x ")"); })
47#else
48#define WARN_ON(x) WARN((x), "%s", "WARN_ON(" __stringify(x) ")")
49#endif
50
51#undef WARN_ON_ONCE
52#define WARN_ON_ONCE(x) WARN_ONCE((x), "%s", "WARN_ON_ONCE(" __stringify(x) ")")
53
54#define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
55 __stringify(x), (long)(x))
56
57void __printf(3, 4)
58__i915_printk(struct drm_i915_private *dev_priv, const char *level,
59 const char *fmt, ...);
60
61#define i915_report_error(dev_priv, fmt, ...) \
62 __i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__)
63
64#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
65
66int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
67 const char *func, int line);
68#define i915_inject_probe_error(_i915, _err) \
69 __i915_inject_probe_error((_i915), (_err), __func__, __LINE__)
70bool i915_error_injected(void);
71
72#else
73
74#define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; })
75#define i915_error_injected() false
76
77#endif
78
79#define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV)
80
81#define i915_probe_error(i915, fmt, ...) \
82 __i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \
83 fmt, ##__VA_ARGS__)
84
85#if defined(GCC_VERSION) && GCC_VERSION >= 70000
86#define add_overflows_t(T, A, B) \
87 __builtin_add_overflow_p((A), (B), (T)0)
88#else
89#define add_overflows_t(T, A, B) ({ \
90 typeof(A) a = (A); \
91 typeof(B) b = (B); \
92 (T)(a + b) < a; \
93})
94#endif
95
96#define add_overflows(A, B) \
97 add_overflows_t(typeof((A) + (B)), (A), (B))
98
99#define range_overflows(start, size, max) ({ \
100 typeof(start) start__ = (start); \
101 typeof(size) size__ = (size); \
102 typeof(max) max__ = (max); \
103 (void)(&start__ == &size__); \
104 (void)(&start__ == &max__); \
105 start__ >= max__ || size__ > max__ - start__; \
106})
107
108#define range_overflows_t(type, start, size, max) \
109 range_overflows((type)(start), (type)(size), (type)(max))
110
111#define range_overflows_end(start, size, max) ({ \
112 typeof(start) start__ = (start); \
113 typeof(size) size__ = (size); \
114 typeof(max) max__ = (max); \
115 (void)(&start__ == &size__); \
116 (void)(&start__ == &max__); \
117 start__ > max__ || size__ > max__ - start__; \
118})
119
120#define range_overflows_end_t(type, start, size, max) \
121 range_overflows_end((type)(start), (type)(size), (type)(max))
122
123/* Note we don't consider signbits :| */
124#define overflows_type(x, T) \
125 (sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T))
126
127static inline bool
128__check_struct_size(size_t base, size_t arr, size_t count, size_t *size)
129{
130 size_t sz;
131
132 if (check_mul_overflow(count, arr, &sz))
133 return false;
134
135 if (check_add_overflow(sz, base, &sz))
136 return false;
137
138 *size = sz;
139 return true;
140}
141
142/**
143 * check_struct_size() - Calculate size of structure with trailing array.
144 * @p: Pointer to the structure.
145 * @member: Name of the array member.
146 * @n: Number of elements in the array.
147 * @sz: Total size of structure and array
148 *
149 * Calculates size of memory needed for structure @p followed by an
150 * array of @n @member elements, like struct_size() but reports
151 * whether it overflowed, and the resultant size in @sz
152 *
153 * Return: false if the calculation overflowed.
154 */
155#define check_struct_size(p, member, n, sz) \
156 likely(__check_struct_size(sizeof(*(p)), \
157 sizeof(*(p)->member) + __must_be_array((p)->member), \
158 n, sz))
159
160#define ptr_mask_bits(ptr, n) ({ \
161 unsigned long __v = (unsigned long)(ptr); \
162 (typeof(ptr))(__v & -BIT(n)); \
163})
164
165#define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
166
167#define ptr_unpack_bits(ptr, bits, n) ({ \
168 unsigned long __v = (unsigned long)(ptr); \
169 *(bits) = __v & (BIT(n) - 1); \
170 (typeof(ptr))(__v & -BIT(n)); \
171})
172
173#define ptr_pack_bits(ptr, bits, n) ({ \
174 unsigned long __bits = (bits); \
175 GEM_BUG_ON(__bits & -BIT(n)); \
176 ((typeof(ptr))((unsigned long)(ptr) | __bits)); \
177})
178
179#define ptr_dec(ptr) ({ \
180 unsigned long __v = (unsigned long)(ptr); \
181 (typeof(ptr))(__v - 1); \
182})
183
184#define ptr_inc(ptr) ({ \
185 unsigned long __v = (unsigned long)(ptr); \
186 (typeof(ptr))(__v + 1); \
187})
188
189#define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
190#define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
191#define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
192#define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
193
194#define struct_member(T, member) (((T *)0)->member)
195
196#define ptr_offset(ptr, member) offsetof(typeof(*(ptr)), member)
197
198#define fetch_and_zero(ptr) ({ \
199 typeof(*ptr) __T = *(ptr); \
200 *(ptr) = (typeof(*ptr))0; \
201 __T; \
202})
203
204static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
205{
206 return a - b;
207}
208
209/*
210 * container_of_user: Extract the superclass from a pointer to a member.
211 *
212 * Exactly like container_of() with the exception that it plays nicely
213 * with sparse for __user @ptr.
214 */
215#define container_of_user(ptr, type, member) ({ \
216 void __user *__mptr = (void __user *)(ptr); \
217 BUILD_BUG_ON_MSG(!__same_type(*(ptr), struct_member(type, member)) && \
218 !__same_type(*(ptr), void), \
219 "pointer type mismatch in container_of()"); \
220 ((type __user *)(__mptr - offsetof(type, member))); })
221
222/*
223 * check_user_mbz: Check that a user value exists and is zero
224 *
225 * Frequently in our uABI we reserve space for future extensions, and
226 * two ensure that userspace is prepared we enforce that space must
227 * be zero. (Then any future extension can safely assume a default value
228 * of 0.)
229 *
230 * check_user_mbz() combines checking that the user pointer is accessible
231 * and that the contained value is zero.
232 *
233 * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
234 */
235#define check_user_mbz(U) ({ \
236 typeof(*(U)) mbz__; \
237 get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \
238})
239
240static inline u64 ptr_to_u64(const void *ptr)
241{
242 return (uintptr_t)ptr;
243}
244
245#define u64_to_ptr(T, x) ({ \
246 typecheck(u64, x); \
247 (T *)(uintptr_t)(x); \
248})
249
250#define __mask_next_bit(mask) ({ \
251 int __idx = ffs(mask) - 1; \
252 mask &= ~BIT(__idx); \
253 __idx; \
254})
255
256static inline bool is_power_of_2_u64(u64 n)
257{
258 return (n != 0 && ((n & (n - 1)) == 0));
259}
260
261static inline void __list_del_many(struct list_head *head,
262 struct list_head *first)
263{
264 first->prev = head;
265 WRITE_ONCE(head->next, first);
266}
267
268static inline int list_is_last_rcu(const struct list_head *list,
269 const struct list_head *head)
270{
271 return READ_ONCE(list->next) == head;
272}
273
274static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
275{
276 unsigned long j = msecs_to_jiffies(m);
277
278 return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
279}
280
281/*
282 * If you need to wait X milliseconds between events A and B, but event B
283 * doesn't happen exactly after event A, you record the timestamp (jiffies) of
284 * when event A happened, then just before event B you call this function and
285 * pass the timestamp as the first argument, and X as the second argument.
286 */
287static inline void
288wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
289{
290 unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
291
292 /*
293 * Don't re-read the value of "jiffies" every time since it may change
294 * behind our back and break the math.
295 */
296 tmp_jiffies = jiffies;
297 target_jiffies = timestamp_jiffies +
298 msecs_to_jiffies_timeout(to_wait_ms);
299
300 if (time_after(target_jiffies, tmp_jiffies)) {
301 remaining_jiffies = target_jiffies - tmp_jiffies;
302 while (remaining_jiffies)
303 remaining_jiffies =
304 schedule_timeout_uninterruptible(remaining_jiffies);
305 }
306}
307
308/**
309 * __wait_for - magic wait macro
310 *
311 * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
312 * important that we check the condition again after having timed out, since the
313 * timeout could be due to preemption or similar and we've never had a chance to
314 * check the condition before the timeout.
315 */
316#define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
317 const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
318 long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
319 int ret__; \
320 might_sleep(); \
321 for (;;) { \
322 const bool expired__ = ktime_after(ktime_get_raw(), end__); \
323 OP; \
324 /* Guarantee COND check prior to timeout */ \
325 barrier(); \
326 if (COND) { \
327 ret__ = 0; \
328 break; \
329 } \
330 if (expired__) { \
331 ret__ = -ETIMEDOUT; \
332 break; \
333 } \
334 usleep_range(wait__, wait__ * 2); \
335 if (wait__ < (Wmax)) \
336 wait__ <<= 1; \
337 } \
338 ret__; \
339})
340
341#define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
342 (Wmax))
343#define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000)
344
345/* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
346#if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
347# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
348#else
349# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
350#endif
351
352#define _wait_for_atomic(COND, US, ATOMIC) \
353({ \
354 int cpu, ret, timeout = (US) * 1000; \
355 u64 base; \
356 _WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
357 if (!(ATOMIC)) { \
358 preempt_disable(); \
359 cpu = smp_processor_id(); \
360 } \
361 base = local_clock(); \
362 for (;;) { \
363 u64 now = local_clock(); \
364 if (!(ATOMIC)) \
365 preempt_enable(); \
366 /* Guarantee COND check prior to timeout */ \
367 barrier(); \
368 if (COND) { \
369 ret = 0; \
370 break; \
371 } \
372 if (now - base >= timeout) { \
373 ret = -ETIMEDOUT; \
374 break; \
375 } \
376 cpu_relax(); \
377 if (!(ATOMIC)) { \
378 preempt_disable(); \
379 if (unlikely(cpu != smp_processor_id())) { \
380 timeout -= now - base; \
381 cpu = smp_processor_id(); \
382 base = local_clock(); \
383 } \
384 } \
385 } \
386 ret; \
387})
388
389#define wait_for_us(COND, US) \
390({ \
391 int ret__; \
392 BUILD_BUG_ON(!__builtin_constant_p(US)); \
393 if ((US) > 10) \
394 ret__ = _wait_for((COND), (US), 10, 10); \
395 else \
396 ret__ = _wait_for_atomic((COND), (US), 0); \
397 ret__; \
398})
399
400#define wait_for_atomic_us(COND, US) \
401({ \
402 BUILD_BUG_ON(!__builtin_constant_p(US)); \
403 BUILD_BUG_ON((US) > 50000); \
404 _wait_for_atomic((COND), (US), 1); \
405})
406
407#define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
408
409#define KHz(x) (1000 * (x))
410#define MHz(x) KHz(1000 * (x))
411
412#define KBps(x) (1000 * (x))
413#define MBps(x) KBps(1000 * (x))
414#define GBps(x) ((u64)1000 * MBps((x)))
415
416static inline const char *yesno(bool v)
417{
418 return v ? "yes" : "no";
419}
420
421static inline const char *onoff(bool v)
422{
423 return v ? "on" : "off";
424}
425
426static inline const char *enabledisable(bool v)
427{
428 return v ? "enable" : "disable";
429}
430
431static inline const char *enableddisabled(bool v)
432{
433 return v ? "enabled" : "disabled";
434}
435
436void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
437static inline void __add_taint_for_CI(unsigned int taint)
438{
439 /*
440 * The system is "ok", just about surviving for the user, but
441 * CI results are now unreliable as the HW is very suspect.
442 * CI checks the taint state after every test and will reboot
443 * the machine if the kernel is tainted.
444 */
445 add_taint(taint, LOCKDEP_STILL_OK);
446}
447
448void cancel_timer(struct timer_list *t);
449void set_timer_ms(struct timer_list *t, unsigned long timeout);
450
451static inline bool timer_active(const struct timer_list *t)
452{
453 return READ_ONCE(t->expires);
454}
455
456static inline bool timer_expired(const struct timer_list *t)
457{
458 return timer_active(t) && !timer_pending(t);
459}
460
461/*
462 * This is a lookalike for IS_ENABLED() that takes a kconfig value,
463 * e.g. CONFIG_DRM_I915_SPIN_REQUEST, and evaluates whether it is non-zero
464 * i.e. whether the configuration is active. Wrapping up the config inside
465 * a boolean context prevents clang and smatch from complaining about potential
466 * issues in confusing logical-&& with bitwise-& for constants.
467 *
468 * Sadly IS_ENABLED() itself does not work with kconfig values.
469 *
470 * Returns 0 if @config is 0, 1 if set to any value.
471 */
472#define IS_ACTIVE(config) ((config) != 0)
473
474#endif /* !__I915_UTILS_H */