]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/gpu/drm/i915/selftests/mock_context.c
drm/amd/display: Check hpd_gpio for NULL before accessing it
[thirdparty/kernel/stable.git] / drivers / gpu / drm / i915 / selftests / mock_context.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include "mock_context.h"
26 #include "mock_gtt.h"
27
28 struct i915_gem_context *
29 mock_context(struct drm_i915_private *i915,
30 const char *name)
31 {
32 struct i915_gem_context *ctx;
33 unsigned int n;
34 int ret;
35
36 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
37 if (!ctx)
38 return NULL;
39
40 kref_init(&ctx->ref);
41 INIT_LIST_HEAD(&ctx->link);
42 ctx->i915 = i915;
43
44 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
45 INIT_LIST_HEAD(&ctx->handles_list);
46 INIT_LIST_HEAD(&ctx->hw_id_link);
47
48 for (n = 0; n < ARRAY_SIZE(ctx->__engine); n++) {
49 struct intel_context *ce = &ctx->__engine[n];
50
51 ce->gem_context = ctx;
52 }
53
54 ret = i915_gem_context_pin_hw_id(ctx);
55 if (ret < 0)
56 goto err_handles;
57
58 if (name) {
59 ctx->name = kstrdup(name, GFP_KERNEL);
60 if (!ctx->name)
61 goto err_put;
62
63 ctx->ppgtt = mock_ppgtt(i915, name);
64 if (!ctx->ppgtt)
65 goto err_put;
66 }
67
68 return ctx;
69
70 err_handles:
71 kfree(ctx);
72 return NULL;
73
74 err_put:
75 i915_gem_context_set_closed(ctx);
76 i915_gem_context_put(ctx);
77 return NULL;
78 }
79
80 void mock_context_close(struct i915_gem_context *ctx)
81 {
82 context_close(ctx);
83 }
84
85 void mock_init_contexts(struct drm_i915_private *i915)
86 {
87 init_contexts(i915);
88 }
89
90 struct i915_gem_context *
91 live_context(struct drm_i915_private *i915, struct drm_file *file)
92 {
93 lockdep_assert_held(&i915->drm.struct_mutex);
94
95 return i915_gem_create_context(i915, file->driver_priv);
96 }
97
98 struct i915_gem_context *
99 kernel_context(struct drm_i915_private *i915)
100 {
101 return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL);
102 }
103
104 void kernel_context_close(struct i915_gem_context *ctx)
105 {
106 context_close(ctx);
107 }