intel_context.h 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright © 2019 Intel Corporation
 */

#ifndef __INTEL_CONTEXT_H__
#define __INTEL_CONTEXT_H__

10
#include <linux/bitops.h>
11
#include <linux/lockdep.h>
12
#include <linux/types.h>
13

14
#include "i915_active.h"
15 16
#include "intel_context_types.h"
#include "intel_engine_types.h"
17
#include "intel_ring_types.h"
18
#include "intel_timeline_types.h"
19

20 21 22 23 24 25 26
#define CE_TRACE(ce, fmt, ...) do {					\
	const struct intel_context *ce__ = (ce);			\
	ENGINE_TRACE(ce__->engine, "context:%llx" fmt,			\
		     ce__->timeline->fence_context,			\
		     ##__VA_ARGS__);					\
} while (0)

27 28 29
void intel_context_init(struct intel_context *ce,
			struct i915_gem_context *ctx,
			struct intel_engine_cs *engine);
30
void intel_context_fini(struct intel_context *ce);
31

32
struct intel_context *
33
intel_context_create(struct i915_gem_context *ctx,
34 35
		     struct intel_engine_cs *engine);

36 37
void intel_context_free(struct intel_context *ce);

38
/**
39 40
 * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
 * @ce - the context
41
 *
42 43 44
 * Acquire a lock on the pinned status of the HW context, such that the context
 * can neither be bound to the GPU or unbound whilst the lock is held, i.e.
 * intel_context_is_pinned() remains stable.
45
 */
46 47 48 49 50
static inline int intel_context_lock_pinned(struct intel_context *ce)
	__acquires(ce->pin_mutex)
{
	return mutex_lock_interruptible(&ce->pin_mutex);
}
51

52 53 54 55 56 57 58 59 60
/**
 * intel_context_is_pinned - Reports the 'pinned' status
 * @ce - the context
 *
 * While in use by the GPU, the context, along with its ring and page
 * tables is pinned into memory and the GTT.
 *
 * Returns: true if the context is currently pinned for use by the GPU.
 */
61 62 63 64 65 66
static inline bool
intel_context_is_pinned(struct intel_context *ce)
{
	return atomic_read(&ce->pin_count);
}

67 68 69 70 71 72 73 74 75 76 77
/**
 * intel_context_unlock_pinned - Releases the earlier locking of 'pinned' status
 * @ce - the context
 *
 * Releases the lock earlier acquired by intel_context_unlock_pinned().
 */
static inline void intel_context_unlock_pinned(struct intel_context *ce)
	__releases(ce->pin_mutex)
{
	mutex_unlock(&ce->pin_mutex);
}
78

79 80 81 82 83 84 85 86 87
int __intel_context_do_pin(struct intel_context *ce);

static inline int intel_context_pin(struct intel_context *ce)
{
	if (likely(atomic_inc_not_zero(&ce->pin_count)))
		return 0;

	return __intel_context_do_pin(ce);
}
88 89 90

static inline void __intel_context_pin(struct intel_context *ce)
{
91 92
	GEM_BUG_ON(!intel_context_is_pinned(ce));
	atomic_inc(&ce->pin_count);
93 94
}

95
void intel_context_unpin(struct intel_context *ce);
96

97 98 99 100 101
void intel_context_enter_engine(struct intel_context *ce);
void intel_context_exit_engine(struct intel_context *ce);

static inline void intel_context_enter(struct intel_context *ce)
{
102
	lockdep_assert_held(&ce->timeline->mutex);
103 104 105 106 107 108
	if (!ce->active_count++)
		ce->ops->enter(ce);
}

static inline void intel_context_mark_active(struct intel_context *ce)
{
109
	lockdep_assert_held(&ce->timeline->mutex);
110 111 112 113 114
	++ce->active_count;
}

static inline void intel_context_exit(struct intel_context *ce)
{
115
	lockdep_assert_held(&ce->timeline->mutex);
116 117 118 119 120
	GEM_BUG_ON(!ce->active_count);
	if (!--ce->active_count)
		ce->ops->exit(ce);
}

121 122
int intel_context_active_acquire(struct intel_context *ce);
void intel_context_active_release(struct intel_context *ce);
123

124 125 126 127 128 129 130 131 132 133 134
static inline struct intel_context *intel_context_get(struct intel_context *ce)
{
	kref_get(&ce->ref);
	return ce;
}

static inline void intel_context_put(struct intel_context *ce)
{
	kref_put(&ce->ref, ce->ops->destroy);
}

135
static inline struct intel_timeline *__must_check
136
intel_context_timeline_lock(struct intel_context *ce)
137
	__acquires(&ce->timeline->mutex)
138
{
139 140 141 142 143 144 145 146
	struct intel_timeline *tl = ce->timeline;
	int err;

	err = mutex_lock_interruptible(&tl->mutex);
	if (err)
		return ERR_PTR(err);

	return tl;
147 148
}

149 150
static inline void intel_context_timeline_unlock(struct intel_timeline *tl)
	__releases(&tl->mutex)
151
{
152
	mutex_unlock(&tl->mutex);
153 154
}

155 156 157
int intel_context_prepare_remote_request(struct intel_context *ce,
					 struct i915_request *rq);

158 159
struct i915_request *intel_context_create_request(struct intel_context *ce);

160 161 162 163 164
static inline struct intel_ring *__intel_context_ring_size(u64 sz)
{
	return u64_to_ptr(struct intel_ring, sz);
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static inline bool intel_context_use_semaphores(const struct intel_context *ce)
{
	return test_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
}

static inline void intel_context_set_use_semaphores(struct intel_context *ce)
{
	set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
}

static inline void intel_context_clear_use_semaphores(struct intel_context *ce)
{
	clear_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
static inline bool intel_context_is_banned(const struct intel_context *ce)
{
	return test_bit(CONTEXT_BANNED, &ce->flags);
}

static inline bool intel_context_set_banned(struct intel_context *ce)
{
	return test_and_set_bit(CONTEXT_BANNED, &ce->flags);
}

static inline bool
intel_context_force_single_submission(const struct intel_context *ce)
{
	return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags);
}

static inline void
intel_context_set_single_submission(struct intel_context *ce)
{
	__set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags);
}

static inline bool
intel_context_nopreempt(const struct intel_context *ce)
{
	return test_bit(CONTEXT_NOPREEMPT, &ce->flags);
}

static inline void
intel_context_set_nopreempt(struct intel_context *ce)
{
	set_bit(CONTEXT_NOPREEMPT, &ce->flags);
}

static inline void
intel_context_clear_nopreempt(struct intel_context *ce)
{
	clear_bit(CONTEXT_NOPREEMPT, &ce->flags);
}

220
#endif /* __INTEL_CONTEXT_H__ */