mock_context.c 2.0 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_drm.h"
9
#include "selftests/mock_gtt.h"
10 11 12 13 14 15

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

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

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

26 27
	i915_gem_context_set_persistence(ctx);

28 29 30 31 32
	mutex_init(&ctx->engines_mutex);
	e = default_engines(ctx);
	if (IS_ERR(e))
		goto err_free;
	RCU_INIT_POINTER(ctx->engines, e);
33

34
	INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
35
	mutex_init(&ctx->mutex);
36

37
	if (name) {
38
		struct i915_ppgtt *ppgtt;
39

40
		strncpy(ctx->name, name, sizeof(ctx->name));
41

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

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

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

	return ctx;

55
err_free:
56 57 58 59 60 61 62 63 64 65 66
	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)
{
67
	context_close(ctx);
68
}
69 70 71

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

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

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

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

	return ctx;

err_ctx:
	context_close(ctx);
	return ERR_PTR(err);
94
}
95 96 97 98

struct i915_gem_context *
kernel_context(struct drm_i915_private *i915)
{
99 100 101 102 103 104 105 106 107 108
	struct i915_gem_context *ctx;

	ctx = i915_gem_create_context(i915, 0);
	if (IS_ERR(ctx))
		return ctx;

	i915_gem_context_clear_bannable(ctx);
	i915_gem_context_set_persistence(ctx);

	return ctx;
109 110 111 112 113 114
}

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