Merge drm/drm-next into drm-intel-gt-next
[linux-2.6-block.git] / drivers / gpu / drm / i915 / i915_utils.c
CommitLineData
358c855c
JN
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
ddae4d7a 11#define FDO_BUG_MSG "Please file a bug on drm/i915; see " FDO_BUG_URL " for details."
358c855c
JN
12
13void
14__i915_printk(struct drm_i915_private *dev_priv, const char *level,
15 const char *fmt, ...)
16{
17 static bool shown_bug_once;
18 struct device *kdev = dev_priv->drm.dev;
19 bool is_error = level[1] <= KERN_ERR[1];
20 bool is_debug = level[1] == KERN_DEBUG[1];
21 struct va_format vaf;
22 va_list args;
23
bdbf43d7 24 if (is_debug && !drm_debug_enabled(DRM_UT_DRIVER))
358c855c
JN
25 return;
26
27 va_start(args, fmt);
28
29 vaf.fmt = fmt;
30 vaf.va = &args;
31
32 if (is_error)
33 dev_printk(level, kdev, "%pV", &vaf);
34 else
35 dev_printk(level, kdev, "[" DRM_NAME ":%ps] %pV",
36 __builtin_return_address(0), &vaf);
37
38 va_end(args);
39
40 if (is_error && !shown_bug_once) {
41 /*
42 * Ask the user to file a bug report for the error, except
43 * if they may have caused the bug by fiddling with unsafe
44 * module parameters.
45 */
46 if (!test_taint(TAINT_USER))
47 dev_notice(kdev, "%s", FDO_BUG_MSG);
48 shown_bug_once = true;
49 }
50}
51
65706203
MW
52void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint)
53{
54 __i915_printk(i915, KERN_NOTICE, "CI tainted:%#x by %pS\n",
55 taint, (void *)_RET_IP_);
fcab594a
MW
56
57 /* Failures that occur during fault injection testing are expected */
58 if (!i915_error_injected())
59 __add_taint_for_CI(taint);
65706203
MW
60}
61
358c855c
JN
62#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
63static unsigned int i915_probe_fail_count;
64
dd6e38df
JK
65int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
66 const char *func, int line)
358c855c 67{
4ec37538 68 if (i915_probe_fail_count >= i915_modparams.inject_probe_failure)
358c855c
JN
69 return 0;
70
4ec37538 71 if (++i915_probe_fail_count < i915_modparams.inject_probe_failure)
358c855c
JN
72 return 0;
73
74 __i915_printk(i915, KERN_INFO,
75 "Injecting failure %d at checkpoint %u [%s:%d]\n",
4ec37538
JK
76 err, i915_modparams.inject_probe_failure, func, line);
77 i915_modparams.inject_probe_failure = 0;
358c855c
JN
78 return err;
79}
80
81bool i915_error_injected(void)
82{
4ec37538 83 return i915_probe_fail_count && !i915_modparams.inject_probe_failure;
358c855c
JN
84}
85
86#endif
3a7a92ab
CW
87
88void cancel_timer(struct timer_list *t)
89{
c185a16e 90 if (!timer_active(t))
3a7a92ab
CW
91 return;
92
93 del_timer(t);
94 WRITE_ONCE(t->expires, 0);
95}
96
97void set_timer_ms(struct timer_list *t, unsigned long timeout)
98{
99 if (!timeout) {
100 cancel_timer(t);
101 return;
102 }
103
f4bb45f7 104 timeout = msecs_to_jiffies(timeout);
3a7a92ab
CW
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
bfae03fe
CW
114 /* Keep t->expires = 0 reserved to indicate a canceled timer. */
115 mod_timer(t, jiffies + timeout ?: 1);
3a7a92ab 116}