xref: /openbsd/sys/dev/pci/drm/i915/i915_utils.c (revision 09467b48)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include <drm/drm_drv.h>
7 
8 #include "i915_drv.h"
9 #include "i915_utils.h"
10 
11 #include <sys/syslog.h>
12 
13 #define FDO_BUG_MSG "Please file a bug on drm/i915; see " FDO_BUG_URL " for details."
14 
15 void
16 __i915_printk(struct drm_i915_private *dev_priv, const char *level,
17 	      const char *fmt, ...)
18 {
19 	static bool shown_bug_once;
20 	struct device *kdev = dev_priv->drm.dev;
21 	bool is_error = level[1] <= KERN_ERR[1];
22 	bool is_debug = level[1] == KERN_DEBUG[1];
23 	struct va_format vaf;
24 	va_list args;
25 
26 	if (is_debug && !drm_debug_enabled(DRM_UT_DRIVER))
27 		return;
28 
29 	va_start(args, fmt);
30 
31 	vaf.fmt = fmt;
32 	vaf.va = &args;
33 
34 #ifdef __linux__
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 #else
41 	if (!is_error)
42 		printf("[" DRM_NAME "] ");
43 	vprintf(fmt, args);
44 #endif
45 
46 	va_end(args);
47 
48 	if (is_error && !shown_bug_once) {
49 		/*
50 		 * Ask the user to file a bug report for the error, except
51 		 * if they may have caused the bug by fiddling with unsafe
52 		 * module parameters.
53 		 */
54 #ifdef __linux__
55 		if (!test_taint(TAINT_USER))
56 			dev_notice(kdev, "%s", FDO_BUG_MSG);
57 #endif
58 		shown_bug_once = true;
59 	}
60 }
61 
62 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
63 static unsigned int i915_probe_fail_count;
64 
65 int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
66 			      const char *func, int line)
67 {
68 	if (i915_probe_fail_count >= i915_modparams.inject_probe_failure)
69 		return 0;
70 
71 	if (++i915_probe_fail_count < i915_modparams.inject_probe_failure)
72 		return 0;
73 
74 	__i915_printk(i915, KERN_INFO,
75 		      "Injecting failure %d at checkpoint %u [%s:%d]\n",
76 		      err, i915_modparams.inject_probe_failure, func, line);
77 	i915_modparams.inject_probe_failure = 0;
78 	return err;
79 }
80 
81 bool i915_error_injected(void)
82 {
83 	return i915_probe_fail_count && !i915_modparams.inject_probe_failure;
84 }
85 
86 #endif
87 
88 void cancel_timer(struct timeout *t)
89 {
90 	if (!READ_ONCE(t->to_time))
91 		return;
92 
93 	del_timer(t);
94 	WRITE_ONCE(t->to_time, 0);
95 }
96 
97 void set_timer_ms(struct timeout *t, unsigned long timeout)
98 {
99 	if (!timeout) {
100 		cancel_timer(t);
101 		return;
102 	}
103 
104 	timeout = msecs_to_jiffies_timeout(timeout);
105 
106 	/*
107 	 * Paranoia to make sure the compiler computes the timeout before
108 	 * loading 'jiffies' as jiffies is volatile and may be updated in
109 	 * the background by a timer tick. All to reduce the complexity
110 	 * of the addition and reduce the risk of losing a jiffie.
111 	 */
112 	barrier();
113 
114 	mod_timer(t, jiffies + timeout);
115 }
116