1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2019 Intel Corporation 4 */ 5 6 #include <linux/device.h> 7 8 #include <drm/drm_drv.h> 9 10 #include "i915_drv.h" 11 #include "i915_reg.h" 12 #include "i915_utils.h" 13 14 #include <sys/syslog.h> 15 16 #define FDO_BUG_MSG "Please file a bug on drm/i915; see " FDO_BUG_URL " for details." 17 18 void 19 __i915_printk(struct drm_i915_private *dev_priv, const char *level, 20 const char *fmt, ...) 21 { 22 static bool shown_bug_once; 23 struct device *kdev = dev_priv->drm.dev; 24 bool is_error = level[1] <= KERN_ERR[1]; 25 bool is_debug = level[1] == KERN_DEBUG[1]; 26 struct va_format vaf; 27 va_list args; 28 29 if (is_debug && !drm_debug_enabled(DRM_UT_DRIVER)) 30 return; 31 32 va_start(args, fmt); 33 34 vaf.fmt = fmt; 35 vaf.va = &args; 36 37 #ifdef __linux__ 38 if (is_error) 39 dev_printk(level, kdev, "%pV", &vaf); 40 else 41 dev_printk(level, kdev, "[" DRM_NAME ":%ps] %pV", 42 __builtin_return_address(0), &vaf); 43 #else 44 if (!is_error) 45 printf("[" DRM_NAME "] "); 46 vprintf(fmt, args); 47 #endif 48 49 va_end(args); 50 51 if (is_error && !shown_bug_once) { 52 /* 53 * Ask the user to file a bug report for the error, except 54 * if they may have caused the bug by fiddling with unsafe 55 * module parameters. 56 */ 57 #ifdef __linux__ 58 if (!test_taint(TAINT_USER)) 59 dev_notice(kdev, "%s", FDO_BUG_MSG); 60 #endif 61 shown_bug_once = true; 62 } 63 } 64 65 void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint) 66 { 67 __i915_printk(i915, KERN_NOTICE, "CI tainted:%#x by %pS\n", 68 taint, (void *)_RET_IP_); 69 70 /* Failures that occur during fault injection testing are expected */ 71 if (!i915_error_injected()) 72 __add_taint_for_CI(taint); 73 } 74 75 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG) 76 static unsigned int i915_probe_fail_count; 77 78 int __i915_inject_probe_error(struct drm_i915_private *i915, int err, 79 const char *func, int line) 80 { 81 if (i915_probe_fail_count >= i915_modparams.inject_probe_failure) 82 return 0; 83 84 if (++i915_probe_fail_count < i915_modparams.inject_probe_failure) 85 return 0; 86 87 __i915_printk(i915, KERN_INFO, 88 "Injecting failure %d at checkpoint %u [%s:%d]\n", 89 err, i915_modparams.inject_probe_failure, func, line); 90 i915_modparams.inject_probe_failure = 0; 91 return err; 92 } 93 94 bool i915_error_injected(void) 95 { 96 return i915_probe_fail_count && !i915_modparams.inject_probe_failure; 97 } 98 99 #endif 100 101 void cancel_timer(struct timeout *t) 102 { 103 if (!timer_active(t)) 104 return; 105 106 del_timer(t); 107 WRITE_ONCE(t->to_time, 0); 108 } 109 110 void set_timer_ms(struct timeout *t, unsigned long timeout) 111 { 112 if (!timeout) { 113 cancel_timer(t); 114 return; 115 } 116 117 timeout = msecs_to_jiffies(timeout); 118 119 /* 120 * Paranoia to make sure the compiler computes the timeout before 121 * loading 'jiffies' as jiffies is volatile and may be updated in 122 * the background by a timer tick. All to reduce the complexity 123 * of the addition and reduce the risk of losing a jiffie. 124 */ 125 barrier(); 126 127 /* Keep t->expires = 0 reserved to indicate a canceled timer. */ 128 mod_timer(t, jiffies + timeout ?: 1); 129 } 130 131 bool i915_vtd_active(struct drm_i915_private *i915) 132 { 133 return false; 134 #ifdef notyet 135 if (device_iommu_mapped(i915->drm.dev)) 136 return true; 137 138 /* Running as a guest, we assume the host is enforcing VT'd */ 139 return i915_run_as_guest(); 140 #endif 141 } 142 143 bool i915_direct_stolen_access(struct drm_i915_private *i915) 144 { 145 /* 146 * Wa_22018444074 147 * 148 * Access via BAR can hang MTL, go directly to GSM/DSM, 149 * except for VM guests which won't have access to it. 150 * 151 * Normally this would not work but on MTL the system firmware 152 * should have relaxed the access permissions sufficiently. 153 * 0x138914==0x1 indicates that the firmware has done its job. 154 */ 155 return IS_METEORLAKE(i915) && !i915_run_as_guest() && 156 intel_uncore_read(&i915->uncore, MTL_PCODE_STOLEN_ACCESS) == STOLEN_ACCESS_ALLOWED; 157 } 158