Commit | Line | Data |
---|---|---|
39e2f501 CW |
1 | /* |
2 | * SPDX-License-Identifier: MIT | |
3 | * | |
4 | * Copyright © 2019 Intel Corporation | |
5 | */ | |
6 | ||
7 | #ifndef __INTEL_CONTEXT_H__ | |
8 | #define __INTEL_CONTEXT_H__ | |
9 | ||
9f3ccd40 | 10 | #include <linux/bitops.h> |
08819549 | 11 | #include <linux/lockdep.h> |
9f3ccd40 | 12 | #include <linux/types.h> |
08819549 | 13 | |
12c255b5 | 14 | #include "i915_active.h" |
39e2f501 CW |
15 | #include "intel_context_types.h" |
16 | #include "intel_engine_types.h" | |
2871ea85 | 17 | #include "intel_ring_types.h" |
e5dadff4 | 18 | #include "intel_timeline_types.h" |
39e2f501 | 19 | |
639f2f24 VSD |
20 | #define CE_TRACE(ce, fmt, ...) do { \ |
21 | const struct intel_context *ce__ = (ce); \ | |
22 | ENGINE_TRACE(ce__->engine, "context:%llx" fmt, \ | |
23 | ce__->timeline->fence_context, \ | |
24 | ##__VA_ARGS__); \ | |
25 | } while (0) | |
26 | ||
39e2f501 | 27 | void intel_context_init(struct intel_context *ce, |
39e2f501 | 28 | struct intel_engine_cs *engine); |
df8cf31e | 29 | void intel_context_fini(struct intel_context *ce); |
39e2f501 | 30 | |
c4d52feb | 31 | struct intel_context * |
e6ba7648 | 32 | intel_context_create(struct intel_engine_cs *engine); |
c4d52feb | 33 | |
5e2a0419 CW |
34 | void intel_context_free(struct intel_context *ce); |
35 | ||
c4d52feb | 36 | /** |
6b736de5 CW |
37 | * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context |
38 | * @ce - the context | |
c4d52feb | 39 | * |
08819549 CW |
40 | * Acquire a lock on the pinned status of the HW context, such that the context |
41 | * can neither be bound to the GPU or unbound whilst the lock is held, i.e. | |
42 | * intel_context_is_pinned() remains stable. | |
c4d52feb | 43 | */ |
6b736de5 CW |
44 | static inline int intel_context_lock_pinned(struct intel_context *ce) |
45 | __acquires(ce->pin_mutex) | |
46 | { | |
47 | return mutex_lock_interruptible(&ce->pin_mutex); | |
48 | } | |
c4d52feb | 49 | |
6b736de5 CW |
50 | /** |
51 | * intel_context_is_pinned - Reports the 'pinned' status | |
52 | * @ce - the context | |
53 | * | |
54 | * While in use by the GPU, the context, along with its ring and page | |
55 | * tables is pinned into memory and the GTT. | |
56 | * | |
57 | * Returns: true if the context is currently pinned for use by the GPU. | |
58 | */ | |
08819549 CW |
59 | static inline bool |
60 | intel_context_is_pinned(struct intel_context *ce) | |
61 | { | |
62 | return atomic_read(&ce->pin_count); | |
63 | } | |
64 | ||
6b736de5 CW |
65 | /** |
66 | * intel_context_unlock_pinned - Releases the earlier locking of 'pinned' status | |
67 | * @ce - the context | |
68 | * | |
69 | * Releases the lock earlier acquired by intel_context_unlock_pinned(). | |
70 | */ | |
71 | static inline void intel_context_unlock_pinned(struct intel_context *ce) | |
72 | __releases(ce->pin_mutex) | |
73 | { | |
74 | mutex_unlock(&ce->pin_mutex); | |
75 | } | |
08819549 | 76 | |
fa9f6681 CW |
77 | int __intel_context_do_pin(struct intel_context *ce); |
78 | ||
79 | static inline int intel_context_pin(struct intel_context *ce) | |
80 | { | |
81 | if (likely(atomic_inc_not_zero(&ce->pin_count))) | |
82 | return 0; | |
83 | ||
84 | return __intel_context_do_pin(ce); | |
85 | } | |
39e2f501 CW |
86 | |
87 | static inline void __intel_context_pin(struct intel_context *ce) | |
88 | { | |
08819549 CW |
89 | GEM_BUG_ON(!intel_context_is_pinned(ce)); |
90 | atomic_inc(&ce->pin_count); | |
39e2f501 CW |
91 | } |
92 | ||
08819549 | 93 | void intel_context_unpin(struct intel_context *ce); |
39e2f501 | 94 | |
6eee33e8 CW |
95 | void intel_context_enter_engine(struct intel_context *ce); |
96 | void intel_context_exit_engine(struct intel_context *ce); | |
97 | ||
98 | static inline void intel_context_enter(struct intel_context *ce) | |
99 | { | |
6c69a454 | 100 | lockdep_assert_held(&ce->timeline->mutex); |
6eee33e8 CW |
101 | if (!ce->active_count++) |
102 | ce->ops->enter(ce); | |
103 | } | |
104 | ||
105 | static inline void intel_context_mark_active(struct intel_context *ce) | |
106 | { | |
6c69a454 | 107 | lockdep_assert_held(&ce->timeline->mutex); |
6eee33e8 CW |
108 | ++ce->active_count; |
109 | } | |
110 | ||
111 | static inline void intel_context_exit(struct intel_context *ce) | |
112 | { | |
6c69a454 | 113 | lockdep_assert_held(&ce->timeline->mutex); |
6eee33e8 CW |
114 | GEM_BUG_ON(!ce->active_count); |
115 | if (!--ce->active_count) | |
116 | ce->ops->exit(ce); | |
117 | } | |
118 | ||
d8af05ff CW |
119 | int intel_context_active_acquire(struct intel_context *ce); |
120 | void intel_context_active_release(struct intel_context *ce); | |
ce476c80 | 121 | |
4c5896dc CW |
122 | static inline struct intel_context *intel_context_get(struct intel_context *ce) |
123 | { | |
124 | kref_get(&ce->ref); | |
125 | return ce; | |
126 | } | |
127 | ||
128 | static inline void intel_context_put(struct intel_context *ce) | |
129 | { | |
130 | kref_put(&ce->ref, ce->ops->destroy); | |
131 | } | |
132 | ||
e5dadff4 | 133 | static inline struct intel_timeline *__must_check |
f4d57d83 | 134 | intel_context_timeline_lock(struct intel_context *ce) |
75d0a7f3 | 135 | __acquires(&ce->timeline->mutex) |
2ccdf6a1 | 136 | { |
e5dadff4 CW |
137 | struct intel_timeline *tl = ce->timeline; |
138 | int err; | |
139 | ||
140 | err = mutex_lock_interruptible(&tl->mutex); | |
141 | if (err) | |
142 | return ERR_PTR(err); | |
143 | ||
144 | return tl; | |
2ccdf6a1 CW |
145 | } |
146 | ||
e5dadff4 CW |
147 | static inline void intel_context_timeline_unlock(struct intel_timeline *tl) |
148 | __releases(&tl->mutex) | |
2ccdf6a1 | 149 | { |
e5dadff4 | 150 | mutex_unlock(&tl->mutex); |
2ccdf6a1 CW |
151 | } |
152 | ||
a9877da2 CW |
153 | int intel_context_prepare_remote_request(struct intel_context *ce, |
154 | struct i915_request *rq); | |
155 | ||
5e2a0419 CW |
156 | struct i915_request *intel_context_create_request(struct intel_context *ce); |
157 | ||
48ae397b CW |
158 | static inline struct intel_ring *__intel_context_ring_size(u64 sz) |
159 | { | |
160 | return u64_to_ptr(struct intel_ring, sz); | |
161 | } | |
162 | ||
e6ba7648 CW |
163 | static inline bool intel_context_is_barrier(const struct intel_context *ce) |
164 | { | |
165 | return test_bit(CONTEXT_BARRIER_BIT, &ce->flags); | |
166 | } | |
167 | ||
0f100b70 CW |
168 | static inline bool intel_context_use_semaphores(const struct intel_context *ce) |
169 | { | |
170 | return test_bit(CONTEXT_USE_SEMAPHORES, &ce->flags); | |
171 | } | |
172 | ||
173 | static inline void intel_context_set_use_semaphores(struct intel_context *ce) | |
174 | { | |
175 | set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags); | |
176 | } | |
177 | ||
178 | static inline void intel_context_clear_use_semaphores(struct intel_context *ce) | |
179 | { | |
180 | clear_bit(CONTEXT_USE_SEMAPHORES, &ce->flags); | |
181 | } | |
182 | ||
9f3ccd40 CW |
183 | static inline bool intel_context_is_banned(const struct intel_context *ce) |
184 | { | |
185 | return test_bit(CONTEXT_BANNED, &ce->flags); | |
186 | } | |
187 | ||
188 | static inline bool intel_context_set_banned(struct intel_context *ce) | |
189 | { | |
190 | return test_and_set_bit(CONTEXT_BANNED, &ce->flags); | |
191 | } | |
192 | ||
193 | static inline bool | |
194 | intel_context_force_single_submission(const struct intel_context *ce) | |
195 | { | |
196 | return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags); | |
197 | } | |
198 | ||
199 | static inline void | |
200 | intel_context_set_single_submission(struct intel_context *ce) | |
201 | { | |
202 | __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags); | |
203 | } | |
204 | ||
205 | static inline bool | |
206 | intel_context_nopreempt(const struct intel_context *ce) | |
207 | { | |
208 | return test_bit(CONTEXT_NOPREEMPT, &ce->flags); | |
209 | } | |
210 | ||
211 | static inline void | |
212 | intel_context_set_nopreempt(struct intel_context *ce) | |
213 | { | |
214 | set_bit(CONTEXT_NOPREEMPT, &ce->flags); | |
215 | } | |
216 | ||
217 | static inline void | |
218 | intel_context_clear_nopreempt(struct intel_context *ce) | |
219 | { | |
220 | clear_bit(CONTEXT_NOPREEMPT, &ce->flags); | |
221 | } | |
222 | ||
39e2f501 | 223 | #endif /* __INTEL_CONTEXT_H__ */ |