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