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 #ifndef MISSING_CASE 42 #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \ 43 __stringify(x), (long)(x)) 44 #endif 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 #ifndef fetch_and_zero 71 #define fetch_and_zero(ptr) ({ \ 72 typeof(*ptr) __T = *(ptr); \ 73 *(ptr) = (typeof(*ptr))0; \ 74 __T; \ 75 }) 76 #endif 77 78 /* 79 * check_user_mbz: Check that a user value exists and is zero 80 * 81 * Frequently in our uABI we reserve space for future extensions, and 82 * two ensure that userspace is prepared we enforce that space must 83 * be zero. (Then any future extension can safely assume a default value 84 * of 0.) 85 * 86 * check_user_mbz() combines checking that the user pointer is accessible 87 * and that the contained value is zero. 88 * 89 * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success. 90 */ 91 #define check_user_mbz(U) ({ \ 92 typeof(*(U)) mbz__; \ 93 get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0; \ 94 }) 95 96 #define __mask_next_bit(mask) ({ \ 97 int __idx = ffs(mask) - 1; \ 98 mask &= ~BIT(__idx); \ 99 __idx; \ 100 }) 101 102 static inline bool is_power_of_2_u64(u64 n) 103 { 104 return (n != 0 && ((n & (n - 1)) == 0)); 105 } 106 107 void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint); 108 static inline void __add_taint_for_CI(unsigned int taint) 109 { 110 /* 111 * The system is "ok", just about surviving for the user, but 112 * CI results are now unreliable as the HW is very suspect. 113 * CI checks the taint state after every test and will reboot 114 * the machine if the kernel is tainted. 115 */ 116 add_taint(taint, LOCKDEP_STILL_OK); 117 } 118 119 static inline bool i915_run_as_guest(void) 120 { 121 #if IS_ENABLED(CONFIG_X86) 122 return !hypervisor_is_type(X86_HYPER_NATIVE); 123 #else 124 /* Not supported yet */ 125 return false; 126 #endif 127 } 128 129 bool i915_vtd_active(struct drm_i915_private *i915); 130 131 bool i915_direct_stolen_access(struct drm_i915_private *i915); 132 133 #endif /* !__I915_UTILS_H */ 134