2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #ifndef __INTEL_CONTEXT_H__
8 #define __INTEL_CONTEXT_H__
10 #include <linux/bitops.h>
11 #include <linux/lockdep.h>
12 #include <linux/types.h>
14 #include "i915_active.h"
16 #include "intel_context_types.h"
17 #include "intel_engine_types.h"
18 #include "intel_ring_types.h"
19 #include "intel_timeline_types.h"
21 #define CE_TRACE(ce, fmt, ...) do { \
22 const struct intel_context *ce__ = (ce); \
23 ENGINE_TRACE(ce__->engine, "context:%llx " fmt, \
24 ce__->timeline->fence_context, \
28 struct i915_gem_ww_ctx
;
30 void intel_context_init(struct intel_context
*ce
,
31 struct intel_engine_cs
*engine
);
32 void intel_context_fini(struct intel_context
*ce
);
34 struct intel_context
*
35 intel_context_create(struct intel_engine_cs
*engine
);
37 int intel_context_alloc_state(struct intel_context
*ce
);
39 void intel_context_free(struct intel_context
*ce
);
41 int intel_context_reconfigure_sseu(struct intel_context
*ce
,
42 const struct intel_sseu sseu
);
45 * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
48 * Acquire a lock on the pinned status of the HW context, such that the context
49 * can neither be bound to the GPU or unbound whilst the lock is held, i.e.
50 * intel_context_is_pinned() remains stable.
52 static inline int intel_context_lock_pinned(struct intel_context
*ce
)
53 __acquires(ce
->pin_mutex
)
55 return mutex_lock_interruptible(&ce
->pin_mutex
);
59 * intel_context_is_pinned - Reports the 'pinned' status
62 * While in use by the GPU, the context, along with its ring and page
63 * tables is pinned into memory and the GTT.
65 * Returns: true if the context is currently pinned for use by the GPU.
68 intel_context_is_pinned(struct intel_context
*ce
)
70 return atomic_read(&ce
->pin_count
);
74 * intel_context_unlock_pinned - Releases the earlier locking of 'pinned' status
77 * Releases the lock earlier acquired by intel_context_unlock_pinned().
79 static inline void intel_context_unlock_pinned(struct intel_context
*ce
)
80 __releases(ce
->pin_mutex
)
82 mutex_unlock(&ce
->pin_mutex
);
85 int __intel_context_do_pin(struct intel_context
*ce
);
86 int __intel_context_do_pin_ww(struct intel_context
*ce
,
87 struct i915_gem_ww_ctx
*ww
);
89 static inline bool intel_context_pin_if_active(struct intel_context
*ce
)
91 return atomic_inc_not_zero(&ce
->pin_count
);
94 static inline int intel_context_pin(struct intel_context
*ce
)
96 if (likely(intel_context_pin_if_active(ce
)))
99 return __intel_context_do_pin(ce
);
102 static inline int intel_context_pin_ww(struct intel_context
*ce
,
103 struct i915_gem_ww_ctx
*ww
)
105 if (likely(intel_context_pin_if_active(ce
)))
108 return __intel_context_do_pin_ww(ce
, ww
);
111 static inline void __intel_context_pin(struct intel_context
*ce
)
113 GEM_BUG_ON(!intel_context_is_pinned(ce
));
114 atomic_inc(&ce
->pin_count
);
117 void intel_context_unpin(struct intel_context
*ce
);
119 void intel_context_enter_engine(struct intel_context
*ce
);
120 void intel_context_exit_engine(struct intel_context
*ce
);
122 static inline void intel_context_enter(struct intel_context
*ce
)
124 lockdep_assert_held(&ce
->timeline
->mutex
);
125 if (!ce
->active_count
++)
129 static inline void intel_context_mark_active(struct intel_context
*ce
)
131 lockdep_assert_held(&ce
->timeline
->mutex
);
135 static inline void intel_context_exit(struct intel_context
*ce
)
137 lockdep_assert_held(&ce
->timeline
->mutex
);
138 GEM_BUG_ON(!ce
->active_count
);
139 if (!--ce
->active_count
)
143 static inline struct intel_context
*intel_context_get(struct intel_context
*ce
)
149 static inline void intel_context_put(struct intel_context
*ce
)
151 kref_put(&ce
->ref
, ce
->ops
->destroy
);
154 static inline struct intel_timeline
*__must_check
155 intel_context_timeline_lock(struct intel_context
*ce
)
156 __acquires(&ce
->timeline
->mutex
)
158 struct intel_timeline
*tl
= ce
->timeline
;
161 err
= mutex_lock_interruptible(&tl
->mutex
);
168 static inline void intel_context_timeline_unlock(struct intel_timeline
*tl
)
169 __releases(&tl
->mutex
)
171 mutex_unlock(&tl
->mutex
);
174 int intel_context_prepare_remote_request(struct intel_context
*ce
,
175 struct i915_request
*rq
);
177 struct i915_request
*intel_context_create_request(struct intel_context
*ce
);
179 static inline struct intel_ring
*__intel_context_ring_size(u64 sz
)
181 return u64_to_ptr(struct intel_ring
, sz
);
184 static inline bool intel_context_is_barrier(const struct intel_context
*ce
)
186 return test_bit(CONTEXT_BARRIER_BIT
, &ce
->flags
);
189 static inline bool intel_context_is_closed(const struct intel_context
*ce
)
191 return test_bit(CONTEXT_CLOSED_BIT
, &ce
->flags
);
194 static inline bool intel_context_use_semaphores(const struct intel_context
*ce
)
196 return test_bit(CONTEXT_USE_SEMAPHORES
, &ce
->flags
);
199 static inline void intel_context_set_use_semaphores(struct intel_context
*ce
)
201 set_bit(CONTEXT_USE_SEMAPHORES
, &ce
->flags
);
204 static inline void intel_context_clear_use_semaphores(struct intel_context
*ce
)
206 clear_bit(CONTEXT_USE_SEMAPHORES
, &ce
->flags
);
209 static inline bool intel_context_is_banned(const struct intel_context
*ce
)
211 return test_bit(CONTEXT_BANNED
, &ce
->flags
);
214 static inline bool intel_context_set_banned(struct intel_context
*ce
)
216 return test_and_set_bit(CONTEXT_BANNED
, &ce
->flags
);
220 intel_context_force_single_submission(const struct intel_context
*ce
)
222 return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION
, &ce
->flags
);
226 intel_context_set_single_submission(struct intel_context
*ce
)
228 __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION
, &ce
->flags
);
232 intel_context_nopreempt(const struct intel_context
*ce
)
234 return test_bit(CONTEXT_NOPREEMPT
, &ce
->flags
);
238 intel_context_set_nopreempt(struct intel_context
*ce
)
240 set_bit(CONTEXT_NOPREEMPT
, &ce
->flags
);
244 intel_context_clear_nopreempt(struct intel_context
*ce
)
246 clear_bit(CONTEXT_NOPREEMPT
, &ce
->flags
);
249 static inline u64
intel_context_get_total_runtime_ns(struct intel_context
*ce
)
252 RUNTIME_INFO(ce
->engine
->i915
)->cs_timestamp_period_ns
;
254 return READ_ONCE(ce
->runtime
.total
) * period
;
257 static inline u64
intel_context_get_avg_runtime_ns(struct intel_context
*ce
)
260 RUNTIME_INFO(ce
->engine
->i915
)->cs_timestamp_period_ns
;
262 return mul_u32_u32(ewma_runtime_read(&ce
->runtime
.avg
), period
);
265 #endif /* __INTEL_CONTEXT_H__ */