virtgpu_gem.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_put_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. uint32_t format;
  79. if (args->bpp != 32)
  80. return -EINVAL;
  81. pitch = args->width * 4;
  82. args->size = pitch * args->height;
  83. args->size = ALIGN(args->size, PAGE_SIZE);
  84. ret = virtio_gpu_gem_create(file_priv, dev, args->size, &gobj,
  85. &args->handle);
  86. if (ret)
  87. goto fail;
  88. format = virtio_gpu_translate_format(DRM_FORMAT_HOST_XRGB8888);
  89. virtio_gpu_resource_id_get(vgdev, &resid);
  90. virtio_gpu_cmd_create_resource(vgdev, resid, format,
  91. args->width, args->height);
  92. /* attach the object to the resource */
  93. obj = gem_to_virtio_gpu_obj(gobj);
  94. ret = virtio_gpu_object_attach(vgdev, obj, resid, NULL);
  95. if (ret)
  96. goto fail;
  97. obj->dumb = true;
  98. args->pitch = pitch;
  99. return ret;
  100. fail:
  101. return ret;
  102. }
  103. int virtio_gpu_mode_dumb_mmap(struct drm_file *file_priv,
  104. struct drm_device *dev,
  105. uint32_t handle, uint64_t *offset_p)
  106. {
  107. struct drm_gem_object *gobj;
  108. struct virtio_gpu_object *obj;
  109. BUG_ON(!offset_p);
  110. gobj = drm_gem_object_lookup(file_priv, handle);
  111. if (gobj == NULL)
  112. return -ENOENT;
  113. obj = gem_to_virtio_gpu_obj(gobj);
  114. *offset_p = virtio_gpu_object_mmap_offset(obj);
  115. drm_gem_object_put_unlocked(gobj);
  116. return 0;
  117. }
  118. int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
  119. struct drm_file *file)
  120. {
  121. struct virtio_gpu_device *vgdev = obj->dev->dev_private;
  122. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  123. struct virtio_gpu_object *qobj = gem_to_virtio_gpu_obj(obj);
  124. int r;
  125. if (!vgdev->has_virgl_3d)
  126. return 0;
  127. r = virtio_gpu_object_reserve(qobj, false);
  128. if (r)
  129. return r;
  130. virtio_gpu_cmd_context_attach_resource(vgdev, vfpriv->ctx_id,
  131. qobj->hw_res_handle);
  132. virtio_gpu_object_unreserve(qobj);
  133. return 0;
  134. }
  135. void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
  136. struct drm_file *file)
  137. {
  138. struct virtio_gpu_device *vgdev = obj->dev->dev_private;
  139. struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
  140. struct virtio_gpu_object *qobj = gem_to_virtio_gpu_obj(obj);
  141. int r;
  142. if (!vgdev->has_virgl_3d)
  143. return;
  144. r = virtio_gpu_object_reserve(qobj, false);
  145. if (r)
  146. return;
  147. virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
  148. qobj->hw_res_handle);
  149. virtio_gpu_object_unreserve(qobj);
  150. }