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