1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "mock_context.h"
26#include "mock_gtt.h"
27
28struct i915_gem_context *
29mock_context(struct drm_i915_private *i915,
30 const char *name)
31{
32 struct i915_gem_context *ctx;
33 int ret;
34
35 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
36 if (!ctx)
37 return NULL;
38
39 kref_init(&ctx->ref);
40 INIT_LIST_HEAD(&ctx->link);
41 ctx->i915 = i915;
42
43 ctx->hw_contexts = RB_ROOT;
44 spin_lock_init(&ctx->hw_contexts_lock);
45
46 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
47 INIT_LIST_HEAD(&ctx->handles_list);
48 INIT_LIST_HEAD(&ctx->hw_id_link);
49 INIT_LIST_HEAD(&ctx->active_engines);
50 mutex_init(&ctx->mutex);
51
52 ret = i915_gem_context_pin_hw_id(ctx);
53 if (ret < 0)
54 goto err_handles;
55
56 if (name) {
57 struct i915_hw_ppgtt *ppgtt;
58
59 ctx->name = kstrdup(name, GFP_KERNEL);
60 if (!ctx->name)
61 goto err_put;
62
63 ppgtt = mock_ppgtt(i915, name);
64 if (!ppgtt)
65 goto err_put;
66
67 __set_ppgtt(ctx, ppgtt);
68 }
69
70 return ctx;
71
72err_handles:
73 kfree(ctx);
74 return NULL;
75
76err_put:
77 i915_gem_context_set_closed(ctx);
78 i915_gem_context_put(ctx);
79 return NULL;
80}
81
82void mock_context_close(struct i915_gem_context *ctx)
83{
84 context_close(ctx);
85}
86
87void mock_init_contexts(struct drm_i915_private *i915)
88{
89 init_contexts(i915);
90}
91
92struct i915_gem_context *
93live_context(struct drm_i915_private *i915, struct drm_file *file)
94{
95 struct i915_gem_context *ctx;
96 int err;
97
98 lockdep_assert_held(&i915->drm.struct_mutex);
99
100 ctx = i915_gem_create_context(i915, 0);
101 if (IS_ERR(ctx))
102 return ctx;
103
104 err = gem_context_register(ctx, file->driver_priv);
105 if (err < 0)
106 goto err_ctx;
107
108 return ctx;
109
110err_ctx:
111 context_close(ctx);
112 return ERR_PTR(err);
113}
114
115struct i915_gem_context *
116kernel_context(struct drm_i915_private *i915)
117{
118 return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL);
119}
120
121void kernel_context_close(struct i915_gem_context *ctx)
122{
123 context_close(ctx);
124}
125