virtgpu_plane.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/drm_atomic_helper.h>
  26. #include <drm/drm_damage_helper.h>
  27. #include <drm/drm_fourcc.h>
  28. #include "virtgpu_drv.h"
  29. static const uint32_t virtio_gpu_formats[] = {
  30. DRM_FORMAT_XRGB8888,
  31. DRM_FORMAT_ARGB8888,
  32. DRM_FORMAT_BGRX8888,
  33. DRM_FORMAT_BGRA8888,
  34. DRM_FORMAT_RGBX8888,
  35. DRM_FORMAT_RGBA8888,
  36. DRM_FORMAT_XBGR8888,
  37. DRM_FORMAT_ABGR8888,
  38. };
  39. static const uint32_t virtio_gpu_cursor_formats[] = {
  40. DRM_FORMAT_HOST_ARGB8888,
  41. };
  42. uint32_t virtio_gpu_translate_format(uint32_t drm_fourcc)
  43. {
  44. uint32_t format;
  45. switch (drm_fourcc) {
  46. #ifdef __BIG_ENDIAN
  47. case DRM_FORMAT_XRGB8888:
  48. format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
  49. break;
  50. case DRM_FORMAT_ARGB8888:
  51. format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
  52. break;
  53. case DRM_FORMAT_BGRX8888:
  54. format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
  55. break;
  56. case DRM_FORMAT_BGRA8888:
  57. format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
  58. break;
  59. case DRM_FORMAT_RGBX8888:
  60. format = VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM;
  61. break;
  62. case DRM_FORMAT_RGBA8888:
  63. format = VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM;
  64. break;
  65. case DRM_FORMAT_XBGR8888:
  66. format = VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM;
  67. break;
  68. case DRM_FORMAT_ABGR8888:
  69. format = VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM;
  70. break;
  71. #else
  72. case DRM_FORMAT_XRGB8888:
  73. format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
  74. break;
  75. case DRM_FORMAT_ARGB8888:
  76. format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
  77. break;
  78. case DRM_FORMAT_BGRX8888:
  79. format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
  80. break;
  81. case DRM_FORMAT_BGRA8888:
  82. format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
  83. break;
  84. case DRM_FORMAT_RGBX8888:
  85. format = VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM;
  86. break;
  87. case DRM_FORMAT_RGBA8888:
  88. format = VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM;
  89. break;
  90. case DRM_FORMAT_XBGR8888:
  91. format = VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM;
  92. break;
  93. case DRM_FORMAT_ABGR8888:
  94. format = VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM;
  95. break;
  96. #endif
  97. default:
  98. /*
  99. * This should not happen, we handle everything listed
  100. * in virtio_gpu_formats[].
  101. */
  102. format = 0;
  103. break;
  104. }
  105. WARN_ON(format == 0);
  106. return format;
  107. }
  108. static const struct drm_plane_funcs virtio_gpu_plane_funcs = {
  109. .update_plane = drm_atomic_helper_update_plane,
  110. .disable_plane = drm_atomic_helper_disable_plane,
  111. .reset = drm_atomic_helper_plane_reset,
  112. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  113. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  114. };
  115. static int virtio_gpu_plane_atomic_check(struct drm_plane *plane,
  116. struct drm_atomic_state *state)
  117. {
  118. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  119. plane);
  120. bool is_cursor = plane->type == DRM_PLANE_TYPE_CURSOR;
  121. struct drm_crtc_state *crtc_state;
  122. int ret;
  123. if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc))
  124. return 0;
  125. crtc_state = drm_atomic_get_crtc_state(state,
  126. new_plane_state->crtc);
  127. if (IS_ERR(crtc_state))
  128. return PTR_ERR(crtc_state);
  129. ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
  130. DRM_PLANE_NO_SCALING,
  131. DRM_PLANE_NO_SCALING,
  132. is_cursor, true);
  133. return ret;
  134. }
  135. static void virtio_gpu_update_dumb_bo(struct virtio_gpu_device *vgdev,
  136. struct drm_plane_state *state,
  137. struct drm_rect *rect)
  138. {
  139. struct virtio_gpu_object *bo =
  140. gem_to_virtio_gpu_obj(state->fb->obj[0]);
  141. struct virtio_gpu_object_array *objs;
  142. uint32_t w = rect->x2 - rect->x1;
  143. uint32_t h = rect->y2 - rect->y1;
  144. uint32_t x = rect->x1;
  145. uint32_t y = rect->y1;
  146. uint32_t off = x * state->fb->format->cpp[0] +
  147. y * state->fb->pitches[0];
  148. objs = virtio_gpu_array_alloc(1);
  149. if (!objs)
  150. return;
  151. virtio_gpu_array_add_obj(objs, &bo->base.base);
  152. virtio_gpu_cmd_transfer_to_host_2d(vgdev, off, w, h, x, y,
  153. objs, NULL);
  154. }
  155. static void virtio_gpu_resource_flush(struct drm_plane *plane,
  156. uint32_t x, uint32_t y,
  157. uint32_t width, uint32_t height)
  158. {
  159. struct drm_device *dev = plane->dev;
  160. struct virtio_gpu_device *vgdev = dev->dev_private;
  161. struct virtio_gpu_framebuffer *vgfb;
  162. struct virtio_gpu_object *bo;
  163. vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
  164. bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
  165. if (vgfb->fence) {
  166. struct virtio_gpu_object_array *objs;
  167. objs = virtio_gpu_array_alloc(1);
  168. if (!objs)
  169. return;
  170. virtio_gpu_array_add_obj(objs, vgfb->base.obj[0]);
  171. virtio_gpu_array_lock_resv(objs);
  172. virtio_gpu_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y,
  173. width, height, objs, vgfb->fence);
  174. virtio_gpu_notify(vgdev);
  175. dma_fence_wait_timeout(&vgfb->fence->f, true,
  176. msecs_to_jiffies(50));
  177. dma_fence_put(&vgfb->fence->f);
  178. vgfb->fence = NULL;
  179. } else {
  180. virtio_gpu_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y,
  181. width, height, NULL, NULL);
  182. virtio_gpu_notify(vgdev);
  183. }
  184. }
  185. static void virtio_gpu_primary_plane_update(struct drm_plane *plane,
  186. struct drm_atomic_state *state)
  187. {
  188. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  189. plane);
  190. struct drm_device *dev = plane->dev;
  191. struct virtio_gpu_device *vgdev = dev->dev_private;
  192. struct virtio_gpu_output *output = NULL;
  193. struct virtio_gpu_object *bo;
  194. struct drm_rect rect;
  195. if (plane->state->crtc)
  196. output = drm_crtc_to_virtio_gpu_output(plane->state->crtc);
  197. if (old_state->crtc)
  198. output = drm_crtc_to_virtio_gpu_output(old_state->crtc);
  199. if (WARN_ON(!output))
  200. return;
  201. if (!plane->state->fb || !output->crtc.state->active) {
  202. DRM_DEBUG("nofb\n");
  203. virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
  204. plane->state->src_w >> 16,
  205. plane->state->src_h >> 16,
  206. 0, 0);
  207. virtio_gpu_notify(vgdev);
  208. return;
  209. }
  210. if (!drm_atomic_helper_damage_merged(old_state, plane->state, &rect))
  211. return;
  212. bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
  213. if (bo->dumb)
  214. virtio_gpu_update_dumb_bo(vgdev, plane->state, &rect);
  215. if (plane->state->fb != old_state->fb ||
  216. plane->state->src_w != old_state->src_w ||
  217. plane->state->src_h != old_state->src_h ||
  218. plane->state->src_x != old_state->src_x ||
  219. plane->state->src_y != old_state->src_y ||
  220. output->needs_modeset) {
  221. output->needs_modeset = false;
  222. DRM_DEBUG("handle 0x%x, crtc %dx%d+%d+%d, src %dx%d+%d+%d\n",
  223. bo->hw_res_handle,
  224. plane->state->crtc_w, plane->state->crtc_h,
  225. plane->state->crtc_x, plane->state->crtc_y,
  226. plane->state->src_w >> 16,
  227. plane->state->src_h >> 16,
  228. plane->state->src_x >> 16,
  229. plane->state->src_y >> 16);
  230. if (bo->host3d_blob || bo->guest_blob) {
  231. virtio_gpu_cmd_set_scanout_blob
  232. (vgdev, output->index, bo,
  233. plane->state->fb,
  234. plane->state->src_w >> 16,
  235. plane->state->src_h >> 16,
  236. plane->state->src_x >> 16,
  237. plane->state->src_y >> 16);
  238. } else {
  239. virtio_gpu_cmd_set_scanout(vgdev, output->index,
  240. bo->hw_res_handle,
  241. plane->state->src_w >> 16,
  242. plane->state->src_h >> 16,
  243. plane->state->src_x >> 16,
  244. plane->state->src_y >> 16);
  245. }
  246. }
  247. virtio_gpu_resource_flush(plane,
  248. rect.x1,
  249. rect.y1,
  250. rect.x2 - rect.x1,
  251. rect.y2 - rect.y1);
  252. }
  253. static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
  254. struct drm_plane_state *new_state)
  255. {
  256. struct drm_device *dev = plane->dev;
  257. struct virtio_gpu_device *vgdev = dev->dev_private;
  258. struct virtio_gpu_framebuffer *vgfb;
  259. struct virtio_gpu_object *bo;
  260. if (!new_state->fb)
  261. return 0;
  262. vgfb = to_virtio_gpu_framebuffer(new_state->fb);
  263. bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
  264. if (!bo || (plane->type == DRM_PLANE_TYPE_PRIMARY && !bo->guest_blob))
  265. return 0;
  266. if (bo->dumb && (plane->state->fb != new_state->fb)) {
  267. vgfb->fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context,
  268. 0);
  269. if (!vgfb->fence)
  270. return -ENOMEM;
  271. }
  272. return 0;
  273. }
  274. static void virtio_gpu_plane_cleanup_fb(struct drm_plane *plane,
  275. struct drm_plane_state *state)
  276. {
  277. struct virtio_gpu_framebuffer *vgfb;
  278. if (!state->fb)
  279. return;
  280. vgfb = to_virtio_gpu_framebuffer(state->fb);
  281. if (vgfb->fence) {
  282. dma_fence_put(&vgfb->fence->f);
  283. vgfb->fence = NULL;
  284. }
  285. }
  286. static void virtio_gpu_cursor_plane_update(struct drm_plane *plane,
  287. struct drm_atomic_state *state)
  288. {
  289. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  290. plane);
  291. struct drm_device *dev = plane->dev;
  292. struct virtio_gpu_device *vgdev = dev->dev_private;
  293. struct virtio_gpu_output *output = NULL;
  294. struct virtio_gpu_framebuffer *vgfb;
  295. struct virtio_gpu_object *bo = NULL;
  296. uint32_t handle;
  297. if (plane->state->crtc)
  298. output = drm_crtc_to_virtio_gpu_output(plane->state->crtc);
  299. if (old_state->crtc)
  300. output = drm_crtc_to_virtio_gpu_output(old_state->crtc);
  301. if (WARN_ON(!output))
  302. return;
  303. if (plane->state->fb) {
  304. vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
  305. bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
  306. handle = bo->hw_res_handle;
  307. } else {
  308. handle = 0;
  309. }
  310. if (bo && bo->dumb && (plane->state->fb != old_state->fb)) {
  311. /* new cursor -- update & wait */
  312. struct virtio_gpu_object_array *objs;
  313. objs = virtio_gpu_array_alloc(1);
  314. if (!objs)
  315. return;
  316. virtio_gpu_array_add_obj(objs, vgfb->base.obj[0]);
  317. virtio_gpu_array_lock_resv(objs);
  318. virtio_gpu_cmd_transfer_to_host_2d
  319. (vgdev, 0,
  320. plane->state->crtc_w,
  321. plane->state->crtc_h,
  322. 0, 0, objs, vgfb->fence);
  323. virtio_gpu_notify(vgdev);
  324. dma_fence_wait(&vgfb->fence->f, true);
  325. dma_fence_put(&vgfb->fence->f);
  326. vgfb->fence = NULL;
  327. }
  328. if (plane->state->fb != old_state->fb) {
  329. DRM_DEBUG("update, handle %d, pos +%d+%d, hot %d,%d\n", handle,
  330. plane->state->crtc_x,
  331. plane->state->crtc_y,
  332. plane->state->fb ? plane->state->fb->hot_x : 0,
  333. plane->state->fb ? plane->state->fb->hot_y : 0);
  334. output->cursor.hdr.type =
  335. cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR);
  336. output->cursor.resource_id = cpu_to_le32(handle);
  337. if (plane->state->fb) {
  338. output->cursor.hot_x =
  339. cpu_to_le32(plane->state->fb->hot_x);
  340. output->cursor.hot_y =
  341. cpu_to_le32(plane->state->fb->hot_y);
  342. } else {
  343. output->cursor.hot_x = cpu_to_le32(0);
  344. output->cursor.hot_y = cpu_to_le32(0);
  345. }
  346. } else {
  347. DRM_DEBUG("move +%d+%d\n",
  348. plane->state->crtc_x,
  349. plane->state->crtc_y);
  350. output->cursor.hdr.type =
  351. cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR);
  352. }
  353. output->cursor.pos.x = cpu_to_le32(plane->state->crtc_x);
  354. output->cursor.pos.y = cpu_to_le32(plane->state->crtc_y);
  355. virtio_gpu_cursor_ping(vgdev, output);
  356. }
  357. static const struct drm_plane_helper_funcs virtio_gpu_primary_helper_funcs = {
  358. .prepare_fb = virtio_gpu_plane_prepare_fb,
  359. .cleanup_fb = virtio_gpu_plane_cleanup_fb,
  360. .atomic_check = virtio_gpu_plane_atomic_check,
  361. .atomic_update = virtio_gpu_primary_plane_update,
  362. };
  363. static const struct drm_plane_helper_funcs virtio_gpu_cursor_helper_funcs = {
  364. .prepare_fb = virtio_gpu_plane_prepare_fb,
  365. .cleanup_fb = virtio_gpu_plane_cleanup_fb,
  366. .atomic_check = virtio_gpu_plane_atomic_check,
  367. .atomic_update = virtio_gpu_cursor_plane_update,
  368. };
  369. struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev,
  370. enum drm_plane_type type,
  371. int index)
  372. {
  373. struct drm_device *dev = vgdev->ddev;
  374. const struct drm_plane_helper_funcs *funcs;
  375. struct drm_plane *plane;
  376. const uint32_t *formats;
  377. int nformats;
  378. if (type == DRM_PLANE_TYPE_CURSOR) {
  379. formats = virtio_gpu_cursor_formats;
  380. nformats = ARRAY_SIZE(virtio_gpu_cursor_formats);
  381. funcs = &virtio_gpu_cursor_helper_funcs;
  382. } else {
  383. formats = virtio_gpu_formats;
  384. nformats = ARRAY_SIZE(virtio_gpu_formats);
  385. funcs = &virtio_gpu_primary_helper_funcs;
  386. }
  387. plane = drmm_universal_plane_alloc(dev, struct drm_plane, dev,
  388. 1 << index, &virtio_gpu_plane_funcs,
  389. formats, nformats, NULL, type, NULL);
  390. if (IS_ERR(plane))
  391. return plane;
  392. drm_plane_helper_add(plane, funcs);
  393. return plane;
  394. }