1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #ifndef __I915_FILE_PRIVATE_H__
7 #define __I915_FILE_PRIVATE_H__
8 
9 #include <linux/mutex.h>
10 #include <linux/types.h>
11 #include <linux/xarray.h>
12 
13 struct drm_i915_private;
14 struct drm_file;
15 
16 struct drm_i915_file_private {
17 	struct drm_i915_private *dev_priv;
18 
19 	union {
20 		struct drm_file *file;
21 		struct rcu_head rcu;
22 	};
23 
24 	/** @proto_context_lock: Guards all struct i915_gem_proto_context
25 	 * operations
26 	 *
27 	 * This not only guards @proto_context_xa, but is always held
28 	 * whenever we manipulate any struct i915_gem_proto_context,
29 	 * including finalizing it on first actual use of the GEM context.
30 	 *
31 	 * See i915_gem_proto_context.
32 	 */
33 	struct mutex proto_context_lock;
34 
35 	/** @proto_context_xa: xarray of struct i915_gem_proto_context
36 	 *
37 	 * Historically, the context uAPI allowed for two methods of
38 	 * setting context parameters: SET_CONTEXT_PARAM and
39 	 * CONTEXT_CREATE_EXT_SETPARAM.  The former is allowed to be called
40 	 * at any time while the later happens as part of
41 	 * GEM_CONTEXT_CREATE.  Everything settable via one was settable
42 	 * via the other.  While some params are fairly simple and setting
43 	 * them on a live context is harmless such as the context priority,
44 	 * others are far trickier such as the VM or the set of engines.
45 	 * In order to swap out the VM, for instance, we have to delay
46 	 * until all current in-flight work is complete, swap in the new
47 	 * VM, and then continue.  This leads to a plethora of potential
48 	 * race conditions we'd really rather avoid.
49 	 *
50 	 * We have since disallowed setting these more complex parameters
51 	 * on active contexts.  This works by delaying the creation of the
52 	 * actual context until after the client is done configuring it
53 	 * with SET_CONTEXT_PARAM.  From the perspective of the client, it
54 	 * has the same u32 context ID the whole time.  From the
55 	 * perspective of i915, however, it's a struct i915_gem_proto_context
56 	 * right up until the point where we attempt to do something which
57 	 * the proto-context can't handle.  Then the struct i915_gem_context
58 	 * gets created.
59 	 *
60 	 * This is accomplished via a little xarray dance.  When
61 	 * GEM_CONTEXT_CREATE is called, we create a struct
62 	 * i915_gem_proto_context, reserve a slot in @context_xa but leave
63 	 * it NULL, and place the proto-context in the corresponding slot
64 	 * in @proto_context_xa.  Then, in i915_gem_context_lookup(), we
65 	 * first check @context_xa.  If it's there, we return the struct
66 	 * i915_gem_context and we're done.  If it's not, we look in
67 	 * @proto_context_xa and, if we find it there, we create the actual
68 	 * context and kill the proto-context.
69 	 *
70 	 * In order for this dance to work properly, everything which ever
71 	 * touches a struct i915_gem_proto_context is guarded by
72 	 * @proto_context_lock, including context creation.  Yes, this
73 	 * means context creation now takes a giant global lock but it
74 	 * can't really be helped and that should never be on any driver's
75 	 * fast-path anyway.
76 	 */
77 	struct xarray proto_context_xa;
78 
79 	/** @context_xa: xarray of fully created i915_gem_context
80 	 *
81 	 * Write access to this xarray is guarded by @proto_context_lock.
82 	 * Otherwise, writers may race with finalize_create_context_locked().
83 	 *
84 	 * See @proto_context_xa.
85 	 */
86 	struct xarray context_xa;
87 	struct xarray vm_xa;
88 
89 	unsigned int bsd_engine;
90 
91 /*
92  * Every context ban increments per client ban score. Also
93  * hangs in short succession increments ban score. If ban threshold
94  * is reached, client is considered banned and submitting more work
95  * will fail. This is a stop gap measure to limit the badly behaving
96  * clients access to gpu. Note that unbannable contexts never increment
97  * the client ban score.
98  */
99 #define I915_CLIENT_SCORE_HANG_FAST	1
100 #define   I915_CLIENT_FAST_HANG_JIFFIES (60 * HZ)
101 #define I915_CLIENT_SCORE_CONTEXT_BAN   3
102 #define I915_CLIENT_SCORE_BANNED	9
103 	/** ban_score: Accumulated score of all ctx bans and fast hangs. */
104 	atomic_t ban_score;
105 	unsigned long hang_timestamp;
106 };
107 
108 #endif /* __I915_FILE_PRIVATE_H__ */
109