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