qxl_gem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  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 shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie
  23. * Alon Levy
  24. */
  25. #include <drm/drm.h>
  26. #include <drm/drm_print.h>
  27. #include "qxl_drv.h"
  28. #include "qxl_object.h"
  29. void qxl_gem_object_free(struct drm_gem_object *gobj)
  30. {
  31. struct qxl_bo *qobj = gem_to_qxl_bo(gobj);
  32. struct qxl_device *qdev;
  33. struct ttm_buffer_object *tbo;
  34. qdev = to_qxl(gobj->dev);
  35. qxl_surface_evict(qdev, qobj, false);
  36. tbo = &qobj->tbo;
  37. ttm_bo_fini(tbo);
  38. }
  39. int qxl_gem_object_create(struct qxl_device *qdev, int size,
  40. int alignment, int initial_domain,
  41. bool discardable, bool kernel,
  42. struct qxl_surface *surf,
  43. struct drm_gem_object **obj)
  44. {
  45. struct qxl_bo *qbo;
  46. int r;
  47. *obj = NULL;
  48. /* At least align on page size */
  49. if (alignment < PAGE_SIZE)
  50. alignment = PAGE_SIZE;
  51. r = qxl_bo_create(qdev, size, kernel, false, initial_domain, 0, surf, &qbo);
  52. if (r) {
  53. if (r != -ERESTARTSYS)
  54. DRM_ERROR(
  55. "Failed to allocate GEM object (%d, %d, %u, %d)\n",
  56. size, initial_domain, alignment, r);
  57. return r;
  58. }
  59. *obj = &qbo->tbo.base;
  60. mutex_lock(&qdev->gem.mutex);
  61. list_add_tail(&qbo->list, &qdev->gem.objects);
  62. mutex_unlock(&qdev->gem.mutex);
  63. return 0;
  64. }
  65. /*
  66. * If the caller passed a valid gobj pointer, it is responsible to call
  67. * drm_gem_object_put() when it no longer needs to acess the object.
  68. *
  69. * If gobj is NULL, it is handled internally.
  70. */
  71. int qxl_gem_object_create_with_handle(struct qxl_device *qdev,
  72. struct drm_file *file_priv,
  73. u32 domain,
  74. size_t size,
  75. struct qxl_surface *surf,
  76. struct drm_gem_object **gobj,
  77. uint32_t *handle)
  78. {
  79. int r;
  80. struct drm_gem_object *local_gobj;
  81. BUG_ON(!handle);
  82. r = qxl_gem_object_create(qdev, size, 0,
  83. domain,
  84. false, false, surf,
  85. &local_gobj);
  86. if (r)
  87. return -ENOMEM;
  88. r = drm_gem_handle_create(file_priv, local_gobj, handle);
  89. if (r)
  90. return r;
  91. if (gobj)
  92. *gobj = local_gobj;
  93. else
  94. /* drop reference from allocate - handle holds it now */
  95. drm_gem_object_put(local_gobj);
  96. return 0;
  97. }
  98. int qxl_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
  99. {
  100. return 0;
  101. }
  102. void qxl_gem_object_close(struct drm_gem_object *obj,
  103. struct drm_file *file_priv)
  104. {
  105. }
  106. void qxl_gem_init(struct qxl_device *qdev)
  107. {
  108. INIT_LIST_HEAD(&qdev->gem.objects);
  109. }
  110. void qxl_gem_fini(struct qxl_device *qdev)
  111. {
  112. qxl_bo_force_delete(qdev);
  113. }