1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2016 Intel Corporation 5 */ 6 7 #include "mock_context.h" 8 #include "selftests/mock_drm.h" 9 #include "selftests/mock_gtt.h" 10 11 struct i915_gem_context * 12 mock_context(struct drm_i915_private *i915, 13 const char *name) 14 { 15 struct i915_gem_context *ctx; 16 struct i915_gem_engines *e; 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 i915_gem_context_set_persistence(ctx); 27 28 mutex_init(&ctx->engines_mutex); 29 e = default_engines(ctx); 30 if (IS_ERR(e)) 31 goto err_free; 32 RCU_INIT_POINTER(ctx->engines, e); 33 34 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL); 35 mutex_init(&ctx->mutex); 36 37 if (name) { 38 struct i915_ppgtt *ppgtt; 39 40 strncpy(ctx->name, name, sizeof(ctx->name)); 41 42 ppgtt = mock_ppgtt(i915, name); 43 if (!ppgtt) 44 goto err_put; 45 46 mutex_lock(&ctx->mutex); 47 __set_ppgtt(ctx, &ppgtt->vm); 48 mutex_unlock(&ctx->mutex); 49 50 i915_vm_put(&ppgtt->vm); 51 } 52 53 return ctx; 54 55 err_free: 56 kfree(ctx); 57 return NULL; 58 59 err_put: 60 i915_gem_context_set_closed(ctx); 61 i915_gem_context_put(ctx); 62 return NULL; 63 } 64 65 void mock_context_close(struct i915_gem_context *ctx) 66 { 67 context_close(ctx); 68 } 69 70 void mock_init_contexts(struct drm_i915_private *i915) 71 { 72 init_contexts(&i915->gem.contexts); 73 } 74 75 struct i915_gem_context * 76 live_context(struct drm_i915_private *i915, struct file *file) 77 { 78 struct i915_gem_context *ctx; 79 int err; 80 u32 id; 81 82 ctx = i915_gem_create_context(i915, 0); 83 if (IS_ERR(ctx)) 84 return ctx; 85 86 err = gem_context_register(ctx, to_drm_file(file)->driver_priv, &id); 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 struct i915_gem_context *ctx; 101 102 ctx = i915_gem_create_context(i915, 0); 103 if (IS_ERR(ctx)) 104 return ctx; 105 106 i915_gem_context_clear_bannable(ctx); 107 i915_gem_context_set_persistence(ctx); 108 109 return ctx; 110 } 111 112 void kernel_context_close(struct i915_gem_context *ctx) 113 { 114 context_close(ctx); 115 } 116