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