mock_context.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "mock_context.h"
  25. #include "mock_gtt.h"
  26. struct i915_gem_context *
  27. mock_context(struct drm_i915_private *i915,
  28. const char *name)
  29. {
  30. struct i915_gem_context *ctx;
  31. unsigned int n;
  32. int ret;
  33. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  34. if (!ctx)
  35. return NULL;
  36. kref_init(&ctx->ref);
  37. INIT_LIST_HEAD(&ctx->link);
  38. ctx->i915 = i915;
  39. INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
  40. INIT_LIST_HEAD(&ctx->handles_list);
  41. INIT_LIST_HEAD(&ctx->hw_id_link);
  42. for (n = 0; n < ARRAY_SIZE(ctx->__engine); n++) {
  43. struct intel_context *ce = &ctx->__engine[n];
  44. ce->gem_context = ctx;
  45. }
  46. ret = i915_gem_context_pin_hw_id(ctx);
  47. if (ret < 0)
  48. goto err_handles;
  49. if (name) {
  50. ctx->name = kstrdup(name, GFP_KERNEL);
  51. if (!ctx->name)
  52. goto err_put;
  53. ctx->ppgtt = mock_ppgtt(i915, name);
  54. if (!ctx->ppgtt)
  55. goto err_put;
  56. }
  57. return ctx;
  58. err_handles:
  59. kfree(ctx);
  60. return NULL;
  61. err_put:
  62. i915_gem_context_set_closed(ctx);
  63. i915_gem_context_put(ctx);
  64. return NULL;
  65. }
  66. void mock_context_close(struct i915_gem_context *ctx)
  67. {
  68. context_close(ctx);
  69. }
  70. void mock_init_contexts(struct drm_i915_private *i915)
  71. {
  72. init_contexts(i915);
  73. }
  74. struct i915_gem_context *
  75. live_context(struct drm_i915_private *i915, struct drm_file *file)
  76. {
  77. lockdep_assert_held(&i915->drm.struct_mutex);
  78. return i915_gem_create_context(i915, file->driver_priv);
  79. }
  80. struct i915_gem_context *
  81. kernel_context(struct drm_i915_private *i915)
  82. {
  83. return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL);
  84. }
  85. void kernel_context_close(struct i915_gem_context *ctx)
  86. {
  87. context_close(ctx);
  88. }