qxl_gem.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "drmP.h"
  26. #include "drm/drm.h"
  27. #include "qxl_drv.h"
  28. #include "qxl_object.h"
  29. int qxl_gem_object_init(struct drm_gem_object *obj)
  30. {
  31. /* we do nothings here */
  32. return 0;
  33. }
  34. void qxl_gem_object_free(struct drm_gem_object *gobj)
  35. {
  36. struct qxl_bo *qobj = gem_to_qxl_bo(gobj);
  37. if (qobj)
  38. qxl_bo_unref(&qobj);
  39. }
  40. int qxl_gem_object_create(struct qxl_device *qdev, int size,
  41. int alignment, int initial_domain,
  42. bool discardable, bool kernel,
  43. struct qxl_surface *surf,
  44. struct drm_gem_object **obj)
  45. {
  46. struct qxl_bo *qbo;
  47. int r;
  48. *obj = NULL;
  49. /* At least align on page size */
  50. if (alignment < PAGE_SIZE)
  51. alignment = PAGE_SIZE;
  52. r = qxl_bo_create(qdev, size, kernel, initial_domain, surf, &qbo);
  53. if (r) {
  54. if (r != -ERESTARTSYS)
  55. DRM_ERROR(
  56. "Failed to allocate GEM object (%d, %d, %u, %d)\n",
  57. size, initial_domain, alignment, r);
  58. return r;
  59. }
  60. *obj = &qbo->gem_base;
  61. mutex_lock(&qdev->gem.mutex);
  62. list_add_tail(&qbo->list, &qdev->gem.objects);
  63. mutex_unlock(&qdev->gem.mutex);
  64. return 0;
  65. }
  66. int qxl_gem_object_create_with_handle(struct qxl_device *qdev,
  67. struct drm_file *file_priv,
  68. u32 domain,
  69. size_t size,
  70. struct qxl_surface *surf,
  71. struct qxl_bo **qobj,
  72. uint32_t *handle)
  73. {
  74. struct drm_gem_object *gobj;
  75. int r;
  76. BUG_ON(!qobj);
  77. BUG_ON(!handle);
  78. r = qxl_gem_object_create(qdev, size, 0,
  79. domain,
  80. false, false, surf,
  81. &gobj);
  82. if (r)
  83. return -ENOMEM;
  84. r = drm_gem_handle_create(file_priv, gobj, handle);
  85. if (r)
  86. return r;
  87. /* drop reference from allocate - handle holds it now */
  88. *qobj = gem_to_qxl_bo(gobj);
  89. drm_gem_object_unreference_unlocked(gobj);
  90. return 0;
  91. }
  92. int qxl_gem_object_pin(struct drm_gem_object *obj, uint32_t pin_domain,
  93. uint64_t *gpu_addr)
  94. {
  95. struct qxl_bo *qobj = obj->driver_private;
  96. int r;
  97. r = qxl_bo_reserve(qobj, false);
  98. if (unlikely(r != 0))
  99. return r;
  100. r = qxl_bo_pin(qobj, pin_domain, gpu_addr);
  101. qxl_bo_unreserve(qobj);
  102. return r;
  103. }
  104. void qxl_gem_object_unpin(struct drm_gem_object *obj)
  105. {
  106. struct qxl_bo *qobj = obj->driver_private;
  107. int r;
  108. r = qxl_bo_reserve(qobj, false);
  109. if (likely(r == 0)) {
  110. qxl_bo_unpin(qobj);
  111. qxl_bo_unreserve(qobj);
  112. }
  113. }
  114. int qxl_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
  115. {
  116. return 0;
  117. }
  118. void qxl_gem_object_close(struct drm_gem_object *obj,
  119. struct drm_file *file_priv)
  120. {
  121. }
  122. int qxl_gem_init(struct qxl_device *qdev)
  123. {
  124. INIT_LIST_HEAD(&qdev->gem.objects);
  125. return 0;
  126. }
  127. void qxl_gem_fini(struct qxl_device *qdev)
  128. {
  129. qxl_bo_force_delete(qdev);
  130. }