virtgpu_gem.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <drm/drmP.h>
  26. #include "virtgpu_drv.h"
  27. void virtio_gpu_gem_free_object(struct drm_gem_object *gem_obj)
  28. {
  29. struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(gem_obj);
  30. if (obj)
  31. virtio_gpu_object_unref(&obj);
  32. }
  33. struct virtio_gpu_object *virtio_gpu_alloc_object(struct drm_device *dev,
  34. size_t size, bool kernel,
  35. bool pinned)
  36. {
  37. struct virtio_gpu_device *vgdev = dev->dev_private;
  38. struct virtio_gpu_object *obj;
  39. int ret;
  40. ret = virtio_gpu_object_create(vgdev, size, kernel, pinned, &obj);
  41. if (ret)
  42. return ERR_PTR(ret);
  43. return obj;
  44. }
  45. int virtio_gpu_gem_create(struct drm_file *file,
  46. struct drm_device *dev,
  47. uint64_t size,
  48. struct drm_gem_object **obj_p,
  49. uint32_t *handle_p)
  50. {
  51. struct virtio_gpu_object *obj;
  52. int ret;
  53. u32 handle;
  54. obj = virtio_gpu_alloc_object(dev, size, false, false);
  55. if (IS_ERR(obj))
  56. return PTR_ERR(obj);
  57. ret = drm_gem_handle_create(file, &obj->gem_base, &handle);
  58. if (ret) {
  59. drm_gem_object_release(&obj->gem_base);
  60. return ret;
  61. }
  62. *obj_p = &obj->gem_base;
  63. /* drop reference from allocate - handle holds it now */
  64. drm_gem_object_unreference_unlocked(&obj->gem_base);
  65. *handle_p = handle;
  66. return 0;
  67. }
  68. int virtio_gpu_mode_dumb_create(struct drm_file *file_priv,
  69. struct drm_device *dev,
  70. struct drm_mode_create_dumb *args)
  71. {
  72. struct virtio_gpu_device *vgdev = dev->dev_private;
  73. struct drm_gem_object *gobj;
  74. struct virtio_gpu_object *obj;
  75. int ret;
  76. uint32_t pitch;
  77. uint32_t resid;
  78. pitch = args->width * ((args->bpp + 1) / 8);
  79. args->size = pitch * args->height;
  80. args->size = ALIGN(args->size, PAGE_SIZE);
  81. ret = virtio_gpu_gem_create(file_priv, dev, args->size, &gobj,
  82. &args->handle);
  83. if (ret)
  84. goto fail;
  85. virtio_gpu_resource_id_get(vgdev, &resid);
  86. virtio_gpu_cmd_create_resource(vgdev, resid,
  87. 2, args->width, args->height);
  88. /* attach the object to the resource */
  89. obj = gem_to_virtio_gpu_obj(gobj);
  90. ret = virtio_gpu_object_attach(vgdev, obj, resid, NULL);
  91. if (ret)
  92. goto fail;
  93. obj->dumb = true;
  94. args->pitch = pitch;
  95. return ret;
  96. fail:
  97. return ret;
  98. }
  99. int virtio_gpu_mode_dumb_destroy(struct drm_file *file_priv,
  100. struct drm_device *dev,
  101. uint32_t handle)
  102. {
  103. return drm_gem_handle_delete(file_priv, handle);
  104. }
  105. int virtio_gpu_mode_dumb_mmap(struct drm_file *file_priv,
  106. struct drm_device *dev,
  107. uint32_t handle, uint64_t *offset_p)
  108. {
  109. struct drm_gem_object *gobj;
  110. struct virtio_gpu_object *obj;
  111. BUG_ON(!offset_p);
  112. gobj = drm_gem_object_lookup(file_priv, handle);
  113. if (gobj == NULL)
  114. return -ENOENT;
  115. obj = gem_to_virtio_gpu_obj(gobj);
  116. *offset_p = virtio_gpu_object_mmap_offset(obj);
  117. drm_gem_object_unreference_unlocked(gobj);
  118. return 0;
  119. }
  120. int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
  121. struct drm_file *file)
  122. {
  123. struct virtio_gpu_device *vgdev = obj->dev->dev_private;
  124. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  125. struct virtio_gpu_object *qobj = gem_to_virtio_gpu_obj(obj);
  126. int r;
  127. if (!vgdev->has_virgl_3d)
  128. return 0;
  129. r = virtio_gpu_object_reserve(qobj, false);
  130. if (r)
  131. return r;
  132. virtio_gpu_cmd_context_attach_resource(vgdev, vfpriv->ctx_id,
  133. qobj->hw_res_handle);
  134. virtio_gpu_object_unreserve(qobj);
  135. return 0;
  136. }
  137. void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
  138. struct drm_file *file)
  139. {
  140. struct virtio_gpu_device *vgdev = obj->dev->dev_private;
  141. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  142. struct virtio_gpu_object *qobj = gem_to_virtio_gpu_obj(obj);
  143. int r;
  144. if (!vgdev->has_virgl_3d)
  145. return;
  146. r = virtio_gpu_object_reserve(qobj, false);
  147. if (r)
  148. return;
  149. virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
  150. qobj->hw_res_handle);
  151. virtio_gpu_object_unreserve(qobj);
  152. }