xref: /dragonfly/sys/dev/drm/i915/i915_gem_context.h (revision 2b57e6df)
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_GEM_CONTEXT_H__
26 #define __I915_GEM_CONTEXT_H__
27 
28 #include <linux/bitops.h>
29 #include <linux/list.h>
30 
31 struct pid;
32 
33 struct drm_device;
34 struct drm_file;
35 
36 struct drm_i915_private;
37 struct drm_i915_file_private;
38 struct i915_hw_ppgtt;
39 struct i915_vma;
40 struct intel_ring;
41 
42 #define DEFAULT_CONTEXT_HANDLE 0
43 
44 /**
45  * struct i915_gem_context - client state
46  *
47  * The struct i915_gem_context represents the combined view of the driver and
48  * logical hardware state for a particular client.
49  */
50 struct i915_gem_context {
51 	/** i915: i915 device backpointer */
52 	struct drm_i915_private *i915;
53 
54 	/** file_priv: owning file descriptor */
55 	struct drm_i915_file_private *file_priv;
56 
57 	/**
58 	 * @ppgtt: unique address space (GTT)
59 	 *
60 	 * In full-ppgtt mode, each context has its own address space ensuring
61 	 * complete seperation of one client from all others.
62 	 *
63 	 * In other modes, this is a NULL pointer with the expectation that
64 	 * the caller uses the shared global GTT.
65 	 */
66 	struct i915_hw_ppgtt *ppgtt;
67 
68 	/**
69 	 * @pid: process id of creator
70 	 *
71 	 * Note that who created the context may not be the principle user,
72 	 * as the context may be shared across a local socket. However,
73 	 * that should only affect the default context, all contexts created
74 	 * explicitly by the client are expected to be isolated.
75 	 */
76 	pid_t pid;
77 
78 	/**
79 	 * @name: arbitrary name
80 	 *
81 	 * A name is constructed for the context from the creator's process
82 	 * name, pid and user handle in order to uniquely identify the
83 	 * context in messages.
84 	 */
85 	const char *name;
86 
87 	/** link: place with &drm_i915_private.context_list */
88 	struct list_head link;
89 
90 	/**
91 	 * @ref: reference count
92 	 *
93 	 * A reference to a context is held by both the client who created it
94 	 * and on each request submitted to the hardware using the request
95 	 * (to ensure the hardware has access to the state until it has
96 	 * finished all pending writes). See i915_gem_context_get() and
97 	 * i915_gem_context_put() for access.
98 	 */
99 	struct kref ref;
100 
101 	/**
102 	 * @flags: small set of booleans
103 	 */
104 	unsigned long flags;
105 #define CONTEXT_NO_ZEROMAP		BIT(0)
106 #define CONTEXT_NO_ERROR_CAPTURE	1
107 #define CONTEXT_CLOSED			2
108 #define CONTEXT_BANNABLE		3
109 #define CONTEXT_BANNED			4
110 #define CONTEXT_FORCE_SINGLE_SUBMISSION	5
111 
112 	/**
113 	 * @hw_id: - unique identifier for the context
114 	 *
115 	 * The hardware needs to uniquely identify the context for a few
116 	 * functions like fault reporting, PASID, scheduling. The
117 	 * &drm_i915_private.context_hw_ida is used to assign a unqiue
118 	 * id for the lifetime of the context.
119 	 */
120 	unsigned int hw_id;
121 
122 	/**
123 	 * @user_handle: userspace identifier
124 	 *
125 	 * A unique per-file identifier is generated from
126 	 * &drm_i915_file_private.contexts.
127 	 */
128 	u32 user_handle;
129 
130 	/**
131 	 * @priority: execution and service priority
132 	 *
133 	 * All clients are equal, but some are more equal than others!
134 	 *
135 	 * Requests from a context with a greater (more positive) value of
136 	 * @priority will be executed before those with a lower @priority
137 	 * value, forming a simple QoS.
138 	 *
139 	 * The &drm_i915_private.kernel_context is assigned the lowest priority.
140 	 */
141 	int priority;
142 
143 	/** ggtt_offset_bias: placement restriction for context objects */
144 	u32 ggtt_offset_bias;
145 
146 	/** engine: per-engine logical HW state */
147 	struct intel_context {
148 		struct i915_vma *state;
149 		struct intel_ring *ring;
150 		u32 *lrc_reg_state;
151 		u64 lrc_desc;
152 		int pin_count;
153 		bool initialised;
154 	} engine[I915_NUM_ENGINES];
155 
156 	/** ring_size: size for allocating the per-engine ring buffer */
157 	u32 ring_size;
158 	/** desc_template: invariant fields for the HW context descriptor */
159 	u32 desc_template;
160 
161 	/** guilty_count: How many times this context has caused a GPU hang. */
162 	unsigned int guilty_count;
163 	/**
164 	 * @active_count: How many times this context was active during a GPU
165 	 * hang, but did not cause it.
166 	 */
167 	unsigned int active_count;
168 
169 #define CONTEXT_SCORE_GUILTY		10
170 #define CONTEXT_SCORE_BAN_THRESHOLD	40
171 	/** ban_score: Accumulated score of all hangs caused by this context. */
172 	int ban_score;
173 
174 	/** remap_slice: Bitmask of cache lines that need remapping */
175 	u8 remap_slice;
176 };
177 
178 static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
179 {
180 	return test_bit(CONTEXT_CLOSED, &ctx->flags);
181 }
182 
183 static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
184 {
185 	GEM_BUG_ON(i915_gem_context_is_closed(ctx));
186 	__set_bit(CONTEXT_CLOSED, &ctx->flags);
187 }
188 
189 static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
190 {
191 	return test_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
192 }
193 
194 static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
195 {
196 	__set_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
197 }
198 
199 static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
200 {
201 	__clear_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
202 }
203 
204 static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
205 {
206 	return test_bit(CONTEXT_BANNABLE, &ctx->flags);
207 }
208 
209 static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
210 {
211 	__set_bit(CONTEXT_BANNABLE, &ctx->flags);
212 }
213 
214 static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
215 {
216 	__clear_bit(CONTEXT_BANNABLE, &ctx->flags);
217 }
218 
219 static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
220 {
221 	return test_bit(CONTEXT_BANNED, &ctx->flags);
222 }
223 
224 static inline void i915_gem_context_set_banned(struct i915_gem_context *ctx)
225 {
226 	__set_bit(CONTEXT_BANNED, &ctx->flags);
227 }
228 
229 static inline bool i915_gem_context_force_single_submission(const struct i915_gem_context *ctx)
230 {
231 	return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
232 }
233 
234 static inline void i915_gem_context_set_force_single_submission(struct i915_gem_context *ctx)
235 {
236 	__set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
237 }
238 
239 static inline bool i915_gem_context_is_default(const struct i915_gem_context *c)
240 {
241 	return c->user_handle == DEFAULT_CONTEXT_HANDLE;
242 }
243 
244 static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
245 {
246 	return !ctx->file_priv;
247 }
248 
249 /* i915_gem_context.c */
250 int __must_check i915_gem_context_init(struct drm_i915_private *dev_priv);
251 void i915_gem_context_lost(struct drm_i915_private *dev_priv);
252 void i915_gem_context_fini(struct drm_i915_private *dev_priv);
253 int i915_gem_context_open(struct drm_device *dev, struct drm_file *file);
254 void i915_gem_context_close(struct drm_device *dev, struct drm_file *file);
255 int i915_switch_context(struct drm_i915_gem_request *req);
256 int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv);
257 void i915_gem_context_free(struct kref *ctx_ref);
258 struct i915_gem_context *
259 i915_gem_context_create_gvt(struct drm_device *dev);
260 
261 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
262 				  struct drm_file *file);
263 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
264 				   struct drm_file *file);
265 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
266 				    struct drm_file *file_priv);
267 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
268 				    struct drm_file *file_priv);
269 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
270 				       struct drm_file *file);
271 
272 #endif /* !__I915_GEM_CONTEXT_H__ */
273