intel_context.h 3.6 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 11
#include <linux/lockdep.h>

12
#include "i915_active.h"
13 14 15 16 17 18
#include "intel_context_types.h"
#include "intel_engine_types.h"

void intel_context_init(struct intel_context *ce,
			struct i915_gem_context *ctx,
			struct intel_engine_cs *engine);
19
void intel_context_fini(struct intel_context *ce);
20

21
struct intel_context *
22
intel_context_create(struct i915_gem_context *ctx,
23 24
		     struct intel_engine_cs *engine);

25 26
void intel_context_free(struct intel_context *ce);

27
/**
28 29
 * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
 * @ce - the context
30
 *
31 32 33
 * 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.
34
 */
35 36 37 38 39
static inline int intel_context_lock_pinned(struct intel_context *ce)
	__acquires(ce->pin_mutex)
{
	return mutex_lock_interruptible(&ce->pin_mutex);
}
40

41 42 43 44 45 46 47 48 49
/**
 * 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.
 */
50 51 52 53 54 55
static inline bool
intel_context_is_pinned(struct intel_context *ce)
{
	return atomic_read(&ce->pin_count);
}

56 57 58 59 60 61 62 63 64 65 66
/**
 * 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);
}
67

68 69 70 71 72 73 74 75 76
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);
}
77 78 79

static inline void __intel_context_pin(struct intel_context *ce)
{
80 81
	GEM_BUG_ON(!intel_context_is_pinned(ce));
	atomic_inc(&ce->pin_count);
82 83
}

84
void intel_context_unpin(struct intel_context *ce);
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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)
{
	if (!ce->active_count++)
		ce->ops->enter(ce);
}

static inline void intel_context_mark_active(struct intel_context *ce)
{
	++ce->active_count;
}

static inline void intel_context_exit(struct intel_context *ce)
{
	GEM_BUG_ON(!ce->active_count);
	if (!--ce->active_count)
		ce->ops->exit(ce);
}

107 108
int intel_context_active_acquire(struct intel_context *ce);
void intel_context_active_release(struct intel_context *ce);
109

110 111 112 113 114 115 116 117 118 119 120
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);
}

121 122
static inline int __must_check
intel_context_timeline_lock(struct intel_context *ce)
123 124
	__acquires(&ce->ring->timeline->mutex)
{
125
	return mutex_lock_interruptible(&ce->ring->timeline->mutex);
126 127 128 129 130 131 132 133
}

static inline void intel_context_timeline_unlock(struct intel_context *ce)
	__releases(&ce->ring->timeline->mutex)
{
	mutex_unlock(&ce->ring->timeline->mutex);
}

134 135 136
int intel_context_prepare_remote_request(struct intel_context *ce,
					 struct i915_request *rq);

137 138
struct i915_request *intel_context_create_request(struct intel_context *ce);

139 140 141 142 143
static inline struct intel_ring *__intel_context_ring_size(u64 sz)
{
	return u64_to_ptr(struct intel_ring, sz);
}

144
#endif /* __INTEL_CONTEXT_H__ */