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/overflow.h> 29 #include <linux/sched.h> 30 #include <linux/string_helpers.h> 31 #include <linux/types.h> 32 #include <linux/workqueue.h> 33 #include <linux/sched/clock.h> 34 35 #ifdef CONFIG_X86 36 #include <asm/hypervisor.h> 37 #endif 38 39 struct drm_i915_private; 40 41 #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \ 42 __stringify(x), (long)(x)) 43 44 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG) 45 46 int __i915_inject_probe_error(struct drm_i915_private *i915, int err, 47 const char *func, int line); 48 #define i915_inject_probe_error(_i915, _err) \ 49 __i915_inject_probe_error((_i915), (_err), __func__, __LINE__) 50 bool i915_error_injected(void); 51 52 #else 53 54 #define i915_inject_probe_error(i915, e) ({ BUILD_BUG_ON_INVALID(i915); 0; }) 55 #define i915_error_injected() false 56 57 #endif 58 59 #define i915_inject_probe_failure(i915) i915_inject_probe_error((i915), -ENODEV) 60 61 #define i915_probe_error(i915, fmt, ...) ({ \ 62 if (i915_error_injected()) \ 63 drm_dbg(&(i915)->drm, fmt, ##__VA_ARGS__); \ 64 else \ 65 drm_err(&(i915)->drm, fmt, ##__VA_ARGS__); \ 66 }) 67 68 #define fetch_and_zero(ptr) ({ \ 69 typeof(*ptr) __T = *(ptr); \ 70 *(ptr) = (typeof(*ptr))0; \ 71 __T; \ 72 }) 73 74 /* 75 * check_user_mbz: Check that a user value exists and is zero 76 * 77 * Frequently in our uABI we reserve space for future extensions, and 78 * two ensure that userspace is prepared we enforce that space must 79 * be zero. (Then any future extension can safely assume a default value 80 * of 0.) 81 * 82 * check_user_mbz() combines checking that the user pointer is accessible 83 * and that the contained value is zero. 84 * 85 * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success. 86 */ 87 #define check_user_mbz(U) ({ \ 88 typeof(*(U)) mbz__; \ 89 get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \ 90 }) 91 92 #define __mask_next_bit(mask) ({ \ 93 int __idx = ffs(mask) - 1; \ 94 mask &= ~BIT(__idx); \ 95 __idx; \ 96 }) 97 98 static inline bool is_power_of_2_u64(u64 n) 99 { 100 return (n != 0 && ((n & (n - 1)) == 0)); 101 } 102 103 static inline unsigned long msecs_to_jiffies_timeout(const unsigned int m) 104 { 105 unsigned long j = msecs_to_jiffies(m); 106 107 return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1); 108 } 109 110 /* 111 * If you need to wait X milliseconds between events A and B, but event B 112 * doesn't happen exactly after event A, you record the timestamp (jiffies) of 113 * when event A happened, then just before event B you call this function and 114 * pass the timestamp as the first argument, and X as the second argument. 115 */ 116 static inline void 117 wait_remaining_ms_from_jiffies(unsigned long timestamp_jiffies, int to_wait_ms) 118 { 119 unsigned long target_jiffies, tmp_jiffies, remaining_jiffies; 120 121 /* 122 * Don't re-read the value of "jiffies" every time since it may change 123 * behind our back and break the math. 124 */ 125 tmp_jiffies = jiffies; 126 target_jiffies = timestamp_jiffies + 127 msecs_to_jiffies_timeout(to_wait_ms); 128 129 if (time_after(target_jiffies, tmp_jiffies)) { 130 remaining_jiffies = target_jiffies - tmp_jiffies; 131 while (remaining_jiffies) 132 remaining_jiffies = 133 schedule_timeout_uninterruptible(remaining_jiffies); 134 } 135 } 136 137 #define KHz(x) (1000 * (x)) 138 #define MHz(x) KHz(1000 * (x)) 139 140 void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint); 141 static inline void __add_taint_for_CI(unsigned int taint) 142 { 143 /* 144 * The system is "ok", just about surviving for the user, but 145 * CI results are now unreliable as the HW is very suspect. 146 * CI checks the taint state after every test and will reboot 147 * the machine if the kernel is tainted. 148 */ 149 add_taint(taint, LOCKDEP_STILL_OK); 150 } 151 152 static inline bool i915_run_as_guest(void) 153 { 154 #if IS_ENABLED(CONFIG_X86) 155 return !hypervisor_is_type(X86_HYPER_NATIVE); 156 #else 157 /* Not supported yet */ 158 return false; 159 #endif 160 } 161 162 bool i915_vtd_active(struct drm_i915_private *i915); 163 164 bool i915_direct_stolen_access(struct drm_i915_private *i915); 165 166 #endif /* !__I915_UTILS_H */ 167