mock_context.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. ret = ida_simple_get(&i915->context_hw_ida,
  39. 0, MAX_CONTEXT_HW_ID, GFP_KERNEL);
  40. if (ret < 0)
  41. goto err_free;
  42. ctx->hw_id = ret;
  43. if (name) {
  44. ctx->name = kstrdup(name, GFP_KERNEL);
  45. if (!ctx->name)
  46. goto err_put;
  47. ctx->ppgtt = mock_ppgtt(i915, name);
  48. if (!ctx->ppgtt)
  49. goto err_put;
  50. }
  51. return ctx;
  52. err_free:
  53. kfree(ctx);
  54. return NULL;
  55. err_put:
  56. i915_gem_context_set_closed(ctx);
  57. i915_gem_context_put(ctx);
  58. return NULL;
  59. }
  60. void mock_context_close(struct i915_gem_context *ctx)
  61. {
  62. i915_gem_context_set_closed(ctx);
  63. i915_ppgtt_close(&ctx->ppgtt->base);
  64. i915_gem_context_put(ctx);
  65. }