]> Git Repo - linux.git/blob - drivers/gpu/drm/i915/i915_gem_context.h
Merge tag 'devicetree-for-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / gpu / drm / i915 / i915_gem_context.h
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 #include <linux/radix-tree.h>
31
32 #include "i915_gem.h"
33
34 struct pid;
35
36 struct drm_device;
37 struct drm_file;
38
39 struct drm_i915_private;
40 struct drm_i915_file_private;
41 struct i915_hw_ppgtt;
42 struct i915_request;
43 struct i915_vma;
44 struct intel_ring;
45
46 #define DEFAULT_CONTEXT_HANDLE 0
47
48 /**
49  * struct i915_gem_context - client state
50  *
51  * The struct i915_gem_context represents the combined view of the driver and
52  * logical hardware state for a particular client.
53  */
54 struct i915_gem_context {
55         /** i915: i915 device backpointer */
56         struct drm_i915_private *i915;
57
58         /** file_priv: owning file descriptor */
59         struct drm_i915_file_private *file_priv;
60
61         /**
62          * @ppgtt: unique address space (GTT)
63          *
64          * In full-ppgtt mode, each context has its own address space ensuring
65          * complete seperation of one client from all others.
66          *
67          * In other modes, this is a NULL pointer with the expectation that
68          * the caller uses the shared global GTT.
69          */
70         struct i915_hw_ppgtt *ppgtt;
71
72         /**
73          * @pid: process id of creator
74          *
75          * Note that who created the context may not be the principle user,
76          * as the context may be shared across a local socket. However,
77          * that should only affect the default context, all contexts created
78          * explicitly by the client are expected to be isolated.
79          */
80         struct pid *pid;
81
82         /**
83          * @name: arbitrary name
84          *
85          * A name is constructed for the context from the creator's process
86          * name, pid and user handle in order to uniquely identify the
87          * context in messages.
88          */
89         const char *name;
90
91         /** link: place with &drm_i915_private.context_list */
92         struct list_head link;
93         struct llist_node free_link;
94
95         /**
96          * @ref: reference count
97          *
98          * A reference to a context is held by both the client who created it
99          * and on each request submitted to the hardware using the request
100          * (to ensure the hardware has access to the state until it has
101          * finished all pending writes). See i915_gem_context_get() and
102          * i915_gem_context_put() for access.
103          */
104         struct kref ref;
105
106         /**
107          * @rcu: rcu_head for deferred freeing.
108          */
109         struct rcu_head rcu;
110
111         /**
112          * @flags: small set of booleans
113          */
114         unsigned long flags;
115 #define CONTEXT_NO_ZEROMAP              BIT(0)
116 #define CONTEXT_NO_ERROR_CAPTURE        1
117 #define CONTEXT_CLOSED                  2
118 #define CONTEXT_BANNABLE                3
119 #define CONTEXT_BANNED                  4
120 #define CONTEXT_FORCE_SINGLE_SUBMISSION 5
121
122         /**
123          * @hw_id: - unique identifier for the context
124          *
125          * The hardware needs to uniquely identify the context for a few
126          * functions like fault reporting, PASID, scheduling. The
127          * &drm_i915_private.context_hw_ida is used to assign a unqiue
128          * id for the lifetime of the context.
129          */
130         unsigned int hw_id;
131
132         /**
133          * @user_handle: userspace identifier
134          *
135          * A unique per-file identifier is generated from
136          * &drm_i915_file_private.contexts.
137          */
138         u32 user_handle;
139
140         struct i915_sched_attr sched;
141
142         /** ggtt_offset_bias: placement restriction for context objects */
143         u32 ggtt_offset_bias;
144
145         /** engine: per-engine logical HW state */
146         struct intel_context {
147                 struct i915_vma *state;
148                 struct intel_ring *ring;
149                 u32 *lrc_reg_state;
150                 u64 lrc_desc;
151                 int pin_count;
152         } __engine[I915_NUM_ENGINES];
153
154         /** ring_size: size for allocating the per-engine ring buffer */
155         u32 ring_size;
156         /** desc_template: invariant fields for the HW context descriptor */
157         u32 desc_template;
158
159         /** guilty_count: How many times this context has caused a GPU hang. */
160         atomic_t guilty_count;
161         /**
162          * @active_count: How many times this context was active during a GPU
163          * hang, but did not cause it.
164          */
165         atomic_t active_count;
166
167 #define CONTEXT_SCORE_GUILTY            10
168 #define CONTEXT_SCORE_BAN_THRESHOLD     40
169         /** ban_score: Accumulated score of all hangs caused by this context. */
170         atomic_t ban_score;
171
172         /** remap_slice: Bitmask of cache lines that need remapping */
173         u8 remap_slice;
174
175         /** handles_vma: rbtree to look up our context specific obj/vma for
176          * the user handle. (user handles are per fd, but the binding is
177          * per vm, which may be one per context or shared with the global GTT)
178          */
179         struct radix_tree_root handles_vma;
180
181         /** handles_list: reverse list of all the rbtree entries in use for
182          * this context, which allows us to free all the allocations on
183          * context close.
184          */
185         struct list_head handles_list;
186 };
187
188 static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
189 {
190         return test_bit(CONTEXT_CLOSED, &ctx->flags);
191 }
192
193 static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
194 {
195         GEM_BUG_ON(i915_gem_context_is_closed(ctx));
196         __set_bit(CONTEXT_CLOSED, &ctx->flags);
197 }
198
199 static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
200 {
201         return test_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
202 }
203
204 static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
205 {
206         __set_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
207 }
208
209 static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
210 {
211         __clear_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
212 }
213
214 static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
215 {
216         return test_bit(CONTEXT_BANNABLE, &ctx->flags);
217 }
218
219 static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
220 {
221         __set_bit(CONTEXT_BANNABLE, &ctx->flags);
222 }
223
224 static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
225 {
226         __clear_bit(CONTEXT_BANNABLE, &ctx->flags);
227 }
228
229 static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
230 {
231         return test_bit(CONTEXT_BANNED, &ctx->flags);
232 }
233
234 static inline void i915_gem_context_set_banned(struct i915_gem_context *ctx)
235 {
236         __set_bit(CONTEXT_BANNED, &ctx->flags);
237 }
238
239 static inline bool i915_gem_context_force_single_submission(const struct i915_gem_context *ctx)
240 {
241         return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
242 }
243
244 static inline void i915_gem_context_set_force_single_submission(struct i915_gem_context *ctx)
245 {
246         __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
247 }
248
249 static inline bool i915_gem_context_is_default(const struct i915_gem_context *c)
250 {
251         return c->user_handle == DEFAULT_CONTEXT_HANDLE;
252 }
253
254 static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
255 {
256         return !ctx->file_priv;
257 }
258
259 static inline struct intel_context *
260 to_intel_context(struct i915_gem_context *ctx,
261                  const struct intel_engine_cs *engine)
262 {
263         return &ctx->__engine[engine->id];
264 }
265
266 static inline struct intel_ring *
267 intel_context_pin(struct i915_gem_context *ctx, struct intel_engine_cs *engine)
268 {
269         return engine->context_pin(engine, ctx);
270 }
271
272 static inline void __intel_context_pin(struct i915_gem_context *ctx,
273                                        const struct intel_engine_cs *engine)
274 {
275         struct intel_context *ce = to_intel_context(ctx, engine);
276
277         GEM_BUG_ON(!ce->pin_count);
278         ce->pin_count++;
279 }
280
281 static inline void intel_context_unpin(struct i915_gem_context *ctx,
282                                        struct intel_engine_cs *engine)
283 {
284         engine->context_unpin(engine, ctx);
285 }
286
287 /* i915_gem_context.c */
288 int __must_check i915_gem_contexts_init(struct drm_i915_private *dev_priv);
289 void i915_gem_contexts_lost(struct drm_i915_private *dev_priv);
290 void i915_gem_contexts_fini(struct drm_i915_private *dev_priv);
291
292 int i915_gem_context_open(struct drm_i915_private *i915,
293                           struct drm_file *file);
294 void i915_gem_context_close(struct drm_file *file);
295
296 int i915_switch_context(struct i915_request *rq);
297 int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv);
298
299 void i915_gem_context_release(struct kref *ctx_ref);
300 struct i915_gem_context *
301 i915_gem_context_create_gvt(struct drm_device *dev);
302
303 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
304                                   struct drm_file *file);
305 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
306                                    struct drm_file *file);
307 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
308                                     struct drm_file *file_priv);
309 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
310                                     struct drm_file *file_priv);
311 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
312                                        struct drm_file *file);
313
314 struct i915_gem_context *
315 i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio);
316
317 static inline struct i915_gem_context *
318 i915_gem_context_get(struct i915_gem_context *ctx)
319 {
320         kref_get(&ctx->ref);
321         return ctx;
322 }
323
324 static inline void i915_gem_context_put(struct i915_gem_context *ctx)
325 {
326         kref_put(&ctx->ref, i915_gem_context_release);
327 }
328
329 #endif /* !__I915_GEM_CONTEXT_H__ */
This page took 0.053289 seconds and 4 git commands to generate.