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