drm/i915: Introduce context->enter() and context->exit()
[linux-2.6-block.git] / drivers / gpu / drm / i915 / gt / intel_context.h
CommitLineData
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
08819549
CW
10#include <linux/lockdep.h>
11
39e2f501
CW
12#include "intel_context_types.h"
13#include "intel_engine_types.h"
14
c4d52feb
CW
15struct intel_context *intel_context_alloc(void);
16void intel_context_free(struct intel_context *ce);
17
39e2f501
CW
18void intel_context_init(struct intel_context *ce,
19 struct i915_gem_context *ctx,
20 struct intel_engine_cs *engine);
21
c4d52feb
CW
22/**
23 * intel_context_lookup - Find the matching HW context for this (ctx, engine)
24 * @ctx - the parent GEM context
25 * @engine - the target HW engine
26 *
27 * May return NULL if the HW context hasn't been instantiated (i.e. unused).
28 */
29struct intel_context *
30intel_context_lookup(struct i915_gem_context *ctx,
31 struct intel_engine_cs *engine);
32
33/**
08819549 34 * intel_context_pin_lock - Stablises the 'pinned' status of the HW context
c4d52feb
CW
35 * @ctx - the parent GEM context
36 * @engine - the target HW engine
37 *
08819549
CW
38 * Acquire a lock on the pinned status of the HW context, such that the context
39 * can neither be bound to the GPU or unbound whilst the lock is held, i.e.
40 * intel_context_is_pinned() remains stable.
c4d52feb
CW
41 */
42struct intel_context *
08819549 43intel_context_pin_lock(struct i915_gem_context *ctx,
c4d52feb
CW
44 struct intel_engine_cs *engine);
45
08819549
CW
46static inline bool
47intel_context_is_pinned(struct intel_context *ce)
48{
49 return atomic_read(&ce->pin_count);
50}
51
52static inline void intel_context_pin_unlock(struct intel_context *ce)
53__releases(ce->pin_mutex)
54{
55 mutex_unlock(&ce->pin_mutex);
56}
57
c4d52feb
CW
58struct intel_context *
59__intel_context_insert(struct i915_gem_context *ctx,
60 struct intel_engine_cs *engine,
61 struct intel_context *ce);
62void
63__intel_context_remove(struct intel_context *ce);
39e2f501 64
95f697eb
CW
65struct intel_context *
66intel_context_pin(struct i915_gem_context *ctx, struct intel_engine_cs *engine);
39e2f501
CW
67
68static inline void __intel_context_pin(struct intel_context *ce)
69{
08819549
CW
70 GEM_BUG_ON(!intel_context_is_pinned(ce));
71 atomic_inc(&ce->pin_count);
39e2f501
CW
72}
73
08819549 74void intel_context_unpin(struct intel_context *ce);
39e2f501 75
6eee33e8
CW
76void intel_context_enter_engine(struct intel_context *ce);
77void intel_context_exit_engine(struct intel_context *ce);
78
79static inline void intel_context_enter(struct intel_context *ce)
80{
81 if (!ce->active_count++)
82 ce->ops->enter(ce);
83}
84
85static inline void intel_context_mark_active(struct intel_context *ce)
86{
87 ++ce->active_count;
88}
89
90static inline void intel_context_exit(struct intel_context *ce)
91{
92 GEM_BUG_ON(!ce->active_count);
93 if (!--ce->active_count)
94 ce->ops->exit(ce);
95}
96
4c5896dc
CW
97static inline struct intel_context *intel_context_get(struct intel_context *ce)
98{
99 kref_get(&ce->ref);
100 return ce;
101}
102
103static inline void intel_context_put(struct intel_context *ce)
104{
105 kref_put(&ce->ref, ce->ops->destroy);
106}
107
39e2f501 108#endif /* __INTEL_CONTEXT_H__ */