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 17 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 18 if (!ctx) 19 return NULL; 20 21 kref_init(&ctx->ref); 22 INIT_LIST_HEAD(&ctx->link); 23 ctx->i915 = i915; 24 25 mutex_init(&ctx->engines_mutex); 26 e = default_engines(ctx); 27 if (IS_ERR(e)) 28 goto err_free; 29 RCU_INIT_POINTER(ctx->engines, e); 30 31 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL); 32 mutex_init(&ctx->mutex); 33 34 if (name) { 35 struct i915_ppgtt *ppgtt; 36 37 ctx->name = kstrdup(name, GFP_KERNEL); 38 if (!ctx->name) 39 goto err_put; 40 41 ppgtt = mock_ppgtt(i915, name); 42 if (!ppgtt) 43 goto err_put; 44 45 mutex_lock(&ctx->mutex); 46 __set_ppgtt(ctx, &ppgtt->vm); 47 mutex_unlock(&ctx->mutex); 48 49 i915_vm_put(&ppgtt->vm); 50 } 51 52 return ctx; 53 54 err_free: 55 kfree(ctx); 56 return NULL; 57 58 err_put: 59 i915_gem_context_set_closed(ctx); 60 i915_gem_context_put(ctx); 61 return NULL; 62 } 63 64 void mock_context_close(struct i915_gem_context *ctx) 65 { 66 context_close(ctx); 67 } 68 69 void mock_init_contexts(struct drm_i915_private *i915) 70 { 71 init_contexts(&i915->gem.contexts); 72 } 73 74 struct i915_gem_context * 75 live_context(struct drm_i915_private *i915, struct drm_file *file) 76 { 77 struct i915_gem_context *ctx; 78 int err; 79 80 ctx = i915_gem_create_context(i915, 0); 81 if (IS_ERR(ctx)) 82 return ctx; 83 84 err = gem_context_register(ctx, file->driver_priv); 85 if (err < 0) 86 goto err_ctx; 87 88 return ctx; 89 90 err_ctx: 91 context_close(ctx); 92 return ERR_PTR(err); 93 } 94 95 struct i915_gem_context * 96 kernel_context(struct drm_i915_private *i915) 97 { 98 return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL); 99 } 100 101 void kernel_context_close(struct i915_gem_context *ctx) 102 { 103 context_close(ctx); 104 } 105