2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016 Intel Corporation
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
,
15 struct i915_gem_context
*ctx
;
16 struct i915_gem_engines
*e
;
18 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
23 INIT_LIST_HEAD(&ctx
->link
);
26 mutex_init(&ctx
->mutex
);
28 spin_lock_init(&ctx
->stale
.lock
);
29 INIT_LIST_HEAD(&ctx
->stale
.engines
);
31 i915_gem_context_set_persistence(ctx
);
33 mutex_init(&ctx
->engines_mutex
);
34 e
= default_engines(ctx
);
37 RCU_INIT_POINTER(ctx
->engines
, e
);
39 INIT_RADIX_TREE(&ctx
->handles_vma
, GFP_KERNEL
);
40 mutex_init(&ctx
->lut_mutex
);
43 struct i915_ppgtt
*ppgtt
;
45 strncpy(ctx
->name
, name
, sizeof(ctx
->name
) - 1);
47 ppgtt
= mock_ppgtt(i915
, name
);
51 mutex_lock(&ctx
->mutex
);
52 __set_ppgtt(ctx
, &ppgtt
->vm
);
53 mutex_unlock(&ctx
->mutex
);
55 i915_vm_put(&ppgtt
->vm
);
65 i915_gem_context_set_closed(ctx
);
66 i915_gem_context_put(ctx
);
70 void mock_context_close(struct i915_gem_context
*ctx
)
75 void mock_init_contexts(struct drm_i915_private
*i915
)
77 init_contexts(&i915
->gem
.contexts
);
80 struct i915_gem_context
*
81 live_context(struct drm_i915_private
*i915
, struct file
*file
)
83 struct i915_gem_context
*ctx
;
87 ctx
= i915_gem_create_context(i915
, 0);
91 i915_gem_context_set_no_error_capture(ctx
);
93 err
= gem_context_register(ctx
, to_drm_file(file
)->driver_priv
, &id
);
104 struct i915_gem_context
*
105 live_context_for_engine(struct intel_engine_cs
*engine
, struct file
*file
)
107 struct i915_gem_engines
*engines
;
108 struct i915_gem_context
*ctx
;
109 struct intel_context
*ce
;
111 engines
= alloc_engines(1);
113 return ERR_PTR(-ENOMEM
);
115 ctx
= live_context(engine
->i915
, file
);
117 __free_engines(engines
, 0);
121 ce
= intel_context_create(engine
);
123 __free_engines(engines
, 0);
127 intel_context_set_gem(ce
, ctx
);
128 engines
->engines
[0] = ce
;
129 engines
->num_engines
= 1;
131 mutex_lock(&ctx
->engines_mutex
);
132 i915_gem_context_set_user_engines(ctx
);
133 engines
= rcu_replace_pointer(ctx
->engines
, engines
, 1);
134 mutex_unlock(&ctx
->engines_mutex
);
136 engines_idle_release(ctx
, engines
);
141 struct i915_gem_context
*
142 kernel_context(struct drm_i915_private
*i915
)
144 struct i915_gem_context
*ctx
;
146 ctx
= i915_gem_create_context(i915
, 0);
150 i915_gem_context_clear_bannable(ctx
);
151 i915_gem_context_set_persistence(ctx
);
152 i915_gem_context_set_no_error_capture(ctx
);
157 void kernel_context_close(struct i915_gem_context
*ctx
)