mock_context.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SPDX-License-Identifier: MIT
  3. *
  4. * Copyright © 2016 Intel Corporation
  5. */
  6. #include "i915_file_private.h"
  7. #include "mock_context.h"
  8. #include "selftests/mock_drm.h"
  9. #include "selftests/mock_gtt.h"
  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. struct intel_sseu null_sseu = {};
  17. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  18. if (!ctx)
  19. return NULL;
  20. kref_init(&ctx->ref);
  21. INIT_LIST_HEAD(&ctx->link);
  22. ctx->i915 = i915;
  23. INIT_WORK(&ctx->release_work, i915_gem_context_release_work);
  24. mutex_init(&ctx->mutex);
  25. spin_lock_init(&ctx->stale.lock);
  26. INIT_LIST_HEAD(&ctx->stale.engines);
  27. i915_gem_context_set_persistence(ctx);
  28. if (name) {
  29. struct i915_ppgtt *ppgtt;
  30. strscpy(ctx->name, name, sizeof(ctx->name));
  31. ppgtt = mock_ppgtt(i915, name);
  32. if (!ppgtt)
  33. goto err_free;
  34. ctx->vm = &ppgtt->vm;
  35. }
  36. mutex_init(&ctx->engines_mutex);
  37. e = default_engines(ctx, null_sseu);
  38. if (IS_ERR(e))
  39. goto err_vm;
  40. RCU_INIT_POINTER(ctx->engines, e);
  41. INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
  42. mutex_init(&ctx->lut_mutex);
  43. return ctx;
  44. err_vm:
  45. if (ctx->vm)
  46. i915_vm_put(ctx->vm);
  47. err_free:
  48. kfree(ctx);
  49. return NULL;
  50. }
  51. void mock_context_close(struct i915_gem_context *ctx)
  52. {
  53. context_close(ctx);
  54. }
  55. void mock_init_contexts(struct drm_i915_private *i915)
  56. {
  57. init_contexts(&i915->gem.contexts);
  58. }
  59. struct i915_gem_context *
  60. live_context(struct drm_i915_private *i915, struct file *file)
  61. {
  62. struct drm_i915_file_private *fpriv = to_drm_file(file)->driver_priv;
  63. struct i915_gem_proto_context *pc;
  64. struct i915_gem_context *ctx;
  65. int err;
  66. u32 id;
  67. pc = proto_context_create(fpriv, i915, 0);
  68. if (IS_ERR(pc))
  69. return ERR_CAST(pc);
  70. ctx = i915_gem_create_context(i915, pc);
  71. proto_context_close(i915, pc);
  72. if (IS_ERR(ctx))
  73. return ctx;
  74. i915_gem_context_set_no_error_capture(ctx);
  75. err = xa_alloc(&fpriv->context_xa, &id, NULL, xa_limit_32b, GFP_KERNEL);
  76. if (err < 0)
  77. goto err_ctx;
  78. gem_context_register(ctx, fpriv, id);
  79. return ctx;
  80. err_ctx:
  81. context_close(ctx);
  82. return ERR_PTR(err);
  83. }
  84. struct i915_gem_context *
  85. live_context_for_engine(struct intel_engine_cs *engine, struct file *file)
  86. {
  87. struct i915_gem_engines *engines;
  88. struct i915_gem_context *ctx;
  89. struct intel_sseu null_sseu = {};
  90. struct intel_context *ce;
  91. engines = alloc_engines(1);
  92. if (!engines)
  93. return ERR_PTR(-ENOMEM);
  94. ctx = live_context(engine->i915, file);
  95. if (IS_ERR(ctx)) {
  96. __free_engines(engines, 0);
  97. return ctx;
  98. }
  99. ce = intel_context_create(engine);
  100. if (IS_ERR(ce)) {
  101. __free_engines(engines, 0);
  102. return ERR_CAST(ce);
  103. }
  104. intel_context_set_gem(ce, ctx, null_sseu);
  105. engines->engines[0] = ce;
  106. engines->num_engines = 1;
  107. mutex_lock(&ctx->engines_mutex);
  108. i915_gem_context_set_user_engines(ctx);
  109. engines = rcu_replace_pointer(ctx->engines, engines, 1);
  110. mutex_unlock(&ctx->engines_mutex);
  111. engines_idle_release(ctx, engines);
  112. return ctx;
  113. }
  114. struct i915_gem_context *
  115. kernel_context(struct drm_i915_private *i915,
  116. struct i915_address_space *vm)
  117. {
  118. struct i915_gem_context *ctx;
  119. struct i915_gem_proto_context *pc;
  120. pc = proto_context_create(NULL, i915, 0);
  121. if (IS_ERR(pc))
  122. return ERR_CAST(pc);
  123. if (vm) {
  124. if (pc->vm)
  125. i915_vm_put(pc->vm);
  126. pc->vm = i915_vm_get(vm);
  127. }
  128. ctx = i915_gem_create_context(i915, pc);
  129. proto_context_close(i915, pc);
  130. if (IS_ERR(ctx))
  131. return ctx;
  132. i915_gem_context_clear_bannable(ctx);
  133. i915_gem_context_set_persistence(ctx);
  134. i915_gem_context_set_no_error_capture(ctx);
  135. return ctx;
  136. }
  137. void kernel_context_close(struct i915_gem_context *ctx)
  138. {
  139. context_close(ctx);
  140. }