2 * Copyright (C) 2015 Red Hat, Inc.
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:
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.
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.
26 #include <drm/drm_atomic_helper.h>
27 #include <drm/drm_damage_helper.h>
28 #include <drm/drm_fourcc.h>
29 #include <drm/drm_plane_helper.h>
31 #include "virtgpu_drv.h"
33 static const uint32_t virtio_gpu_formats[] = {
34 DRM_FORMAT_HOST_XRGB8888,
37 static const uint32_t virtio_gpu_cursor_formats[] = {
38 DRM_FORMAT_HOST_ARGB8888,
41 uint32_t virtio_gpu_translate_format(uint32_t drm_fourcc)
46 case DRM_FORMAT_XRGB8888:
47 format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
49 case DRM_FORMAT_ARGB8888:
50 format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
52 case DRM_FORMAT_BGRX8888:
53 format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
55 case DRM_FORMAT_BGRA8888:
56 format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
60 * This should not happen, we handle everything listed
61 * in virtio_gpu_formats[].
70 static const struct drm_plane_funcs virtio_gpu_plane_funcs = {
71 .update_plane = drm_atomic_helper_update_plane,
72 .disable_plane = drm_atomic_helper_disable_plane,
73 .destroy = drm_plane_cleanup,
74 .reset = drm_atomic_helper_plane_reset,
75 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
76 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
79 static int virtio_gpu_plane_atomic_check(struct drm_plane *plane,
80 struct drm_atomic_state *state)
82 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
84 bool is_cursor = plane->type == DRM_PLANE_TYPE_CURSOR;
85 struct drm_crtc_state *crtc_state;
88 if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc))
91 crtc_state = drm_atomic_get_crtc_state(state,
92 new_plane_state->crtc);
93 if (IS_ERR(crtc_state))
94 return PTR_ERR(crtc_state);
96 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
97 DRM_PLANE_HELPER_NO_SCALING,
98 DRM_PLANE_HELPER_NO_SCALING,
103 static void virtio_gpu_update_dumb_bo(struct virtio_gpu_device *vgdev,
104 struct drm_plane_state *state,
105 struct drm_rect *rect)
107 struct virtio_gpu_object *bo =
108 gem_to_virtio_gpu_obj(state->fb->obj[0]);
109 struct virtio_gpu_object_array *objs;
110 uint32_t w = rect->x2 - rect->x1;
111 uint32_t h = rect->y2 - rect->y1;
112 uint32_t x = rect->x1;
113 uint32_t y = rect->y1;
114 uint32_t off = x * state->fb->format->cpp[0] +
115 y * state->fb->pitches[0];
117 objs = virtio_gpu_array_alloc(1);
120 virtio_gpu_array_add_obj(objs, &bo->base.base);
122 virtio_gpu_cmd_transfer_to_host_2d(vgdev, off, w, h, x, y,
126 static void virtio_gpu_resource_flush(struct drm_plane *plane,
127 uint32_t x, uint32_t y,
128 uint32_t width, uint32_t height)
130 struct drm_device *dev = plane->dev;
131 struct virtio_gpu_device *vgdev = dev->dev_private;
132 struct virtio_gpu_framebuffer *vgfb;
133 struct virtio_gpu_object *bo;
135 vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
136 bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
138 struct virtio_gpu_object_array *objs;
140 objs = virtio_gpu_array_alloc(1);
143 virtio_gpu_array_add_obj(objs, vgfb->base.obj[0]);
144 virtio_gpu_array_lock_resv(objs);
145 virtio_gpu_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y,
146 width, height, objs, vgfb->fence);
147 virtio_gpu_notify(vgdev);
149 dma_fence_wait_timeout(&vgfb->fence->f, true,
150 msecs_to_jiffies(50));
151 dma_fence_put(&vgfb->fence->f);
154 virtio_gpu_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y,
155 width, height, NULL, NULL);
156 virtio_gpu_notify(vgdev);
160 static void virtio_gpu_primary_plane_update(struct drm_plane *plane,
161 struct drm_atomic_state *state)
163 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
165 struct drm_device *dev = plane->dev;
166 struct virtio_gpu_device *vgdev = dev->dev_private;
167 struct virtio_gpu_output *output = NULL;
168 struct virtio_gpu_object *bo;
169 struct drm_rect rect;
171 if (plane->state->crtc)
172 output = drm_crtc_to_virtio_gpu_output(plane->state->crtc);
174 output = drm_crtc_to_virtio_gpu_output(old_state->crtc);
175 if (WARN_ON(!output))
178 if (!plane->state->fb || !output->crtc.state->active) {
180 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
181 plane->state->src_w >> 16,
182 plane->state->src_h >> 16,
184 virtio_gpu_notify(vgdev);
188 if (!drm_atomic_helper_damage_merged(old_state, plane->state, &rect))
191 bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
193 virtio_gpu_update_dumb_bo(vgdev, plane->state, &rect);
195 if (plane->state->fb != old_state->fb ||
196 plane->state->src_w != old_state->src_w ||
197 plane->state->src_h != old_state->src_h ||
198 plane->state->src_x != old_state->src_x ||
199 plane->state->src_y != old_state->src_y ||
200 output->needs_modeset) {
201 output->needs_modeset = false;
202 DRM_DEBUG("handle 0x%x, crtc %dx%d+%d+%d, src %dx%d+%d+%d\n",
204 plane->state->crtc_w, plane->state->crtc_h,
205 plane->state->crtc_x, plane->state->crtc_y,
206 plane->state->src_w >> 16,
207 plane->state->src_h >> 16,
208 plane->state->src_x >> 16,
209 plane->state->src_y >> 16);
211 if (bo->host3d_blob || bo->guest_blob) {
212 virtio_gpu_cmd_set_scanout_blob
213 (vgdev, output->index, bo,
215 plane->state->src_w >> 16,
216 plane->state->src_h >> 16,
217 plane->state->src_x >> 16,
218 plane->state->src_y >> 16);
220 virtio_gpu_cmd_set_scanout(vgdev, output->index,
222 plane->state->src_w >> 16,
223 plane->state->src_h >> 16,
224 plane->state->src_x >> 16,
225 plane->state->src_y >> 16);
229 virtio_gpu_resource_flush(plane,
236 static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
237 struct drm_plane_state *new_state)
239 struct drm_device *dev = plane->dev;
240 struct virtio_gpu_device *vgdev = dev->dev_private;
241 struct virtio_gpu_framebuffer *vgfb;
242 struct virtio_gpu_object *bo;
247 vgfb = to_virtio_gpu_framebuffer(new_state->fb);
248 bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
249 if (!bo || (plane->type == DRM_PLANE_TYPE_PRIMARY && !bo->guest_blob))
252 if (bo->dumb && (plane->state->fb != new_state->fb)) {
253 vgfb->fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context,
262 static void virtio_gpu_plane_cleanup_fb(struct drm_plane *plane,
263 struct drm_plane_state *state)
265 struct virtio_gpu_framebuffer *vgfb;
270 vgfb = to_virtio_gpu_framebuffer(state->fb);
272 dma_fence_put(&vgfb->fence->f);
277 static void virtio_gpu_cursor_plane_update(struct drm_plane *plane,
278 struct drm_atomic_state *state)
280 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
282 struct drm_device *dev = plane->dev;
283 struct virtio_gpu_device *vgdev = dev->dev_private;
284 struct virtio_gpu_output *output = NULL;
285 struct virtio_gpu_framebuffer *vgfb;
286 struct virtio_gpu_object *bo = NULL;
289 if (plane->state->crtc)
290 output = drm_crtc_to_virtio_gpu_output(plane->state->crtc);
292 output = drm_crtc_to_virtio_gpu_output(old_state->crtc);
293 if (WARN_ON(!output))
296 if (plane->state->fb) {
297 vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
298 bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
299 handle = bo->hw_res_handle;
304 if (bo && bo->dumb && (plane->state->fb != old_state->fb)) {
305 /* new cursor -- update & wait */
306 struct virtio_gpu_object_array *objs;
308 objs = virtio_gpu_array_alloc(1);
311 virtio_gpu_array_add_obj(objs, vgfb->base.obj[0]);
312 virtio_gpu_array_lock_resv(objs);
313 virtio_gpu_cmd_transfer_to_host_2d
315 plane->state->crtc_w,
316 plane->state->crtc_h,
317 0, 0, objs, vgfb->fence);
318 virtio_gpu_notify(vgdev);
319 dma_fence_wait(&vgfb->fence->f, true);
320 dma_fence_put(&vgfb->fence->f);
324 if (plane->state->fb != old_state->fb) {
325 DRM_DEBUG("update, handle %d, pos +%d+%d, hot %d,%d\n", handle,
326 plane->state->crtc_x,
327 plane->state->crtc_y,
328 plane->state->fb ? plane->state->fb->hot_x : 0,
329 plane->state->fb ? plane->state->fb->hot_y : 0);
330 output->cursor.hdr.type =
331 cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR);
332 output->cursor.resource_id = cpu_to_le32(handle);
333 if (plane->state->fb) {
334 output->cursor.hot_x =
335 cpu_to_le32(plane->state->fb->hot_x);
336 output->cursor.hot_y =
337 cpu_to_le32(plane->state->fb->hot_y);
339 output->cursor.hot_x = cpu_to_le32(0);
340 output->cursor.hot_y = cpu_to_le32(0);
343 DRM_DEBUG("move +%d+%d\n",
344 plane->state->crtc_x,
345 plane->state->crtc_y);
346 output->cursor.hdr.type =
347 cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR);
349 output->cursor.pos.x = cpu_to_le32(plane->state->crtc_x);
350 output->cursor.pos.y = cpu_to_le32(plane->state->crtc_y);
351 virtio_gpu_cursor_ping(vgdev, output);
354 static const struct drm_plane_helper_funcs virtio_gpu_primary_helper_funcs = {
355 .prepare_fb = virtio_gpu_plane_prepare_fb,
356 .cleanup_fb = virtio_gpu_plane_cleanup_fb,
357 .atomic_check = virtio_gpu_plane_atomic_check,
358 .atomic_update = virtio_gpu_primary_plane_update,
361 static const struct drm_plane_helper_funcs virtio_gpu_cursor_helper_funcs = {
362 .prepare_fb = virtio_gpu_plane_prepare_fb,
363 .cleanup_fb = virtio_gpu_plane_cleanup_fb,
364 .atomic_check = virtio_gpu_plane_atomic_check,
365 .atomic_update = virtio_gpu_cursor_plane_update,
368 struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev,
369 enum drm_plane_type type,
372 struct drm_device *dev = vgdev->ddev;
373 const struct drm_plane_helper_funcs *funcs;
374 struct drm_plane *plane;
375 const uint32_t *formats;
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;
383 formats = virtio_gpu_formats;
384 nformats = ARRAY_SIZE(virtio_gpu_formats);
385 funcs = &virtio_gpu_primary_helper_funcs;
388 plane = drmm_universal_plane_alloc(dev, struct drm_plane, dev,
389 1 << index, &virtio_gpu_plane_funcs,
390 formats, nformats, NULL, type, NULL);
394 drm_plane_helper_add(plane, funcs);