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 struct i915_gem_engines {
34 	union {
35 		struct list_head link;
36 		struct rcu_head rcu;
37 	};
38 	struct i915_sw_fence fence;
39 	struct i915_gem_context *ctx;
40 	unsigned int num_engines;
41 	struct intel_context *engines[];
42 };
43 
44 struct i915_gem_engines_iter {
45 	unsigned int idx;
46 	const struct i915_gem_engines *engines;
47 };
48 
49 /**
50  * struct i915_gem_context - client state
51  *
52  * The struct i915_gem_context represents the combined view of the driver and
53  * logical hardware state for a particular client.
54  */
55 struct i915_gem_context {
56 	/** i915: i915 device backpointer */
57 	struct drm_i915_private *i915;
58 
59 	/** file_priv: owning file descriptor */
60 	struct drm_i915_file_private *file_priv;
61 
62 	/**
63 	 * @engines: User defined engines for this context
64 	 *
65 	 * Various uAPI offer the ability to lookup up an
66 	 * index from this array to select an engine operate on.
67 	 *
68 	 * Multiple logically distinct instances of the same engine
69 	 * may be defined in the array, as well as composite virtual
70 	 * engines.
71 	 *
72 	 * Execbuf uses the I915_EXEC_RING_MASK as an index into this
73 	 * array to select which HW context + engine to execute on. For
74 	 * the default array, the user_ring_map[] is used to translate
75 	 * the legacy uABI onto the approprate index (e.g. both
76 	 * I915_EXEC_DEFAULT and I915_EXEC_RENDER select the same
77 	 * context, and I915_EXEC_BSD is weird). For a use defined
78 	 * array, execbuf uses I915_EXEC_RING_MASK as a plain index.
79 	 *
80 	 * User defined by I915_CONTEXT_PARAM_ENGINE (when the
81 	 * CONTEXT_USER_ENGINES flag is set).
82 	 */
83 	struct i915_gem_engines __rcu *engines;
84 	struct mutex engines_mutex; /* guards writes to engines */
85 
86 	/**
87 	 * @syncobj: Shared timeline syncobj
88 	 *
89 	 * When the SHARED_TIMELINE flag is set on context creation, we
90 	 * emulate a single timeline across all engines using this syncobj.
91 	 * For every execbuffer2 call, this syncobj is used as both an in-
92 	 * and out-fence.  Unlike the real intel_timeline, this doesn't
93 	 * provide perfect atomic in-order guarantees if the client races
94 	 * with itself by calling execbuffer2 twice concurrently.  However,
95 	 * if userspace races with itself, that's not likely to yield well-
96 	 * defined results anyway so we choose to not care.
97 	 */
98 	struct drm_syncobj *syncobj;
99 
100 	/**
101 	 * @vm: unique address space (GTT)
102 	 *
103 	 * In full-ppgtt mode, each context has its own address space ensuring
104 	 * complete seperation of one client from all others.
105 	 *
106 	 * In other modes, this is a NULL pointer with the expectation that
107 	 * the caller uses the shared global GTT.
108 	 */
109 	struct i915_address_space __rcu *vm;
110 
111 	/**
112 	 * @pid: process id of creator
113 	 *
114 	 * Note that who created the context may not be the principle user,
115 	 * as the context may be shared across a local socket. However,
116 	 * that should only affect the default context, all contexts created
117 	 * explicitly by the client are expected to be isolated.
118 	 */
119 	struct pid *pid;
120 
121 	/** link: place with &drm_i915_private.context_list */
122 	struct list_head link;
123 
124 	/**
125 	 * @ref: reference count
126 	 *
127 	 * A reference to a context is held by both the client who created it
128 	 * and on each request submitted to the hardware using the request
129 	 * (to ensure the hardware has access to the state until it has
130 	 * finished all pending writes). See i915_gem_context_get() and
131 	 * i915_gem_context_put() for access.
132 	 */
133 	struct kref ref;
134 
135 	/**
136 	 * @rcu: rcu_head for deferred freeing.
137 	 */
138 	struct rcu_head rcu;
139 
140 	/**
141 	 * @user_flags: small set of booleans controlled by the user
142 	 */
143 	unsigned long user_flags;
144 #define UCONTEXT_NO_ERROR_CAPTURE	1
145 #define UCONTEXT_BANNABLE		2
146 #define UCONTEXT_RECOVERABLE		3
147 #define UCONTEXT_PERSISTENCE		4
148 
149 	/**
150 	 * @flags: small set of booleans
151 	 */
152 	unsigned long flags;
153 #define CONTEXT_CLOSED			0
154 #define CONTEXT_USER_ENGINES		1
155 
156 	struct mutex mutex;
157 
158 	struct i915_sched_attr sched;
159 
160 	/** guilty_count: How many times this context has caused a GPU hang. */
161 	atomic_t guilty_count;
162 	/**
163 	 * @active_count: How many times this context was active during a GPU
164 	 * hang, but did not cause it.
165 	 */
166 	atomic_t active_count;
167 
168 	/**
169 	 * @hang_timestamp: The last time(s) this context caused a GPU hang
170 	 */
171 	unsigned long hang_timestamp[2];
172 #define CONTEXT_FAST_HANG_JIFFIES (120 * HZ) /* 3 hangs within 120s? Banned! */
173 
174 	/** remap_slice: Bitmask of cache lines that need remapping */
175 	u8 remap_slice;
176 
177 	/**
178 	 * handles_vma: rbtree to look up our context specific obj/vma for
179 	 * the user handle. (user handles are per fd, but the binding is
180 	 * per vm, which may be one per context or shared with the global GTT)
181 	 */
182 	struct radix_tree_root handles_vma;
183 	struct mutex lut_mutex;
184 
185 	/**
186 	 * @name: arbitrary name, used for user debug
187 	 *
188 	 * A name is constructed for the context from the creator's process
189 	 * name, pid and user handle in order to uniquely identify the
190 	 * context in messages.
191 	 */
192 	char name[TASK_COMM_LEN + 8];
193 
194 	struct {
195 		spinlock_t lock;
196 		struct list_head engines;
197 	} stale;
198 };
199 
200 #endif /* __I915_GEM_CONTEXT_TYPES_H__ */
201