1/*	$NetBSD: i915_utils.c,v 1.4 2021/12/19 11:37:41 riastradh Exp $	*/
2
3// SPDX-License-Identifier: MIT
4/*
5 * Copyright �� 2019 Intel Corporation
6 */
7
8#include <sys/cdefs.h>
9__KERNEL_RCSID(0, "$NetBSD: i915_utils.c,v 1.4 2021/12/19 11:37:41 riastradh Exp $");
10
11#include <drm/drm_drv.h>
12
13#include "i915_drv.h"
14#include "i915_utils.h"
15
16#ifdef __NetBSD__
17#define	NBSD_BUG_URL "https://gnats.NetBSD.org/"
18#define	NBSD_BUG_MSG							      \
19	"Please file a bug at " NBSD_BUG_URL " in category kern"	      \
20	" providing the dmesg log by booting with debug/verbose"	      \
21	" as in `boot -vx'."
22#else
23#define FDO_BUG_URL "https://gitlab.freedesktop.org/drm/intel/-/wikis/How-to-file-i915-bugs"
24#define FDO_BUG_MSG "Please file a bug on drm/i915; see " FDO_BUG_URL " for details."
25#endif
26
27void
28__i915_printk(struct drm_i915_private *dev_priv, const char *level,
29	      const char *fmt, ...)
30{
31#ifdef __NetBSD__
32	static volatile unsigned done = 0;
33	va_list va;
34
35	va_start(va, fmt);
36	printf("%s: %s ", device_xname(dev_priv->drm.dev), level);
37	vprintf(fmt, va);
38	va_end(va);
39
40	if (strncmp(level, KERN_ERR, strlen(KERN_ERR)) == 0 &&
41	    atomic_swap_uint(&done, 1) == 0)
42		printf("%s\n", NBSD_BUG_MSG);
43#else
44	static bool shown_bug_once;
45	struct device *kdev = dev_priv->drm.dev;
46	bool is_error = level[1] <= KERN_ERR[1];
47	bool is_debug = level[1] == KERN_DEBUG[1];
48	struct va_format vaf;
49	va_list args;
50
51	if (is_debug && !drm_debug_enabled(DRM_UT_DRIVER))
52		return;
53
54	va_start(args, fmt);
55
56	vaf.fmt = fmt;
57	vaf.va = &args;
58
59	if (is_error)
60		dev_printk(level, kdev, "%pV", &vaf);
61	else
62		dev_printk(level, kdev, "[" DRM_NAME ":%ps] %pV",
63			   __builtin_return_address(0), &vaf);
64
65	va_end(args);
66
67	if (is_error && !shown_bug_once) {
68		/*
69		 * Ask the user to file a bug report for the error, except
70		 * if they may have caused the bug by fiddling with unsafe
71		 * module parameters.
72		 */
73		if (!test_taint(TAINT_USER))
74			dev_notice(kdev, "%s", FDO_BUG_MSG);
75		shown_bug_once = true;
76	}
77#endif
78}
79
80#if IS_ENABLED(CONFIG_DRM_I915_DEBUG)
81static unsigned int i915_probe_fail_count;
82
83int __i915_inject_probe_error(struct drm_i915_private *i915, int err,
84			      const char *func, int line)
85{
86	if (i915_probe_fail_count >= i915_modparams.inject_probe_failure)
87		return 0;
88
89	if (++i915_probe_fail_count < i915_modparams.inject_probe_failure)
90		return 0;
91
92	__i915_printk(i915, KERN_INFO,
93		      "Injecting failure %d at checkpoint %u [%s:%d]\n",
94		      err, i915_modparams.inject_probe_failure, func, line);
95	i915_modparams.inject_probe_failure = 0;
96	return err;
97}
98
99bool i915_error_injected(void)
100{
101	return i915_probe_fail_count && !i915_modparams.inject_probe_failure;
102}
103
104#endif
105
106void cancel_timer(struct timer_list *t)
107{
108#ifndef __NetBSD__
109	if (!READ_ONCE(t->expires))
110		return;
111#endif
112
113	del_timer(t);
114#ifndef __NetBSD__
115	WRITE_ONCE(t->expires, 0);
116#endif
117}
118
119void set_timer_ms(struct timer_list *t, unsigned long timeout)
120{
121	if (!timeout) {
122		cancel_timer(t);
123		return;
124	}
125
126	timeout = msecs_to_jiffies_timeout(timeout);
127
128	/*
129	 * Paranoia to make sure the compiler computes the timeout before
130	 * loading 'jiffies' as jiffies is volatile and may be updated in
131	 * the background by a timer tick. All to reduce the complexity
132	 * of the addition and reduce the risk of losing a jiffie.
133	 */
134	barrier();
135
136	mod_timer(t, jiffies + timeout);
137}
138