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