1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2019 Intel Corporation
5  */
6 
7 #ifndef __I915_GEM_CONTEXT_TYPES_H__
8 #define __I915_GEM_CONTEXT_TYPES_H__
9 
10 #include <linux/atomic.h>
11 #include <linux/list.h>
12 #include <linux/llist.h>
13 #include <linux/kref.h>
14 #include <linux/mutex.h>
15 #include <linux/radix-tree.h>
16 #include <linux/rbtree.h>
17 #include <linux/rcupdate.h>
18 #include <linux/types.h>
19 
20 #include "gt/intel_context_types.h"
21 
22 #include "i915_scheduler.h"
23 #include "i915_sw_fence.h"
24 
25 struct pid;
26 
27 struct drm_i915_private;
28 struct drm_i915_file_private;
29 struct i915_address_space;
30 struct intel_timeline;
31 struct intel_ring;
32 
33 /**
34  * struct i915_gem_engines - A set of engines
35  */
36 struct i915_gem_engines {
37 	union {
38 		/** @link: Link in i915_gem_context::stale::engines */
39 		struct list_head link;
40 
41 		/** @rcu: RCU to use when freeing */
42 		struct rcu_head rcu;
43 	};
44 
45 	/** @fence: Fence used for delayed destruction of engines */
46 	struct i915_sw_fence fence;
47 
48 	/** @ctx: i915_gem_context backpointer */
49 	struct i915_gem_context *ctx;
50 
51 	/** @num_engines: Number of engines in this set */
52 	unsigned int num_engines;
53 
54 	/** @engines: Array of engines */
55 	struct intel_context *engines[];
56 };
57 
58 /**
59  * struct i915_gem_engines_iter - Iterator for an i915_gem_engines set
60  */
61 struct i915_gem_engines_iter {
62 	/** @idx: Index into i915_gem_engines::engines */
63 	unsigned int idx;
64 
65 	/** @engines: Engine set being iterated */
66 	const struct i915_gem_engines *engines;
67 };
68 
69 /**
70  * struct i915_gem_proto_context - prototype context
71  *
72  * The struct i915_gem_proto_context represents the creation parameters for
73  * a struct i915_gem_context.  This is used to gather parameters provided
74  * either through creation flags or via SET_CONTEXT_PARAM so that, when we
75  * create the final i915_gem_context, those parameters can be immutable.
76  */
77 struct i915_gem_proto_context {
78 	/** @vm: See &i915_gem_context.vm */
79 	struct i915_address_space *vm;
80 
81 	/** @user_flags: See &i915_gem_context.user_flags */
82 	unsigned long user_flags;
83 
84 	/** @sched: See &i915_gem_context.sched */
85 	struct i915_sched_attr sched;
86 
87 	/** @single_timeline: See See &i915_gem_context.syncobj */
88 	bool single_timeline;
89 };
90 
91 /**
92  * struct i915_gem_context - client state
93  *
94  * The struct i915_gem_context represents the combined view of the driver and
95  * logical hardware state for a particular client.
96  */
97 struct i915_gem_context {
98 	/** @i915: i915 device backpointer */
99 	struct drm_i915_private *i915;
100 
101 	/** @file_priv: owning file descriptor */
102 	struct drm_i915_file_private *file_priv;
103 
104 	/**
105 	 * @engines: User defined engines for this context
106 	 *
107 	 * Various uAPI offer the ability to lookup up an
108 	 * index from this array to select an engine operate on.
109 	 *
110 	 * Multiple logically distinct instances of the same engine
111 	 * may be defined in the array, as well as composite virtual
112 	 * engines.
113 	 *
114 	 * Execbuf uses the I915_EXEC_RING_MASK as an index into this
115 	 * array to select which HW context + engine to execute on. For
116 	 * the default array, the user_ring_map[] is used to translate
117 	 * the legacy uABI onto the approprate index (e.g. both
118 	 * I915_EXEC_DEFAULT and I915_EXEC_RENDER select the same
119 	 * context, and I915_EXEC_BSD is weird). For a use defined
120 	 * array, execbuf uses I915_EXEC_RING_MASK as a plain index.
121 	 *
122 	 * User defined by I915_CONTEXT_PARAM_ENGINE (when the
123 	 * CONTEXT_USER_ENGINES flag is set).
124 	 */
125 	struct i915_gem_engines __rcu *engines;
126 
127 	/** @engines_mutex: guards writes to engines */
128 	struct mutex engines_mutex;
129 
130 	/**
131 	 * @syncobj: Shared timeline syncobj
132 	 *
133 	 * When the SHARED_TIMELINE flag is set on context creation, we
134 	 * emulate a single timeline across all engines using this syncobj.
135 	 * For every execbuffer2 call, this syncobj is used as both an in-
136 	 * and out-fence.  Unlike the real intel_timeline, this doesn't
137 	 * provide perfect atomic in-order guarantees if the client races
138 	 * with itself by calling execbuffer2 twice concurrently.  However,
139 	 * if userspace races with itself, that's not likely to yield well-
140 	 * defined results anyway so we choose to not care.
141 	 */
142 	struct drm_syncobj *syncobj;
143 
144 	/**
145 	 * @vm: unique address space (GTT)
146 	 *
147 	 * In full-ppgtt mode, each context has its own address space ensuring
148 	 * complete seperation of one client from all others.
149 	 *
150 	 * In other modes, this is a NULL pointer with the expectation that
151 	 * the caller uses the shared global GTT.
152 	 */
153 	struct i915_address_space __rcu *vm;
154 
155 	/**
156 	 * @pid: process id of creator
157 	 *
158 	 * Note that who created the context may not be the principle user,
159 	 * as the context may be shared across a local socket. However,
160 	 * that should only affect the default context, all contexts created
161 	 * explicitly by the client are expected to be isolated.
162 	 */
163 	struct pid *pid;
164 
165 	/** @link: place with &drm_i915_private.context_list */
166 	struct list_head link;
167 
168 	/**
169 	 * @ref: reference count
170 	 *
171 	 * A reference to a context is held by both the client who created it
172 	 * and on each request submitted to the hardware using the request
173 	 * (to ensure the hardware has access to the state until it has
174 	 * finished all pending writes). See i915_gem_context_get() and
175 	 * i915_gem_context_put() for access.
176 	 */
177 	struct kref ref;
178 
179 	/**
180 	 * @rcu: rcu_head for deferred freeing.
181 	 */
182 	struct rcu_head rcu;
183 
184 	/**
185 	 * @user_flags: small set of booleans controlled by the user
186 	 */
187 	unsigned long user_flags;
188 #define UCONTEXT_NO_ERROR_CAPTURE	1
189 #define UCONTEXT_BANNABLE		2
190 #define UCONTEXT_RECOVERABLE		3
191 #define UCONTEXT_PERSISTENCE		4
192 
193 	/**
194 	 * @flags: small set of booleans
195 	 */
196 	unsigned long flags;
197 #define CONTEXT_CLOSED			0
198 #define CONTEXT_USER_ENGINES		1
199 
200 	/** @mutex: guards everything that isn't engines or handles_vma */
201 	struct mutex mutex;
202 
203 	/** @sched: scheduler parameters */
204 	struct i915_sched_attr sched;
205 
206 	/** @guilty_count: How many times this context has caused a GPU hang. */
207 	atomic_t guilty_count;
208 	/**
209 	 * @active_count: How many times this context was active during a GPU
210 	 * hang, but did not cause it.
211 	 */
212 	atomic_t active_count;
213 
214 	/**
215 	 * @hang_timestamp: The last time(s) this context caused a GPU hang
216 	 */
217 	unsigned long hang_timestamp[2];
218 #define CONTEXT_FAST_HANG_JIFFIES (120 * HZ) /* 3 hangs within 120s? Banned! */
219 
220 	/** @remap_slice: Bitmask of cache lines that need remapping */
221 	u8 remap_slice;
222 
223 	/**
224 	 * @handles_vma: rbtree to look up our context specific obj/vma for
225 	 * the user handle. (user handles are per fd, but the binding is
226 	 * per vm, which may be one per context or shared with the global GTT)
227 	 */
228 	struct radix_tree_root handles_vma;
229 
230 	/** @lut_mutex: Locks handles_vma */
231 	struct mutex lut_mutex;
232 
233 	/**
234 	 * @name: arbitrary name, used for user debug
235 	 *
236 	 * A name is constructed for the context from the creator's process
237 	 * name, pid and user handle in order to uniquely identify the
238 	 * context in messages.
239 	 */
240 	char name[TASK_COMM_LEN + 8];
241 
242 	/** @stale: tracks stale engines to be destroyed */
243 	struct {
244 		/** @lock: guards engines */
245 		spinlock_t lock;
246 		/** @engines: list of stale engines */
247 		struct list_head engines;
248 	} stale;
249 };
250 
251 #endif /* __I915_GEM_CONTEXT_TYPES_H__ */
252