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