mock_context.c 1.8 KB
Newer Older
1
/*
2
 * SPDX-License-Identifier: MIT
3
 *
4
 * Copyright © 2016 Intel Corporation
5 6 7
 */

#include "mock_context.h"
8
#include "selftests/mock_gtt.h"
9 10 11 12 13 14

struct i915_gem_context *
mock_context(struct drm_i915_private *i915,
	     const char *name)
{
	struct i915_gem_context *ctx;
15
	struct i915_gem_engines *e;
16 17 18 19 20 21 22 23 24

	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return NULL;

	kref_init(&ctx->ref);
	INIT_LIST_HEAD(&ctx->link);
	ctx->i915 = i915;

25 26 27 28 29
	mutex_init(&ctx->engines_mutex);
	e = default_engines(ctx);
	if (IS_ERR(e))
		goto err_free;
	RCU_INIT_POINTER(ctx->engines, e);
30

31
	INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
32
	mutex_init(&ctx->mutex);
33

34
	if (name) {
35
		struct i915_ppgtt *ppgtt;
36

37 38 39 40
		ctx->name = kstrdup(name, GFP_KERNEL);
		if (!ctx->name)
			goto err_put;

41 42
		ppgtt = mock_ppgtt(i915, name);
		if (!ppgtt)
43
			goto err_put;
44

45
		mutex_lock(&ctx->mutex);
46
		__set_ppgtt(ctx, &ppgtt->vm);
47 48
		mutex_unlock(&ctx->mutex);

49
		i915_vm_put(&ppgtt->vm);
50 51 52 53
	}

	return ctx;

54
err_free:
55 56 57 58 59 60 61 62 63 64 65
	kfree(ctx);
	return NULL;

err_put:
	i915_gem_context_set_closed(ctx);
	i915_gem_context_put(ctx);
	return NULL;
}

void mock_context_close(struct i915_gem_context *ctx)
{
66
	context_close(ctx);
67
}
68 69 70

void mock_init_contexts(struct drm_i915_private *i915)
{
71
	init_contexts(&i915->gem.contexts);
72
}
73 74 75 76

struct i915_gem_context *
live_context(struct drm_i915_private *i915, struct drm_file *file)
{
77 78 79
	struct i915_gem_context *ctx;
	int err;

80
	ctx = i915_gem_create_context(i915, 0);
81 82 83 84
	if (IS_ERR(ctx))
		return ctx;

	err = gem_context_register(ctx, file->driver_priv);
85
	if (err < 0)
86 87 88 89 90 91 92
		goto err_ctx;

	return ctx;

err_ctx:
	context_close(ctx);
	return ERR_PTR(err);
93
}
94 95 96 97 98 99 100 101 102 103 104

struct i915_gem_context *
kernel_context(struct drm_i915_private *i915)
{
	return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL);
}

void kernel_context_close(struct i915_gem_context *ctx)
{
	context_close(ctx);
}