1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2016 Intel Corporation
5  */
6 
7 #ifndef __I915_GEM_CONTEXT_H__
8 #define __I915_GEM_CONTEXT_H__
9 
10 #include "i915_gem_context_types.h"
11 
12 #include "gt/intel_context.h"
13 
14 #include "i915_drv.h"
15 #include "i915_gem.h"
16 #include "i915_scheduler.h"
17 #include "intel_device_info.h"
18 
19 struct drm_device;
20 struct drm_file;
21 
22 static inline bool i915_gem_context_is_closed(const struct i915_gem_context *ctx)
23 {
24 	return test_bit(CONTEXT_CLOSED, &ctx->flags);
25 }
26 
27 static inline void i915_gem_context_set_closed(struct i915_gem_context *ctx)
28 {
29 	GEM_BUG_ON(i915_gem_context_is_closed(ctx));
30 	set_bit(CONTEXT_CLOSED, &ctx->flags);
31 }
32 
33 static inline bool i915_gem_context_no_error_capture(const struct i915_gem_context *ctx)
34 {
35 	return test_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
36 }
37 
38 static inline void i915_gem_context_set_no_error_capture(struct i915_gem_context *ctx)
39 {
40 	set_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
41 }
42 
43 static inline void i915_gem_context_clear_no_error_capture(struct i915_gem_context *ctx)
44 {
45 	clear_bit(UCONTEXT_NO_ERROR_CAPTURE, &ctx->user_flags);
46 }
47 
48 static inline bool i915_gem_context_is_bannable(const struct i915_gem_context *ctx)
49 {
50 	return test_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
51 }
52 
53 static inline void i915_gem_context_set_bannable(struct i915_gem_context *ctx)
54 {
55 	set_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
56 }
57 
58 static inline void i915_gem_context_clear_bannable(struct i915_gem_context *ctx)
59 {
60 	clear_bit(UCONTEXT_BANNABLE, &ctx->user_flags);
61 }
62 
63 static inline bool i915_gem_context_is_recoverable(const struct i915_gem_context *ctx)
64 {
65 	return test_bit(UCONTEXT_RECOVERABLE, &ctx->user_flags);
66 }
67 
68 static inline void i915_gem_context_set_recoverable(struct i915_gem_context *ctx)
69 {
70 	set_bit(UCONTEXT_RECOVERABLE, &ctx->user_flags);
71 }
72 
73 static inline void i915_gem_context_clear_recoverable(struct i915_gem_context *ctx)
74 {
75 	clear_bit(UCONTEXT_RECOVERABLE, &ctx->user_flags);
76 }
77 
78 static inline bool i915_gem_context_is_persistent(const struct i915_gem_context *ctx)
79 {
80 	return test_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
81 }
82 
83 static inline void i915_gem_context_set_persistence(struct i915_gem_context *ctx)
84 {
85 	set_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
86 }
87 
88 static inline void i915_gem_context_clear_persistence(struct i915_gem_context *ctx)
89 {
90 	clear_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
91 }
92 
93 static inline bool
94 i915_gem_context_user_engines(const struct i915_gem_context *ctx)
95 {
96 	return test_bit(CONTEXT_USER_ENGINES, &ctx->flags);
97 }
98 
99 static inline void
100 i915_gem_context_set_user_engines(struct i915_gem_context *ctx)
101 {
102 	set_bit(CONTEXT_USER_ENGINES, &ctx->flags);
103 }
104 
105 static inline void
106 i915_gem_context_clear_user_engines(struct i915_gem_context *ctx)
107 {
108 	clear_bit(CONTEXT_USER_ENGINES, &ctx->flags);
109 }
110 
111 /* i915_gem_context.c */
112 void i915_gem_init__contexts(struct drm_i915_private *i915);
113 
114 int i915_gem_context_open(struct drm_i915_private *i915,
115 			  struct drm_file *file);
116 void i915_gem_context_close(struct drm_file *file);
117 
118 void i915_gem_context_release(struct kref *ctx_ref);
119 
120 int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data,
121 			     struct drm_file *file);
122 int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data,
123 			      struct drm_file *file);
124 
125 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
126 				  struct drm_file *file);
127 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
128 				   struct drm_file *file);
129 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
130 				    struct drm_file *file_priv);
131 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
132 				    struct drm_file *file_priv);
133 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, void *data,
134 				       struct drm_file *file);
135 
136 static inline struct i915_gem_context *
137 i915_gem_context_get(struct i915_gem_context *ctx)
138 {
139 	kref_get(&ctx->ref);
140 	return ctx;
141 }
142 
143 static inline void i915_gem_context_put(struct i915_gem_context *ctx)
144 {
145 	kref_put(&ctx->ref, i915_gem_context_release);
146 }
147 
148 static inline struct i915_address_space *
149 i915_gem_context_vm(struct i915_gem_context *ctx)
150 {
151 	return rcu_dereference_protected(ctx->vm, lockdep_is_held(&ctx->mutex));
152 }
153 
154 static inline struct i915_address_space *
155 i915_gem_context_get_vm_rcu(struct i915_gem_context *ctx)
156 {
157 	struct i915_address_space *vm;
158 
159 	rcu_read_lock();
160 	vm = rcu_dereference(ctx->vm);
161 	if (!vm)
162 		vm = &ctx->i915->ggtt.vm;
163 	vm = i915_vm_get(vm);
164 	rcu_read_unlock();
165 
166 	return vm;
167 }
168 
169 static inline struct i915_gem_engines *
170 i915_gem_context_engines(struct i915_gem_context *ctx)
171 {
172 	return rcu_dereference_protected(ctx->engines,
173 					 lockdep_is_held(&ctx->engines_mutex));
174 }
175 
176 static inline struct i915_gem_engines *
177 i915_gem_context_lock_engines(struct i915_gem_context *ctx)
178 	__acquires(&ctx->engines_mutex)
179 {
180 	mutex_lock(&ctx->engines_mutex);
181 	return i915_gem_context_engines(ctx);
182 }
183 
184 static inline void
185 i915_gem_context_unlock_engines(struct i915_gem_context *ctx)
186 	__releases(&ctx->engines_mutex)
187 {
188 	mutex_unlock(&ctx->engines_mutex);
189 }
190 
191 static inline struct intel_context *
192 i915_gem_context_get_engine(struct i915_gem_context *ctx, unsigned int idx)
193 {
194 	struct intel_context *ce;
195 
196 	rcu_read_lock(); {
197 		struct i915_gem_engines *e = rcu_dereference(ctx->engines);
198 		if (unlikely(!e)) /* context was closed! */
199 			ce = ERR_PTR(-ENOENT);
200 		else if (likely(idx < e->num_engines && e->engines[idx]))
201 			ce = intel_context_get(e->engines[idx]);
202 		else
203 			ce = ERR_PTR(-EINVAL);
204 	} rcu_read_unlock();
205 
206 	return ce;
207 }
208 
209 static inline void
210 i915_gem_engines_iter_init(struct i915_gem_engines_iter *it,
211 			   struct i915_gem_engines *engines)
212 {
213 	it->engines = engines;
214 	it->idx = 0;
215 }
216 
217 struct intel_context *
218 i915_gem_engines_iter_next(struct i915_gem_engines_iter *it);
219 
220 #define for_each_gem_engine(ce, engines, it) \
221 	for (i915_gem_engines_iter_init(&(it), (engines)); \
222 	     ((ce) = i915_gem_engines_iter_next(&(it)));)
223 
224 struct i915_lut_handle *i915_lut_handle_alloc(void);
225 void i915_lut_handle_free(struct i915_lut_handle *lut);
226 
227 int i915_gem_user_to_context_sseu(struct intel_gt *gt,
228 				  const struct drm_i915_gem_context_param_sseu *user,
229 				  struct intel_sseu *context);
230 
231 #endif /* !__I915_GEM_CONTEXT_H__ */
232