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 
add_taint_for_CI(struct drm_i915_private * i915,unsigned int taint)16 void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint)
17 {
18 	drm_notice(&i915->drm, "CI tainted: %#x by %pS\n",
19 		   taint, __builtin_return_address(0));
20 
21 	/* Failures that occur during fault injection testing are expected */
22 	if (!i915_error_injected())
23 		__add_taint_for_CI(taint);
24 }
25 
26 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
27 static unsigned int i915_probe_fail_count;
28 
__i915_inject_probe_error(struct drm_i915_private * i915,int err,const char * func,int line)29 int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
30 			      const char *func, int line)
31 {
32 	if (i915_probe_fail_count >= i915_modparams.inject_probe_failure)
33 		return 0;
34 
35 	if (++i915_probe_fail_count < i915_modparams.inject_probe_failure)
36 		return 0;
37 
38 	drm_info(&i915->drm, "Injecting failure %d at checkpoint %u [%s:%d]\n",
39 		 err, i915_modparams.inject_probe_failure, func, line);
40 
41 	i915_modparams.inject_probe_failure = 0;
42 	return err;
43 }
44 
i915_error_injected(void)45 bool i915_error_injected(void)
46 {
47 	return i915_probe_fail_count && !i915_modparams.inject_probe_failure;
48 }
49 
50 #endif
51 
cancel_timer(struct timeout * t)52 void cancel_timer(struct timeout *t)
53 {
54 	if (!timer_active(t))
55 		return;
56 
57 	del_timer(t);
58 	WRITE_ONCE(t->to_time, 0);
59 }
60 
set_timer_ms(struct timeout * t,unsigned long timeout)61 void set_timer_ms(struct timeout *t, unsigned long timeout)
62 {
63 	if (!timeout) {
64 		cancel_timer(t);
65 		return;
66 	}
67 
68 	timeout = msecs_to_jiffies(timeout);
69 
70 	/*
71 	 * Paranoia to make sure the compiler computes the timeout before
72 	 * loading 'jiffies' as jiffies is volatile and may be updated in
73 	 * the background by a timer tick. All to reduce the complexity
74 	 * of the addition and reduce the risk of losing a jiffy.
75 	 */
76 	barrier();
77 
78 	/* Keep t->expires = 0 reserved to indicate a canceled timer. */
79 	mod_timer(t, jiffies + timeout ?: 1);
80 }
81 
i915_vtd_active(struct drm_i915_private * i915)82 bool i915_vtd_active(struct drm_i915_private *i915)
83 {
84 	return false;
85 #ifdef notyet
86 	if (device_iommu_mapped(i915->drm.dev))
87 		return true;
88 
89 	/* Running as a guest, we assume the host is enforcing VT'd */
90 	return i915_run_as_guest();
91 #endif
92 }
93 
i915_direct_stolen_access(struct drm_i915_private * i915)94 bool i915_direct_stolen_access(struct drm_i915_private *i915)
95 {
96 	/*
97 	 * Wa_22018444074
98 	 *
99 	 * Access via BAR can hang MTL, go directly to GSM/DSM,
100 	 * except for VM guests which won't have access to it.
101 	 *
102 	 * Normally this would not work but on MTL the system firmware
103 	 * should have relaxed the access permissions sufficiently.
104 	 * 0x138914==0x1 indicates that the firmware has done its job.
105 	 */
106 	return IS_METEORLAKE(i915) && !i915_run_as_guest() &&
107 		intel_uncore_read(&i915->uncore, MTL_PCODE_STOLEN_ACCESS) == STOLEN_ACCESS_ALLOWED;
108 }
109