virtgpu_plane.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "virtgpu_drv.h"
  26. #include <drm/drm_plane_helper.h>
  27. #include <drm/drm_atomic_helper.h>
  28. static const uint32_t virtio_gpu_formats[] = {
  29. DRM_FORMAT_HOST_XRGB8888,
  30. };
  31. static const uint32_t virtio_gpu_cursor_formats[] = {
  32. DRM_FORMAT_HOST_ARGB8888,
  33. };
  34. uint32_t virtio_gpu_translate_format(uint32_t drm_fourcc)
  35. {
  36. uint32_t format;
  37. switch (drm_fourcc) {
  38. case DRM_FORMAT_XRGB8888:
  39. format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
  40. break;
  41. case DRM_FORMAT_ARGB8888:
  42. format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
  43. break;
  44. case DRM_FORMAT_BGRX8888:
  45. format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
  46. break;
  47. case DRM_FORMAT_BGRA8888:
  48. format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
  49. break;
  50. default:
  51. /*
  52. * This should not happen, we handle everything listed
  53. * in virtio_gpu_formats[].
  54. */
  55. format = 0;
  56. break;
  57. }
  58. WARN_ON(format == 0);
  59. return format;
  60. }
  61. static void virtio_gpu_plane_destroy(struct drm_plane *plane)
  62. {
  63. drm_plane_cleanup(plane);
  64. kfree(plane);
  65. }
  66. static const struct drm_plane_funcs virtio_gpu_plane_funcs = {
  67. .update_plane = drm_atomic_helper_update_plane,
  68. .disable_plane = drm_atomic_helper_disable_plane,
  69. .destroy = virtio_gpu_plane_destroy,
  70. .reset = drm_atomic_helper_plane_reset,
  71. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  72. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  73. };
  74. static int virtio_gpu_plane_atomic_check(struct drm_plane *plane,
  75. struct drm_plane_state *state)
  76. {
  77. return 0;
  78. }
  79. static void virtio_gpu_primary_plane_update(struct drm_plane *plane,
  80. struct drm_plane_state *old_state)
  81. {
  82. struct drm_device *dev = plane->dev;
  83. struct virtio_gpu_device *vgdev = dev->dev_private;
  84. struct virtio_gpu_output *output = NULL;
  85. struct virtio_gpu_framebuffer *vgfb;
  86. struct virtio_gpu_object *bo;
  87. uint32_t handle;
  88. if (plane->state->crtc)
  89. output = drm_crtc_to_virtio_gpu_output(plane->state->crtc);
  90. if (old_state->crtc)
  91. output = drm_crtc_to_virtio_gpu_output(old_state->crtc);
  92. if (WARN_ON(!output))
  93. return;
  94. if (plane->state->fb && output->enabled) {
  95. vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
  96. bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
  97. handle = bo->hw_res_handle;
  98. if (bo->dumb) {
  99. virtio_gpu_cmd_transfer_to_host_2d
  100. (vgdev, bo, 0,
  101. cpu_to_le32(plane->state->src_w >> 16),
  102. cpu_to_le32(plane->state->src_h >> 16),
  103. cpu_to_le32(plane->state->src_x >> 16),
  104. cpu_to_le32(plane->state->src_y >> 16), NULL);
  105. }
  106. } else {
  107. handle = 0;
  108. }
  109. DRM_DEBUG("handle 0x%x, crtc %dx%d+%d+%d, src %dx%d+%d+%d\n", handle,
  110. plane->state->crtc_w, plane->state->crtc_h,
  111. plane->state->crtc_x, plane->state->crtc_y,
  112. plane->state->src_w >> 16,
  113. plane->state->src_h >> 16,
  114. plane->state->src_x >> 16,
  115. plane->state->src_y >> 16);
  116. virtio_gpu_cmd_set_scanout(vgdev, output->index, handle,
  117. plane->state->src_w >> 16,
  118. plane->state->src_h >> 16,
  119. plane->state->src_x >> 16,
  120. plane->state->src_y >> 16);
  121. virtio_gpu_cmd_resource_flush(vgdev, handle,
  122. plane->state->src_x >> 16,
  123. plane->state->src_y >> 16,
  124. plane->state->src_w >> 16,
  125. plane->state->src_h >> 16);
  126. }
  127. static void virtio_gpu_cursor_plane_update(struct drm_plane *plane,
  128. struct drm_plane_state *old_state)
  129. {
  130. struct drm_device *dev = plane->dev;
  131. struct virtio_gpu_device *vgdev = dev->dev_private;
  132. struct virtio_gpu_output *output = NULL;
  133. struct virtio_gpu_framebuffer *vgfb;
  134. struct virtio_gpu_fence *fence = NULL;
  135. struct virtio_gpu_object *bo = NULL;
  136. uint32_t handle;
  137. int ret = 0;
  138. if (plane->state->crtc)
  139. output = drm_crtc_to_virtio_gpu_output(plane->state->crtc);
  140. if (old_state->crtc)
  141. output = drm_crtc_to_virtio_gpu_output(old_state->crtc);
  142. if (WARN_ON(!output))
  143. return;
  144. if (plane->state->fb) {
  145. vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
  146. bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
  147. handle = bo->hw_res_handle;
  148. } else {
  149. handle = 0;
  150. }
  151. if (bo && bo->dumb && (plane->state->fb != old_state->fb)) {
  152. /* new cursor -- update & wait */
  153. virtio_gpu_cmd_transfer_to_host_2d
  154. (vgdev, bo, 0,
  155. cpu_to_le32(plane->state->crtc_w),
  156. cpu_to_le32(plane->state->crtc_h),
  157. 0, 0, &fence);
  158. ret = virtio_gpu_object_reserve(bo, false);
  159. if (!ret) {
  160. reservation_object_add_excl_fence(bo->tbo.resv,
  161. &fence->f);
  162. dma_fence_put(&fence->f);
  163. fence = NULL;
  164. virtio_gpu_object_unreserve(bo);
  165. virtio_gpu_object_wait(bo, false);
  166. }
  167. }
  168. if (plane->state->fb != old_state->fb) {
  169. DRM_DEBUG("update, handle %d, pos +%d+%d, hot %d,%d\n", handle,
  170. plane->state->crtc_x,
  171. plane->state->crtc_y,
  172. plane->state->fb ? plane->state->fb->hot_x : 0,
  173. plane->state->fb ? plane->state->fb->hot_y : 0);
  174. output->cursor.hdr.type =
  175. cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR);
  176. output->cursor.resource_id = cpu_to_le32(handle);
  177. if (plane->state->fb) {
  178. output->cursor.hot_x =
  179. cpu_to_le32(plane->state->fb->hot_x);
  180. output->cursor.hot_y =
  181. cpu_to_le32(plane->state->fb->hot_y);
  182. } else {
  183. output->cursor.hot_x = cpu_to_le32(0);
  184. output->cursor.hot_y = cpu_to_le32(0);
  185. }
  186. } else {
  187. DRM_DEBUG("move +%d+%d\n",
  188. plane->state->crtc_x,
  189. plane->state->crtc_y);
  190. output->cursor.hdr.type =
  191. cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR);
  192. }
  193. output->cursor.pos.x = cpu_to_le32(plane->state->crtc_x);
  194. output->cursor.pos.y = cpu_to_le32(plane->state->crtc_y);
  195. virtio_gpu_cursor_ping(vgdev, output);
  196. }
  197. static const struct drm_plane_helper_funcs virtio_gpu_primary_helper_funcs = {
  198. .atomic_check = virtio_gpu_plane_atomic_check,
  199. .atomic_update = virtio_gpu_primary_plane_update,
  200. };
  201. static const struct drm_plane_helper_funcs virtio_gpu_cursor_helper_funcs = {
  202. .atomic_check = virtio_gpu_plane_atomic_check,
  203. .atomic_update = virtio_gpu_cursor_plane_update,
  204. };
  205. struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev,
  206. enum drm_plane_type type,
  207. int index)
  208. {
  209. struct drm_device *dev = vgdev->ddev;
  210. const struct drm_plane_helper_funcs *funcs;
  211. struct drm_plane *plane;
  212. const uint32_t *formats;
  213. int ret, nformats;
  214. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  215. if (!plane)
  216. return ERR_PTR(-ENOMEM);
  217. if (type == DRM_PLANE_TYPE_CURSOR) {
  218. formats = virtio_gpu_cursor_formats;
  219. nformats = ARRAY_SIZE(virtio_gpu_cursor_formats);
  220. funcs = &virtio_gpu_cursor_helper_funcs;
  221. } else {
  222. formats = virtio_gpu_formats;
  223. nformats = ARRAY_SIZE(virtio_gpu_formats);
  224. funcs = &virtio_gpu_primary_helper_funcs;
  225. }
  226. ret = drm_universal_plane_init(dev, plane, 1 << index,
  227. &virtio_gpu_plane_funcs,
  228. formats, nformats,
  229. NULL, type, NULL);
  230. if (ret)
  231. goto err_plane_init;
  232. drm_plane_helper_add(plane, funcs);
  233. return plane;
  234. err_plane_init:
  235. kfree(plane);
  236. return ERR_PTR(ret);
  237. }