189613Sdes/* 292297Sdes * Copyright �� 2016 Intel Corporation 389613Sdes * 489613Sdes * Permission is hereby granted, free of charge, to any person obtaining a 589613Sdes * copy of this software and associated documentation files (the "Software"), 689613Sdes * to deal in the Software without restriction, including without limitation 789613Sdes * the rights to use, copy, modify, merge, publish, distribute, sublicense, 889613Sdes * and/or sell copies of the Software, and to permit persons to whom the 989613Sdes * Software is furnished to do so, subject to the following conditions: 1089613Sdes * 1189613Sdes * The above copyright notice and this permission notice (including the next 1289613Sdes * paragraph) shall be included in all copies or substantial portions of the 1389613Sdes * Software. 1489613Sdes * 1589613Sdes * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1689613Sdes * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1789613Sdes * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1889613Sdes * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1989613Sdes * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2089613Sdes * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2189613Sdes * IN THE SOFTWARE. 2289613Sdes * 2389613Sdes */ 2489613Sdes 2589613Sdes#ifndef __I915_UTILS_H 2689613Sdes#define __I915_UTILS_H 2789613Sdes 2889613Sdes#include <linux/list.h> 2989613Sdes#include <linux/overflow.h> 3089613Sdes#include <linux/sched.h> 3189613Sdes#include <linux/string_helpers.h> 3289613Sdes#include <linux/types.h> 3389613Sdes#include <linux/workqueue.h> 3489613Sdes#include <linux/sched/clock.h> 3589613Sdes 3689613Sdes#ifdef CONFIG_X86 3789613Sdes#include <asm/hypervisor.h> 3889613Sdes#endif 3989613Sdes 4089613Sdesstruct drm_i915_private; 4189613Sdesstruct timer_list; 4292939Smarkm 4389613Sdes#define FDO_BUG_URL "https://drm.pages.freedesktop.org/intel-docs/how-to-file-i915-bugs.html" 4489613Sdes 4589613Sdes#define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \ 4689613Sdes __stringify(x), (long)(x)) 4789613Sdes 4889613Sdesvoid __printf(3, 4) 4989613Sdes__i915_printk(struct drm_i915_private *dev_priv, const char *level, 5090229Sdes const char *fmt, ...); 5189613Sdes 5290229Sdes#define i915_report_error(dev_priv, fmt, ...) \ 5389613Sdes __i915_printk(dev_priv, KERN_ERR, fmt, ##__VA_ARGS__) 5489613Sdes 5594564Sdes#if IS_ENABLED(CONFIG_DRM_I915_DEBUG) 5694564Sdes 5789613Sdesint __i915_inject_probe_error(struct drm_i915_private *i915, int err, 5889613Sdes const char *func, int line); 5989613Sdes#define i915_inject_probe_error(_i915, _err) \ 60110968Sdes __i915_inject_probe_error((_i915), (_err), __func__, __LINE__) 6189613Sdesbool i915_error_injected(void); 6289613Sdes 6389613Sdes#else 6489613Sdes 6594564Sdes#define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; }) 6689613Sdes#define i915_error_injected() false 6794564Sdes 6889613Sdes#endif 6989613Sdes 70112056Sdes#define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV) 7194734Sdes 7294564Sdes#define i915_probe_error(i915, fmt, ...) \ 7389613Sdes __i915_printk(i915, i915_error_injected() ? KERN_DEBUG : KERN_ERR, \ 7489613Sdes fmt, ##__VA_ARGS__) 7594564Sdes 76110968Sdes#define range_overflows(start, size, max) ({ \ 77110991Sdes typeof(start) start__ = (start); \ 78110991Sdes typeof(size) size__ = (size); \ 7994564Sdes typeof(max) max__ = (max); \ 80112056Sdes (void)(&start__ == &size__); \ 81112056Sdes (void)(&start__ == &max__); \ 8294734Sdes start__ >= max__ || size__ > max__ - start__; \ 8394564Sdes}) 8489613Sdes 8589613Sdes#define range_overflows_t(type, start, size, max) \ 8694564Sdes range_overflows((type)(start), (type)(size), (type)(max)) 8789613Sdes 8889613Sdes#define range_overflows_end(start, size, max) ({ \ 8989613Sdes typeof(start) start__ = (start); \ 9094564Sdes typeof(size) size__ = (size); \ 9194564Sdes typeof(max) max__ = (max); \ 9289613Sdes (void)(&start__ == &size__); \ 9389613Sdes (void)(&start__ == &max__); \ 9494564Sdes start__ > max__ || size__ > max__ - start__; \ 9589613Sdes}) 9689613Sdes 9789613Sdes#define range_overflows_end_t(type, start, size, max) \ 98 range_overflows_end((type)(start), (type)(size), (type)(max)) 99 100#define ptr_mask_bits(ptr, n) ({ \ 101 unsigned long __v = (unsigned long)(ptr); \ 102 (typeof(ptr))(__v & -BIT(n)); \ 103}) 104 105#define ptr_unmask_bits(ptr, n) ((unsigned long)(ptr) & (BIT(n) - 1)) 106 107#define ptr_unpack_bits(ptr, bits, n) ({ \ 108 unsigned long __v = (unsigned long)(ptr); \ 109 *(bits) = __v & (BIT(n) - 1); \ 110 (typeof(ptr))(__v & -BIT(n)); \ 111}) 112 113#define ptr_pack_bits(ptr, bits, n) ({ \ 114 unsigned long __bits = (bits); \ 115 GEM_BUG_ON(__bits & -BIT(n)); \ 116 ((typeof(ptr))((unsigned long)(ptr) | __bits)); \ 117}) 118 119#define ptr_dec(ptr) ({ \ 120 unsigned long __v = (unsigned long)(ptr); \ 121 (typeof(ptr))(__v - 1); \ 122}) 123 124#define ptr_inc(ptr) ({ \ 125 unsigned long __v = (unsigned long)(ptr); \ 126 (typeof(ptr))(__v + 1); \ 127}) 128 129#define page_mask_bits(ptr) ptr_mask_bits(ptr, PAGE_SHIFT) 130#define page_unmask_bits(ptr) ptr_unmask_bits(ptr, PAGE_SHIFT) 131#define page_pack_bits(ptr, bits) ptr_pack_bits(ptr, bits, PAGE_SHIFT) 132#define page_unpack_bits(ptr, bits) ptr_unpack_bits(ptr, bits, PAGE_SHIFT) 133 134#define fetch_and_zero(ptr) ({ \ 135 typeof(*ptr) __T = *(ptr); \ 136 *(ptr) = (typeof(*ptr))0; \ 137 __T; \ 138}) 139 140static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b) 141{ 142 return a - b; 143} 144 145/* 146 * container_of_user: Extract the superclass from a pointer to a member. 147 * 148 * Exactly like container_of() with the exception that it plays nicely 149 * with sparse for __user @ptr. 150 */ 151#define container_of_user(ptr, type, member) ({ \ 152 void __user *__mptr = (void __user *)(ptr); \ 153 BUILD_BUG_ON_MSG(!__same_type(*(ptr), typeof_member(type, member)) && \ 154 !__same_type(*(ptr), void), \ 155 "pointer type mismatch in container_of()"); \ 156 ((type __user *)(__mptr - offsetof(type, member))); }) 157 158/* 159 * check_user_mbz: Check that a user value exists and is zero 160 * 161 * Frequently in our uABI we reserve space for future extensions, and 162 * two ensure that userspace is prepared we enforce that space must 163 * be zero. (Then any future extension can safely assume a default value 164 * of 0.) 165 * 166 * check_user_mbz() combines checking that the user pointer is accessible 167 * and that the contained value is zero. 168 * 169 * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success. 170 */ 171#define check_user_mbz(U) ({ \ 172 typeof(*(U)) mbz__; \ 173 get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \ 174}) 175 176#define u64_to_ptr(T, x) ({ \ 177 typecheck(u64, x); \ 178 (T *)(uintptr_t)(x); \ 179}) 180 181#define __mask_next_bit(mask) ({ \ 182 int __idx = ffs(mask) - 1; \ 183 mask &= ~BIT(__idx); \ 184 __idx; \ 185}) 186 187static inline bool is_power_of_2_u64(u64 n) 188{ 189 return (n != 0 && ((n & (n - 1)) == 0)); 190} 191 192static inline void __list_del_many(struct list_head *head, 193 struct list_head *first) 194{ 195 first->prev = head; 196 WRITE_ONCE(head->next, first); 197} 198 199static inline int list_is_last_rcu(const struct list_head *list, 200 const struct list_head *head) 201{ 202 return READ_ONCE(list->next) == head; 203} 204 205static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m) 206{ 207 unsigned long j = msecs_to_jiffies(m); 208 209 return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1); 210} 211 212/* 213 * If you need to wait X milliseconds between events A and B, but event B 214 * doesn't happen exactly after event A, you record the timestamp (jiffies) of 215 * when event A happened, then just before event B you call this function and 216 * pass the timestamp as the first argument, and X as the second argument. 217 */ 218static inline void 219wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms) 220{ 221 unsigned long target_jiffies, tmp_jiffies, remaining_jiffies; 222 223 /* 224 * Don't re-read the value of "jiffies" every time since it may change 225 * behind our back and break the math. 226 */ 227 tmp_jiffies = jiffies; 228 target_jiffies = timestamp_jiffies + 229 msecs_to_jiffies_timeout(to_wait_ms); 230 231 if (time_after(target_jiffies, tmp_jiffies)) { 232 remaining_jiffies = target_jiffies - tmp_jiffies; 233 while (remaining_jiffies) 234 remaining_jiffies = 235 schedule_timeout_uninterruptible(remaining_jiffies); 236 } 237} 238 239/* 240 * __wait_for - magic wait macro 241 * 242 * Macro to help avoid open coding check/wait/timeout patterns. Note that it's 243 * important that we check the condition again after having timed out, since the 244 * timeout could be due to preemption or similar and we've never had a chance to 245 * check the condition before the timeout. 246 */ 247#define __wait_for(OP, COND, US, Wmin, Wmax) ({ \ 248 const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \ 249 long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \ 250 int ret__; \ 251 might_sleep(); \ 252 for (;;) { \ 253 const bool expired__ = ktime_after(ktime_get_raw(), end__); \ 254 OP; \ 255 /* Guarantee COND check prior to timeout */ \ 256 barrier(); \ 257 if (COND) { \ 258 ret__ = 0; \ 259 break; \ 260 } \ 261 if (expired__) { \ 262 ret__ = -ETIMEDOUT; \ 263 break; \ 264 } \ 265 usleep_range(wait__, wait__ * 2); \ 266 if (wait__ < (Wmax)) \ 267 wait__ <<= 1; \ 268 } \ 269 ret__; \ 270}) 271 272#define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \ 273 (Wmax)) 274#define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000) 275 276/* If CONFIG_PREEMPT_COUNT is disabled, in_atomic() always reports false. */ 277#if defined(CONFIG_DRM_I915_DEBUG) && defined(CONFIG_PREEMPT_COUNT) 278# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) WARN_ON_ONCE((ATOMIC) && !in_atomic()) 279#else 280# define _WAIT_FOR_ATOMIC_CHECK(ATOMIC) do { } while (0) 281#endif 282 283#define _wait_for_atomic(COND, US, ATOMIC) \ 284({ \ 285 int cpu, ret, timeout = (US) * 1000; \ 286 u64 base; \ 287 _WAIT_FOR_ATOMIC_CHECK(ATOMIC); \ 288 if (!(ATOMIC)) { \ 289 preempt_disable(); \ 290 cpu = smp_processor_id(); \ 291 } \ 292 base = local_clock(); \ 293 for (;;) { \ 294 u64 now = local_clock(); \ 295 if (!(ATOMIC)) \ 296 preempt_enable(); \ 297 /* Guarantee COND check prior to timeout */ \ 298 barrier(); \ 299 if (COND) { \ 300 ret = 0; \ 301 break; \ 302 } \ 303 if (now - base >= timeout) { \ 304 ret = -ETIMEDOUT; \ 305 break; \ 306 } \ 307 cpu_relax(); \ 308 if (!(ATOMIC)) { \ 309 preempt_disable(); \ 310 if (unlikely(cpu != smp_processor_id())) { \ 311 timeout -= now - base; \ 312 cpu = smp_processor_id(); \ 313 base = local_clock(); \ 314 } \ 315 } \ 316 } \ 317 ret; \ 318}) 319 320#define wait_for_us(COND, US) \ 321({ \ 322 int ret__; \ 323 BUILD_BUG_ON(!__builtin_constant_p(US)); \ 324 if ((US) > 10) \ 325 ret__ = _wait_for((COND), (US), 10, 10); \ 326 else \ 327 ret__ = _wait_for_atomic((COND), (US), 0); \ 328 ret__; \ 329}) 330 331#define wait_for_atomic_us(COND, US) \ 332({ \ 333 BUILD_BUG_ON(!__builtin_constant_p(US)); \ 334 BUILD_BUG_ON((US) > 50000); \ 335 _wait_for_atomic((COND), (US), 1); \ 336}) 337 338#define wait_for_atomic(COND, MS) wait_for_atomic_us((COND), (MS) * 1000) 339 340#define KHz(x) (1000 * (x)) 341#define MHz(x) KHz(1000 * (x)) 342 343void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint); 344static inline void __add_taint_for_CI(unsigned int taint) 345{ 346 /* 347 * The system is "ok", just about surviving for the user, but 348 * CI results are now unreliable as the HW is very suspect. 349 * CI checks the taint state after every test and will reboot 350 * the machine if the kernel is tainted. 351 */ 352 add_taint(taint, LOCKDEP_STILL_OK); 353} 354 355void cancel_timer(struct timer_list *t); 356void set_timer_ms(struct timer_list *t, unsigned long timeout); 357 358static inline bool timer_active(const struct timer_list *t) 359{ 360 return READ_ONCE(t->expires); 361} 362 363static inline bool timer_expired(const struct timer_list *t) 364{ 365 return timer_active(t) && !timer_pending(t); 366} 367 368static inline bool i915_run_as_guest(void) 369{ 370#if IS_ENABLED(CONFIG_X86) 371 return !hypervisor_is_type(X86_HYPER_NATIVE); 372#else 373 /* Not supported yet */ 374 return false; 375#endif 376} 377 378bool i915_vtd_active(struct drm_i915_private *i915); 379 380bool i915_direct_stolen_access(struct drm_i915_private *i915); 381 382#endif /* !__I915_UTILS_H */ 383