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 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
47 for (n = 0; n < ARRAY_SIZE(ctx->__engine); n++) {
48 struct intel_context *ce = &ctx->__engine[n];
49
50 ce->gem_context = ctx;
51 }
52
53 ret = ida_simple_get(&i915->contexts.hw_ida,
54 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
55 if (ret < 0)
56 goto err_handles;
57 ctx->hw_id = ret;
58
59 if (name) {
60 ctx->name = kstrdup(name, GFP_KERNEL);
61 if (!ctx->name)
62 goto err_put;
63
64 ctx->ppgtt = mock_ppgtt(i915, name);
65 if (!ctx->ppgtt)
66 goto err_put;
67 }
68
69 return ctx;
70
71err_handles:
72 kfree(ctx);
73 return NULL;
74
75err_put:
76 i915_gem_context_set_closed(ctx);
77 i915_gem_context_put(ctx);
78 return NULL;
79}
80
81void mock_context_close(struct i915_gem_context *ctx)
82{
83 context_close(ctx);
84}
85
86void mock_init_contexts(struct drm_i915_private *i915)
87{
88 INIT_LIST_HEAD(&i915->contexts.list);
89 ida_init(&i915->contexts.hw_ida);
90
91 INIT_WORK(&i915->contexts.free_work, contexts_free_worker);
92 init_llist_head(&i915->contexts.free_list);
93}
94
95struct i915_gem_context *
96live_context(struct drm_i915_private *i915, struct drm_file *file)
97{
98 lockdep_assert_held(&i915->drm.struct_mutex);
99
100 return i915_gem_create_context(i915, file->driver_priv);
101}
102
103struct i915_gem_context *
104kernel_context(struct drm_i915_private *i915)
105{
106 return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL);
107}
108
109void kernel_context_close(struct i915_gem_context *ctx)
110{
111 context_close(ctx);
112}
113