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