treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / gem / selftests / mock_context.c
blob384143aa77769abe7513494b3b08c93b92c3f13a
1 /*
2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016 Intel Corporation
5 */
7 #include "mock_context.h"
8 #include "selftests/mock_drm.h"
9 #include "selftests/mock_gtt.h"
11 struct i915_gem_context *
12 mock_context(struct drm_i915_private *i915,
13 const char *name)
15 struct i915_gem_context *ctx;
16 struct i915_gem_engines *e;
18 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
19 if (!ctx)
20 return NULL;
22 kref_init(&ctx->ref);
23 INIT_LIST_HEAD(&ctx->link);
24 ctx->i915 = i915;
26 i915_gem_context_set_persistence(ctx);
28 mutex_init(&ctx->engines_mutex);
29 e = default_engines(ctx);
30 if (IS_ERR(e))
31 goto err_free;
32 RCU_INIT_POINTER(ctx->engines, e);
34 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
35 mutex_init(&ctx->mutex);
37 if (name) {
38 struct i915_ppgtt *ppgtt;
40 strncpy(ctx->name, name, sizeof(ctx->name));
42 ppgtt = mock_ppgtt(i915, name);
43 if (!ppgtt)
44 goto err_put;
46 mutex_lock(&ctx->mutex);
47 __set_ppgtt(ctx, &ppgtt->vm);
48 mutex_unlock(&ctx->mutex);
50 i915_vm_put(&ppgtt->vm);
53 return ctx;
55 err_free:
56 kfree(ctx);
57 return NULL;
59 err_put:
60 i915_gem_context_set_closed(ctx);
61 i915_gem_context_put(ctx);
62 return NULL;
65 void mock_context_close(struct i915_gem_context *ctx)
67 context_close(ctx);
70 void mock_init_contexts(struct drm_i915_private *i915)
72 init_contexts(&i915->gem.contexts);
75 struct i915_gem_context *
76 live_context(struct drm_i915_private *i915, struct file *file)
78 struct i915_gem_context *ctx;
79 int err;
80 u32 id;
82 ctx = i915_gem_create_context(i915, 0);
83 if (IS_ERR(ctx))
84 return ctx;
86 err = gem_context_register(ctx, to_drm_file(file)->driver_priv, &id);
87 if (err < 0)
88 goto err_ctx;
90 return ctx;
92 err_ctx:
93 context_close(ctx);
94 return ERR_PTR(err);
97 struct i915_gem_context *
98 kernel_context(struct drm_i915_private *i915)
100 struct i915_gem_context *ctx;
102 ctx = i915_gem_create_context(i915, 0);
103 if (IS_ERR(ctx))
104 return ctx;
106 i915_gem_context_clear_bannable(ctx);
107 i915_gem_context_set_persistence(ctx);
109 return ctx;
112 void kernel_context_close(struct i915_gem_context *ctx)
114 context_close(ctx);