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 spin_lock_init(&ctx->stale.lock); 27 INIT_LIST_HEAD(&ctx->stale.engines); 28 29 i915_gem_context_set_persistence(ctx); 30 31 mutex_init(&ctx->engines_mutex); 32 e = default_engines(ctx); 33 if (IS_ERR(e)) 34 goto err_free; 35 RCU_INIT_POINTER(ctx->engines, e); 36 37 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL); 38 mutex_init(&ctx->mutex); 39 40 if (name) { 41 struct i915_ppgtt *ppgtt; 42 43 strncpy(ctx->name, name, sizeof(ctx->name) - 1); 44 45 ppgtt = mock_ppgtt(i915, name); 46 if (!ppgtt) 47 goto err_put; 48 49 mutex_lock(&ctx->mutex); 50 __set_ppgtt(ctx, &ppgtt->vm); 51 mutex_unlock(&ctx->mutex); 52 53 i915_vm_put(&ppgtt->vm); 54 } 55 56 return ctx; 57 58 err_free: 59 kfree(ctx); 60 return NULL; 61 62 err_put: 63 i915_gem_context_set_closed(ctx); 64 i915_gem_context_put(ctx); 65 return NULL; 66 } 67 68 void mock_context_close(struct i915_gem_context *ctx) 69 { 70 context_close(ctx); 71 } 72 73 void mock_init_contexts(struct drm_i915_private *i915) 74 { 75 init_contexts(&i915->gem.contexts); 76 } 77 78 struct i915_gem_context * 79 live_context(struct drm_i915_private *i915, struct file *file) 80 { 81 struct i915_gem_context *ctx; 82 int err; 83 u32 id; 84 85 ctx = i915_gem_create_context(i915, 0); 86 if (IS_ERR(ctx)) 87 return ctx; 88 89 i915_gem_context_set_no_error_capture(ctx); 90 91 err = gem_context_register(ctx, to_drm_file(file)->driver_priv, &id); 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 struct i915_gem_context *ctx; 106 107 ctx = i915_gem_create_context(i915, 0); 108 if (IS_ERR(ctx)) 109 return ctx; 110 111 i915_gem_context_clear_bannable(ctx); 112 i915_gem_context_set_persistence(ctx); 113 i915_gem_context_set_no_error_capture(ctx); 114 115 return ctx; 116 } 117 118 void kernel_context_close(struct i915_gem_context *ctx) 119 { 120 context_close(ctx); 121 } 122