xref: /linux/drivers/gpu/drm/i915/i915_utils.h (revision c17ee635fd3a482b2ad2bf5e269755c2eae5f25e)
1 /*
2  * Copyright © 2016 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #ifndef __I915_UTILS_H
26 #define __I915_UTILS_H
27 
28 #include <linux/overflow.h>
29 #include <linux/sched.h>
30 #include <linux/string_helpers.h>
31 #include <linux/types.h>
32 #include <linux/workqueue.h>
33 #include <linux/sched/clock.h>
34 
35 #ifdef CONFIG_X86
36 #include <asm/hypervisor.h>
37 #endif
38 
39 struct drm_i915_private;
40 
41 #define MISSING_CASE(x) WARN(1, "Missing case (%s == %ld)\n", \
42 			     __stringify(x), (long)(x))
43 
44 #define i915_probe_error(i915, fmt, ...) ({ \
45 	drm_err(&(i915)->drm, fmt, ##__VA_ARGS__); \
46 })
47 
48 #define fetch_and_zero(ptr) ({						\
49 	typeof(*ptr) __T = *(ptr);					\
50 	*(ptr) = (typeof(*ptr))0;					\
51 	__T;								\
52 })
53 
54 /*
55  * check_user_mbz: Check that a user value exists and is zero
56  *
57  * Frequently in our uABI we reserve space for future extensions, and
58  * two ensure that userspace is prepared we enforce that space must
59  * be zero. (Then any future extension can safely assume a default value
60  * of 0.)
61  *
62  * check_user_mbz() combines checking that the user pointer is accessible
63  * and that the contained value is zero.
64  *
65  * Returns: -EFAULT if not accessible, -EINVAL if !zero, or 0 on success.
66  */
67 #define check_user_mbz(U) ({						\
68 	typeof(*(U)) mbz__;						\
69 	get_user(mbz__, (U)) ? -EFAULT : mbz__ ? -EINVAL : 0;		\
70 })
71 
72 #define __mask_next_bit(mask) ({					\
73 	int __idx = ffs(mask) - 1;					\
74 	mask &= ~BIT(__idx);						\
75 	__idx;								\
76 })
77 
78 static inline bool is_power_of_2_u64(u64 n)
79 {
80 	return (n != 0 && ((n & (n - 1)) == 0));
81 }
82 
83 void add_taint_for_CI(struct drm_i915_private *i915, unsigned int taint);
84 static inline void __add_taint_for_CI(unsigned int taint)
85 {
86 	/*
87 	 * The system is "ok", just about surviving for the user, but
88 	 * CI results are now unreliable as the HW is very suspect.
89 	 * CI checks the taint state after every test and will reboot
90 	 * the machine if the kernel is tainted.
91 	 */
92 	add_taint(taint, LOCKDEP_STILL_OK);
93 }
94 
95 static inline bool i915_run_as_guest(void)
96 {
97 #if IS_ENABLED(CONFIG_X86)
98 	return !hypervisor_is_type(X86_HYPER_NATIVE);
99 #else
100 	/* Not supported yet */
101 	return false;
102 #endif
103 }
104 
105 bool i915_vtd_active(struct drm_i915_private *i915);
106 
107 bool i915_direct_stolen_access(struct drm_i915_private *i915);
108 
109 #endif /* !__I915_UTILS_H */
110