2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #ifndef __I915_GEM_CONTEXT_H__
26 #define __I915_GEM_CONTEXT_H__
28 #include <linux/bitops.h>
29 #include <linux/list.h>
30 #include <linux/radix-tree.h>
33 #include "i915_scheduler.h"
40 struct drm_i915_private;
41 struct drm_i915_file_private;
47 #define DEFAULT_CONTEXT_HANDLE 0
51 struct intel_context_ops {
52 void (*unpin)(struct intel_context *ce);
53 void (*destroy)(struct intel_context *ce);
57 * struct i915_gem_context - client state
59 * The struct i915_gem_context represents the combined view of the driver and
60 * logical hardware state for a particular client.
62 struct i915_gem_context {
63 /** i915: i915 device backpointer */
64 struct drm_i915_private *i915;
66 /** file_priv: owning file descriptor */
67 struct drm_i915_file_private *file_priv;
70 * @ppgtt: unique address space (GTT)
72 * In full-ppgtt mode, each context has its own address space ensuring
73 * complete seperation of one client from all others.
75 * In other modes, this is a NULL pointer with the expectation that
76 * the caller uses the shared global GTT.
78 struct i915_hw_ppgtt *ppgtt;
81 * @pid: process id of creator
83 * Note that who created the context may not be the principle user,
84 * as the context may be shared across a local socket. However,
85 * that should only affect the default context, all contexts created
86 * explicitly by the client are expected to be isolated.
91 * @name: arbitrary name
93 * A name is constructed for the context from the creator's process
94 * name, pid and user handle in order to uniquely identify the
95 * context in messages.
99 /** link: place with &drm_i915_private.context_list */
100 struct list_head link;
101 struct llist_node free_link;
104 * @ref: reference count
106 * A reference to a context is held by both the client who created it
107 * and on each request submitted to the hardware using the request
108 * (to ensure the hardware has access to the state until it has
109 * finished all pending writes). See i915_gem_context_get() and
110 * i915_gem_context_put() for access.
115 * @rcu: rcu_head for deferred freeing.
120 * @flags: small set of booleans
123 #define CONTEXT_NO_ZEROMAP BIT(0)
124 #define CONTEXT_NO_ERROR_CAPTURE 1
125 #define CONTEXT_CLOSED 2
126 #define CONTEXT_BANNABLE 3
127 #define CONTEXT_BANNED 4
128 #define CONTEXT_FORCE_SINGLE_SUBMISSION 5
131 * @hw_id: - unique identifier for the context
133 * The hardware needs to uniquely identify the context for a few
134 * functions like fault reporting, PASID, scheduling. The
135 * &drm_i915_private.context_hw_ida is used to assign a unqiue
136 * id for the lifetime of the context.
138 * @hw_id_pin_count: - number of times this context had been pinned
139 * for use (should be, at most, once per engine).
141 * @hw_id_link: - all contexts with an assigned id are tracked
142 * for possible repossession.
145 atomic_t hw_id_pin_count;
146 struct list_head hw_id_link;
149 * @user_handle: userspace identifier
151 * A unique per-file identifier is generated from
152 * &drm_i915_file_private.contexts.
156 struct i915_sched_attr sched;
158 /** engine: per-engine logical HW state */
159 struct intel_context {
160 struct i915_gem_context *gem_context;
161 struct i915_vma *state;
162 struct intel_ring *ring;
167 const struct intel_context_ops *ops;
168 } __engine[I915_NUM_ENGINES];
170 /** ring_size: size for allocating the per-engine ring buffer */
172 /** desc_template: invariant fields for the HW context descriptor */
175 /** guilty_count: How many times this context has caused a GPU hang. */
176 atomic_t guilty_count;
178 * @active_count: How many times this context was active during a GPU
179 * hang, but did not cause it.
181 atomic_t active_count;
183 #define CONTEXT_SCORE_GUILTY 10
184 #define CONTEXT_SCORE_BAN_THRESHOLD 40
185 /** ban_score: Accumulated score of all hangs caused by this context. */
188 /** remap_slice: Bitmask of cache lines that need remapping */
191 /** handles_vma: rbtree to look up our context specific obj/vma for
192 * the user handle. (user handles are per fd, but the binding is
193 * per vm, which may be one per context or shared with the global GTT)
195 struct radix_tree_root handles_vma;
197 /** handles_list: reverse list of all the rbtree entries in use for
198 * this context, which allows us to free all the allocations on
201 struct list_head handles_list;
204 static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
206 return test_bit(CONTEXT_CLOSED, &ctx->flags);
209 static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
211 GEM_BUG_ON(i915_gem_context_is_closed(ctx));
212 __set_bit(CONTEXT_CLOSED, &ctx->flags);
215 static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
217 return test_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
220 static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
222 __set_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
225 static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
227 __clear_bit(CONTEXT_NO_ERROR_CAPTURE, &ctx->flags);
230 static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
232 return test_bit(CONTEXT_BANNABLE, &ctx->flags);
235 static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
237 __set_bit(CONTEXT_BANNABLE, &ctx->flags);
240 static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
242 __clear_bit(CONTEXT_BANNABLE, &ctx->flags);
245 static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
247 return test_bit(CONTEXT_BANNED, &ctx->flags);
250 static inline void i915_gem_context_set_banned(struct i915_gem_context *ctx)
252 __set_bit(CONTEXT_BANNED, &ctx->flags);
255 static inline bool i915_gem_context_force_single_submission(const struct i915_gem_context *ctx)
257 return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
260 static inline void i915_gem_context_set_force_single_submission(struct i915_gem_context *ctx)
262 __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ctx->flags);
265 int __i915_gem_context_pin_hw_id(struct i915_gem_context *ctx);
266 static inline int i915_gem_context_pin_hw_id(struct i915_gem_context *ctx)
268 if (atomic_inc_not_zero(&ctx->hw_id_pin_count))
271 return __i915_gem_context_pin_hw_id(ctx);
274 static inline void i915_gem_context_unpin_hw_id(struct i915_gem_context *ctx)
276 GEM_BUG_ON(atomic_read(&ctx->hw_id_pin_count) == 0u);
277 atomic_dec(&ctx->hw_id_pin_count);
280 static inline bool i915_gem_context_is_default(const struct i915_gem_context *c)
282 return c->user_handle == DEFAULT_CONTEXT_HANDLE;
285 static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
287 return !ctx->file_priv;
290 static inline struct intel_context *
291 to_intel_context(struct i915_gem_context *ctx,
292 const struct intel_engine_cs *engine)
294 return &ctx->__engine[engine->id];
297 static inline struct intel_context *
298 intel_context_pin(struct i915_gem_context *ctx, struct intel_engine_cs *engine)
300 return engine->context_pin(engine, ctx);
303 static inline void __intel_context_pin(struct intel_context *ce)
305 GEM_BUG_ON(!ce->pin_count);
309 static inline void intel_context_unpin(struct intel_context *ce)
311 GEM_BUG_ON(!ce->pin_count);
315 GEM_BUG_ON(!ce->ops);
319 /* i915_gem_context.c */
320 int __must_check i915_gem_contexts_init(struct drm_i915_private *dev_priv);
321 void i915_gem_contexts_lost(struct drm_i915_private *dev_priv);
322 void i915_gem_contexts_fini(struct drm_i915_private *dev_priv);
324 int i915_gem_context_open(struct drm_i915_private *i915,
325 struct drm_file *file);
326 void i915_gem_context_close(struct drm_file *file);
328 int i915_switch_context(struct i915_request *rq);
329 int i915_gem_switch_to_kernel_context(struct drm_i915_private *dev_priv);
331 void i915_gem_context_release(struct kref *ctx_ref);
332 struct i915_gem_context *
333 i915_gem_context_create_gvt(struct drm_device *dev);
335 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
336 struct drm_file *file);
337 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
338 struct drm_file *file);
339 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
340 struct drm_file *file_priv);
341 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
342 struct drm_file *file_priv);
343 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
344 struct drm_file *file);
346 struct i915_gem_context *
347 i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio);
349 static inline struct i915_gem_context *
350 i915_gem_context_get(struct i915_gem_context *ctx)
356 static inline void i915_gem_context_put(struct i915_gem_context *ctx)
358 kref_put(&ctx->ref, i915_gem_context_release);
361 #endif /* !__I915_GEM_CONTEXT_H__ */