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
40 #define drm_i915_private inteldrm_softc
41
42 struct drm_i915_private;
43 struct timeout;
44
45 #define FDO_BUG_URL "https://drm.pages.freedesktop.org/intel-docs/how-to-file-i915-bugs.html"
46
47 #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
48 __stringify(x), (long)(x))
49
50 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
51
52 int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
53 const char *func, int line);
54 #define i915_inject_probe_error(_i915, _err) \
55 __i915_inject_probe_error((_i915), (_err), __func__, __LINE__)
56 bool i915_error_injected(void);
57
58 #else
59
60 #define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; })
61 #define i915_error_injected() false
62
63 #endif
64
65 #define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV)
66
67 #define i915_probe_error(i915, fmt, ...) ({ \
68 if (i915_error_injected()) \
69 drm_dbg(&(i915)->drm, fmt, ##__VA_ARGS__); \
70 else \
71 drm_err(&(i915)->drm, fmt, ##__VA_ARGS__); \
72 })
73
74 #define range_overflows(start, size, max) ({ \
75 typeof(start) start__ = (start); \
76 typeof(size) size__ = (size); \
77 typeof(max) max__ = (max); \
78 (void)(&start__ == &size__); \
79 (void)(&start__ == &max__); \
80 start__ >= max__ || size__ > max__ - start__; \
81 })
82
83 #define range_overflows_t(type, start, size, max) \
84 range_overflows((type)(start), (type)(size), (type)(max))
85
86 #define range_overflows_end(start, size, max) ({ \
87 typeof(start) start__ = (start); \
88 typeof(size) size__ = (size); \
89 typeof(max) max__ = (max); \
90 (void)(&start__ == &size__); \
91 (void)(&start__ == &max__); \
92 start__ > max__ || size__ > max__ - start__; \
93 })
94
95 #define range_overflows_end_t(type, start, size, max) \
96 range_overflows_end((type)(start), (type)(size), (type)(max))
97
98 /* Note we don't consider signbits :| */
99 #define overflows_type(x, T) \
100 (sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T))
101
102 #define ptr_mask_bits(ptr, n) ({ \
103 unsigned long __v = (unsigned long)(ptr); \
104 (typeof(ptr))(__v & -BIT(n)); \
105 })
106
107 #define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1))
108
109 #define ptr_unpack_bits(ptr, bits, n) ({ \
110 unsigned long __v = (unsigned long)(ptr); \
111 *(bits) = __v & (BIT(n) - 1); \
112 (typeof(ptr))(__v & -BIT(n)); \
113 })
114
115 #define ptr_pack_bits(ptr, bits, n) ({ \
116 unsigned long __bits = (bits); \
117 GEM_BUG_ON(__bits & -BIT(n)); \
118 ((typeof(ptr))((unsigned long)(ptr) | __bits)); \
119 })
120
121 #define ptr_dec(ptr) ({ \
122 unsigned long __v = (unsigned long)(ptr); \
123 (typeof(ptr))(__v - 1); \
124 })
125
126 #define ptr_inc(ptr) ({ \
127 unsigned long __v = (unsigned long)(ptr); \
128 (typeof(ptr))(__v + 1); \
129 })
130
131 #define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT)
132 #define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT)
133 #define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT)
134 #define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT)
135
136 #define fetch_and_zero(ptr) ({ \
137 typeof(*ptr) __T = *(ptr); \
138 *(ptr) = (typeof(*ptr))0; \
139 __T; \
140 })
141
ptrdiff(const void * a,const void * b)142 static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
143 {
144 return a - b;
145 }
146
147 /*
148 * container_of_user: Extract the superclass from a pointer to a member.
149 *
150 * Exactly like container_of() with the exception that it plays nicely
151 * with sparse for __user @ptr.
152 */
153 #define container_of_user(ptr, type, member) ({ \
154 void __user *__mptr = (void __user *)(ptr); \
155 BUILD_BUG_ON_MSG(!__same_type(*(ptr), typeof_member(type, member)) && \
156 !__same_type(*(ptr), void), \
157 "pointer type mismatch in container_of()"); \
158 ((type __user *)(__mptr - offsetof(type, member))); })
159
160 /*
161 * check_user_mbz: Check that a user value exists and is zero
162 *
163 * Frequently in our uABI we reserve space for future extensions, and
164 * two ensure that userspace is prepared we enforce that space must
165 * be zero. (Then any future extension can safely assume a default value
166 * of 0.)
167 *
168 * check_user_mbz() combines checking that the user pointer is accessible
169 * and that the contained value is zero.
170 *
171 * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
172 */
173 #define check_user_mbz(U) ({ \
174 typeof(*(U)) mbz__; \
175 get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \
176 })
177
178 #define u64_to_ptr(T, x) ({ \
179 typecheck(u64, x); \
180 (T *)(uintptr_t)(x); \
181 })
182
183 #define __mask_next_bit(mask) ({ \
184 int __idx = ffs(mask) - 1; \
185 mask &= ~BIT(__idx); \
186 __idx; \
187 })
188
is_power_of_2_u64(u64 n)189 static inline bool is_power_of_2_u64(u64 n)
190 {
191 return (n != 0 && ((n & (n - 1)) == 0));
192 }
193
__list_del_many(struct list_head * head,struct list_head * first)194 static inline void __list_del_many(struct list_head *head,
195 struct list_head *first)
196 {
197 first->prev = head;
198 WRITE_ONCE(head->next, first);
199 }
200
list_is_last_rcu(const struct list_head * list,const struct list_head * head)201 static inline int list_is_last_rcu(const struct list_head *list,
202 const struct list_head *head)
203 {
204 return READ_ONCE(list->next) == head;
205 }
206
msecs_to_jiffies_timeout(const unsigned int m)207 static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m)
208 {
209 unsigned long j = msecs_to_jiffies(m);
210
211 return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
212 }
213
214 /*
215 * If you need to wait X milliseconds between events A and B, but event B
216 * doesn't happen exactly after event A, you record the timestamp (jiffies) of
217 * when event A happened, then just before event B you call this function and
218 * pass the timestamp as the first argument, and X as the second argument.
219 */
220 static inline void
wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies,int to_wait_ms)221 wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms)
222 {
223 unsigned long target_jiffies, tmp_jiffies, remaining_jiffies;
224
225 /*
226 * Don't re-read the value of "jiffies" every time since it may change
227 * behind our back and break the math.
228 */
229 tmp_jiffies = jiffies;
230 target_jiffies = timestamp_jiffies +
231 msecs_to_jiffies_timeout(to_wait_ms);
232
233 if (time_after(target_jiffies, tmp_jiffies)) {
234 remaining_jiffies = target_jiffies - tmp_jiffies;
235 while (remaining_jiffies)
236 remaining_jiffies =
237 schedule_timeout_uninterruptible(remaining_jiffies);
238 }
239 }
240
241 /*
242 * __wait_for - magic wait macro
243 *
244 * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
245 * important that we check the condition again after having timed out, since the
246 * timeout could be due to preemption or similar and we've never had a chance to
247 * check the condition before the timeout.
248 */
249 #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
250 const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
251 long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
252 int ret__; \
253 might_sleep(); \
254 for (;;) { \
255 const bool expired__ = ktime_after(ktime_get_raw(), end__); \
256 OP; \
257 /* Guarantee COND check prior to timeout */ \
258 barrier(); \
259 if (COND) { \
260 ret__ = 0; \
261 break; \
262 } \
263 if (expired__) { \
264 ret__ = -ETIMEDOUT; \
265 break; \
266 } \
267 usleep_range(wait__, wait__ * 2); \
268 if (wait__ < (Wmax)) \
269 wait__ <<= 1; \
270 } \
271 ret__; \
272 })
273
274 #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
275 (Wmax))
276 #define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000)
277
278 /* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */
279 #if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT)
280 # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic())
281 #else
282 # define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0)
283 #endif
284
285 #define _wait_for_atomic(COND, US, ATOMIC) \
286 ({ \
287 int cpu, ret, timeout = (US) * 1000; \
288 u64 base; \
289 _WAIT_FOR_ATOMIC_CHECK(ATOMIC); \
290 if (!(ATOMIC)) { \
291 preempt_disable(); \
292 cpu = smp_processor_id(); \
293 } \
294 base = local_clock(); \
295 for (;;) { \
296 u64 now = local_clock(); \
297 if (!(ATOMIC)) \
298 preempt_enable(); \
299 /* Guarantee COND check prior to timeout */ \
300 barrier(); \
301 if (COND) { \
302 ret = 0; \
303 break; \
304 } \
305 if (now - base >= timeout) { \
306 ret = -ETIMEDOUT; \
307 break; \
308 } \
309 cpu_relax(); \
310 if (!(ATOMIC)) { \
311 preempt_disable(); \
312 if (unlikely(cpu != smp_processor_id())) { \
313 timeout -= now - base; \
314 cpu = smp_processor_id(); \
315 base = local_clock(); \
316 } \
317 } \
318 } \
319 ret; \
320 })
321
322 #define wait_for_us(COND, US) \
323 ({ \
324 int ret__; \
325 BUILD_BUG_ON(!__builtin_constant_p(US)); \
326 if ((US) > 10) \
327 ret__ = _wait_for((COND), (US), 10, 10); \
328 else \
329 ret__ = _wait_for_atomic((COND), (US), 0); \
330 ret__; \
331 })
332
333 #define wait_for_atomic_us(COND, US) \
334 ({ \
335 BUILD_BUG_ON(!__builtin_constant_p(US)); \
336 BUILD_BUG_ON((US) > 50000); \
337 _wait_for_atomic((COND), (US), 1); \
338 })
339
340 #define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000)
341
342 #define KHz(x) (1000 * (x))
343 #define MHz(x) KHz(1000 * (x))
344
345 void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
__add_taint_for_CI(unsigned int taint)346 static inline void __add_taint_for_CI(unsigned int taint)
347 {
348 /*
349 * The system is "ok", just about surviving for the user, but
350 * CI results are now unreliable as the HW is very suspect.
351 * CI checks the taint state after every test and will reboot
352 * the machine if the kernel is tainted.
353 */
354 add_taint(taint, LOCKDEP_STILL_OK);
355 }
356
357 void cancel_timer(struct timeout *t);
358 void set_timer_ms(struct timeout *t, unsigned long timeout);
359
timer_active(const struct timeout * t)360 static inline bool timer_active(const struct timeout *t)
361 {
362 #ifdef __linux__
363 return READ_ONCE(t->expires);
364 #else
365 return READ_ONCE(t->to_time);
366 #endif
367 }
368
timer_expired(const struct timeout * t)369 static inline bool timer_expired(const struct timeout *t)
370 {
371 return timer_active(t) && !timer_pending(t);
372 }
373
i915_run_as_guest(void)374 static inline bool i915_run_as_guest(void)
375 {
376 #if IS_ENABLED(CONFIG_X86)
377 return !hypervisor_is_type(X86_HYPER_NATIVE);
378 #else
379 /* Not supported yet */
380 return false;
381 #endif
382 }
383
384 bool i915_vtd_active(struct drm_i915_private *i915);
385
386 bool i915_direct_stolen_access(struct drm_i915_private *i915);
387
388 #endif /* !__I915_UTILS_H */
389