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

#include "i915_drv.h"
#include "i915_gem_context.h"
#include "i915_globals.h"
10

11
#include "intel_context.h"
12
#include "intel_engine.h"
13
#include "intel_engine_pm.h"
14 15 16 17 18 19

static struct i915_global_context {
	struct i915_global base;
	struct kmem_cache *slab_ce;
} global;

20
static struct intel_context *intel_context_alloc(void)
21 22 23 24 25 26 27 28 29 30
{
	return kmem_cache_zalloc(global.slab_ce, GFP_KERNEL);
}

void intel_context_free(struct intel_context *ce)
{
	kmem_cache_free(global.slab_ce, ce);
}

struct intel_context *
31
intel_context_create(struct i915_gem_context *ctx,
32 33
		     struct intel_engine_cs *engine)
{
34
	struct intel_context *ce;
35 36 37 38 39 40

	ce = intel_context_alloc();
	if (!ce)
		return ERR_PTR(-ENOMEM);

	intel_context_init(ce, ctx, engine);
41
	return ce;
42 43
}

44 45 46
int __intel_context_do_pin(struct intel_context *ce)
{
	int err;
47 48

	if (mutex_lock_interruptible(&ce->pin_mutex))
49
		return -EINTR;
50 51

	if (likely(!atomic_read(&ce->pin_count))) {
52 53 54 55 56
		intel_wakeref_t wakeref;

		err = 0;
		with_intel_runtime_pm(ce->engine->i915, wakeref)
			err = ce->ops->pin(ce);
57
		if (err)
58
			goto err;
59

60
		i915_gem_context_get(ce->gem_context); /* for ctx->ppgtt */
61

62
		intel_context_get(ce);
63
		smp_mb__before_atomic(); /* flush pin before it is visible */
64 65
	}

66 67 68 69
	atomic_inc(&ce->pin_count);
	GEM_BUG_ON(!intel_context_is_pinned(ce)); /* no overflow! */

	mutex_unlock(&ce->pin_mutex);
70
	return 0;
71

72 73
err:
	mutex_unlock(&ce->pin_mutex);
74
	return err;
75 76
}

77 78 79 80 81 82
void intel_context_unpin(struct intel_context *ce)
{
	if (likely(atomic_add_unless(&ce->pin_count, -1, 1)))
		return;

	/* We may be called from inside intel_context_pin() to evict another */
83
	intel_context_get(ce);
84 85
	mutex_lock_nested(&ce->pin_mutex, SINGLE_DEPTH_NESTING);

86
	if (likely(atomic_dec_and_test(&ce->pin_count))) {
87 88
		ce->ops->unpin(ce);

89
		i915_gem_context_put(ce->gem_context);
90
		intel_context_put(ce);
91 92
	}

93
	mutex_unlock(&ce->pin_mutex);
94
	intel_context_put(ce);
95 96
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110
static void intel_context_retire(struct i915_active_request *active,
				 struct i915_request *rq)
{
	struct intel_context *ce =
		container_of(active, typeof(*ce), active_tracker);

	intel_context_unpin(ce);
}

void
intel_context_init(struct intel_context *ce,
		   struct i915_gem_context *ctx,
		   struct intel_engine_cs *engine)
{
111 112
	GEM_BUG_ON(!engine->cops);

113 114
	kref_init(&ce->ref);

115 116 117
	ce->gem_context = ctx;
	ce->engine = engine;
	ce->ops = engine->cops;
118
	ce->sseu = engine->sseu;
119
	ce->saturated = 0;
120 121 122 123

	INIT_LIST_HEAD(&ce->signal_link);
	INIT_LIST_HEAD(&ce->signals);

124 125
	mutex_init(&ce->pin_mutex);

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	i915_active_request_init(&ce->active_tracker,
				 NULL, intel_context_retire);
}

static void i915_global_context_shrink(void)
{
	kmem_cache_shrink(global.slab_ce);
}

static void i915_global_context_exit(void)
{
	kmem_cache_destroy(global.slab_ce);
}

static struct i915_global_context global = { {
	.shrink = i915_global_context_shrink,
	.exit = i915_global_context_exit,
} };

int __init i915_global_context_init(void)
{
	global.slab_ce = KMEM_CACHE(intel_context, SLAB_HWCACHE_ALIGN);
	if (!global.slab_ce)
		return -ENOMEM;

	i915_global_register(&global.base);
	return 0;
}
154 155 156

void intel_context_enter_engine(struct intel_context *ce)
{
157
	intel_engine_pm_get(ce->engine);
158 159 160 161
}

void intel_context_exit_engine(struct intel_context *ce)
{
162
	ce->saturated = 0;
163
	intel_engine_pm_put(ce->engine);
164
}
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

struct i915_request *intel_context_create_request(struct intel_context *ce)
{
	struct i915_request *rq;
	int err;

	err = intel_context_pin(ce);
	if (unlikely(err))
		return ERR_PTR(err);

	rq = i915_request_create(ce);
	intel_context_unpin(ce);

	return rq;
}