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