mock_context.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. int ret;
  32. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  33. if (!ctx)
  34. return NULL;
  35. kref_init(&ctx->ref);
  36. INIT_LIST_HEAD(&ctx->link);
  37. ctx->i915 = i915;
  38. INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
  39. INIT_LIST_HEAD(&ctx->handles_list);
  40. ret = ida_simple_get(&i915->contexts.hw_ida,
  41. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  42. if (ret < 0)
  43. goto err_handles;
  44. ctx->hw_id = ret;
  45. if (name) {
  46. ctx->name = kstrdup(name, GFP_KERNEL);
  47. if (!ctx->name)
  48. goto err_put;
  49. ctx->ppgtt = mock_ppgtt(i915, name);
  50. if (!ctx->ppgtt)
  51. goto err_put;
  52. }
  53. return ctx;
  54. err_handles:
  55. kfree(ctx);
  56. return NULL;
  57. err_put:
  58. i915_gem_context_set_closed(ctx);
  59. i915_gem_context_put(ctx);
  60. return NULL;
  61. }
  62. void mock_context_close(struct i915_gem_context *ctx)
  63. {
  64. context_close(ctx);
  65. }
  66. void mock_init_contexts(struct drm_i915_private *i915)
  67. {
  68. INIT_LIST_HEAD(&i915->contexts.list);
  69. ida_init(&i915->contexts.hw_ida);
  70. INIT_WORK(&i915->contexts.free_work, contexts_free_worker);
  71. init_llist_head(&i915->contexts.free_list);
  72. }
  73. struct i915_gem_context *
  74. live_context(struct drm_i915_private *i915, struct drm_file *file)
  75. {
  76. lockdep_assert_held(&i915->drm.struct_mutex);
  77. return i915_gem_create_context(i915, file->driver_priv);
  78. }
  79. struct i915_gem_context *
  80. kernel_context(struct drm_i915_private *i915)
  81. {
  82. return i915_gem_context_create_kernel(i915, I915_PRIORITY_NORMAL);
  83. }
  84. void kernel_context_close(struct i915_gem_context *ctx)
  85. {
  86. context_close(ctx);
  87. }