2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #ifndef __INTEL_CONTEXT_H__
8 #define __INTEL_CONTEXT_H__
10 #include <linux/lockdep.h>
12 #include "intel_context_types.h"
13 #include "intel_engine_types.h"
15 void intel_context_init(struct intel_context *ce,
16 struct i915_gem_context *ctx,
17 struct intel_engine_cs *engine);
19 struct intel_context *
20 intel_context_create(struct i915_gem_context *ctx,
21 struct intel_engine_cs *engine);
23 void intel_context_free(struct intel_context *ce);
26 * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
29 * Acquire a lock on the pinned status of the HW context, such that the context
30 * can neither be bound to the GPU or unbound whilst the lock is held, i.e.
31 * intel_context_is_pinned() remains stable.
33 static inline int intel_context_lock_pinned(struct intel_context *ce)
34 __acquires(ce->pin_mutex)
36 return mutex_lock_interruptible(&ce->pin_mutex);
40 * intel_context_is_pinned - Reports the 'pinned' status
43 * While in use by the GPU, the context, along with its ring and page
44 * tables is pinned into memory and the GTT.
46 * Returns: true if the context is currently pinned for use by the GPU.
49 intel_context_is_pinned(struct intel_context *ce)
51 return atomic_read(&ce->pin_count);
55 * intel_context_unlock_pinned - Releases the earlier locking of 'pinned' status
58 * Releases the lock earlier acquired by intel_context_unlock_pinned().
60 static inline void intel_context_unlock_pinned(struct intel_context *ce)
61 __releases(ce->pin_mutex)
63 mutex_unlock(&ce->pin_mutex);
66 int __intel_context_do_pin(struct intel_context *ce);
68 static inline int intel_context_pin(struct intel_context *ce)
70 if (likely(atomic_inc_not_zero(&ce->pin_count)))
73 return __intel_context_do_pin(ce);
76 static inline void __intel_context_pin(struct intel_context *ce)
78 GEM_BUG_ON(!intel_context_is_pinned(ce));
79 atomic_inc(&ce->pin_count);
82 void intel_context_unpin(struct intel_context *ce);
84 void intel_context_enter_engine(struct intel_context *ce);
85 void intel_context_exit_engine(struct intel_context *ce);
87 static inline void intel_context_enter(struct intel_context *ce)
89 if (!ce->active_count++)
93 static inline void intel_context_mark_active(struct intel_context *ce)
98 static inline void intel_context_exit(struct intel_context *ce)
100 GEM_BUG_ON(!ce->active_count);
101 if (!--ce->active_count)
105 int intel_context_active_acquire(struct intel_context *ce, unsigned long flags);
106 void intel_context_active_release(struct intel_context *ce);
108 static inline struct intel_context *intel_context_get(struct intel_context *ce)
114 static inline void intel_context_put(struct intel_context *ce)
116 kref_put(&ce->ref, ce->ops->destroy);
119 static inline int __must_check
120 intel_context_timeline_lock(struct intel_context *ce)
121 __acquires(&ce->ring->timeline->mutex)
123 return mutex_lock_interruptible(&ce->ring->timeline->mutex);
126 static inline void intel_context_timeline_unlock(struct intel_context *ce)
127 __releases(&ce->ring->timeline->mutex)
129 mutex_unlock(&ce->ring->timeline->mutex);
132 struct i915_request *intel_context_create_request(struct intel_context *ce);
134 #endif /* __INTEL_CONTEXT_H__ */