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