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