xref: /linux/drivers/gpu/drm/virtio/virtgpu_plane.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
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 
26 #include <drm/drm_atomic_helper.h>
27 #include <drm/drm_blend.h>
28 #include <drm/drm_damage_helper.h>
29 #include <drm/drm_fourcc.h>
30 #include <drm/drm_gem_atomic_helper.h>
31 #include <linux/virtio_dma_buf.h>
32 #include <drm/drm_managed.h>
33 #include <drm/drm_panic.h>
34 #include <drm/drm_panic_helper.h>
35 #include <drm/drm_print.h>
36 
37 #include "virtgpu_drv.h"
38 
39 static const uint32_t virtio_gpu_formats[] = {
40 	DRM_FORMAT_HOST_XRGB8888,
41 	DRM_FORMAT_HOST_ARGB8888,
42 };
43 
44 static const uint32_t virtio_gpu_cursor_formats[] = {
45 	DRM_FORMAT_HOST_ARGB8888,
46 };
47 
48 uint32_t virtio_gpu_translate_format(uint32_t drm_fourcc)
49 {
50 	uint32_t format;
51 
52 	switch (drm_fourcc) {
53 	case DRM_FORMAT_XRGB8888:
54 		format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM;
55 		break;
56 	case DRM_FORMAT_ARGB8888:
57 		format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM;
58 		break;
59 	case DRM_FORMAT_BGRX8888:
60 		format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM;
61 		break;
62 	case DRM_FORMAT_BGRA8888:
63 		format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM;
64 		break;
65 	default:
66 		/*
67 		 * This should not happen, we handle everything listed
68 		 * in virtio_gpu_formats[].
69 		 */
70 		format = 0;
71 		break;
72 	}
73 	WARN_ON(format == 0);
74 	return format;
75 }
76 
77 static struct
78 drm_plane_state *virtio_gpu_plane_duplicate_state(struct drm_plane *plane)
79 {
80 	struct virtio_gpu_plane_state *new;
81 
82 	if (WARN_ON(!plane->state))
83 		return NULL;
84 
85 	new = kzalloc_obj(*new);
86 	if (!new)
87 		return NULL;
88 
89 	__drm_atomic_helper_plane_duplicate_state(plane, &new->base);
90 
91 	return &new->base;
92 }
93 
94 static const struct drm_plane_funcs virtio_gpu_plane_funcs = {
95 	.update_plane		= drm_atomic_helper_update_plane,
96 	.disable_plane		= drm_atomic_helper_disable_plane,
97 	.atomic_create_state = drm_atomic_helper_plane_create_state,
98 	.atomic_duplicate_state = virtio_gpu_plane_duplicate_state,
99 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
100 	DRM_PANIC_PLANE_FUNCS,
101 };
102 
103 static int virtio_gpu_plane_atomic_check(struct drm_plane *plane,
104 					 struct drm_atomic_commit *state)
105 {
106 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
107 										 plane);
108 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state,
109 										 plane);
110 	struct virtio_gpu_object *bo;
111 	bool is_cursor = plane->type == DRM_PLANE_TYPE_CURSOR;
112 	struct drm_crtc_state *crtc_state;
113 	int ret;
114 
115 	if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc))
116 		return 0;
117 
118 	/*
119 	 * Ignore damage clips if the framebuffer attached to the plane's state
120 	 * has changed since the last plane update (page-flip). In this case, a
121 	 * full plane update should happen for dumb buffers because uploads are
122 	 * done per-buffer. Rendered resources are already coherent on the host,
123 	 * so preserve userspace's accumulated per-buffer damage for those.
124 	 */
125 	bo = gem_to_virtio_gpu_obj(new_plane_state->fb->obj[0]);
126 	new_plane_state->ignore_damage_clips =
127 		old_plane_state->fb != new_plane_state->fb && bo->dumb;
128 
129 	crtc_state = drm_atomic_get_crtc_state(state,
130 					       new_plane_state->crtc);
131 	if (IS_ERR(crtc_state))
132 		return PTR_ERR(crtc_state);
133 
134 	ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
135 						  DRM_PLANE_NO_SCALING,
136 						  DRM_PLANE_NO_SCALING,
137 						  is_cursor, true);
138 	return ret;
139 }
140 
141 /* For drm panic */
142 static int virtio_gpu_panic_update_dumb_bo(struct virtio_gpu_device *vgdev,
143 					   struct drm_plane_state *state,
144 					   struct drm_rect *rect)
145 {
146 	struct virtio_gpu_object *bo =
147 		gem_to_virtio_gpu_obj(state->fb->obj[0]);
148 	struct virtio_gpu_object_array *objs;
149 	uint32_t w = rect->x2 - rect->x1;
150 	uint32_t h = rect->y2 - rect->y1;
151 	uint32_t x = rect->x1;
152 	uint32_t y = rect->y1;
153 	uint32_t off = x * state->fb->format->cpp[0] +
154 		y * state->fb->pitches[0];
155 
156 	objs = virtio_gpu_panic_array_alloc();
157 	if (!objs)
158 		return -ENOMEM;
159 	virtio_gpu_array_add_obj(objs, &bo->base.base);
160 
161 	return virtio_gpu_panic_cmd_transfer_to_host_2d(vgdev, off, w, h, x, y,
162 							objs);
163 }
164 
165 static void virtio_gpu_update_dumb_bo(struct virtio_gpu_device *vgdev,
166 				      struct drm_plane_state *state,
167 				      struct drm_rect *rect)
168 {
169 	struct virtio_gpu_object *bo =
170 		gem_to_virtio_gpu_obj(state->fb->obj[0]);
171 	struct virtio_gpu_object_array *objs;
172 	uint32_t w = rect->x2 - rect->x1;
173 	uint32_t h = rect->y2 - rect->y1;
174 	uint32_t x = rect->x1;
175 	uint32_t y = rect->y1;
176 	uint32_t off = x * state->fb->format->cpp[0] +
177 		y * state->fb->pitches[0];
178 
179 	objs = virtio_gpu_array_alloc(1);
180 	if (!objs)
181 		return;
182 	virtio_gpu_array_add_obj(objs, &bo->base.base);
183 
184 	virtio_gpu_cmd_transfer_to_host_2d(vgdev, off, w, h, x, y,
185 					   objs, NULL);
186 }
187 
188 /* For drm_panic */
189 static void virtio_gpu_panic_resource_flush(struct drm_plane *plane,
190 					    uint32_t x, uint32_t y,
191 					    uint32_t width, uint32_t height)
192 {
193 	struct drm_device *dev = plane->dev;
194 	struct virtio_gpu_device *vgdev = dev->dev_private;
195 	struct virtio_gpu_framebuffer *vgfb;
196 	struct virtio_gpu_object *bo;
197 
198 	vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
199 	bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
200 
201 	virtio_gpu_panic_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y,
202 					    width, height);
203 	virtio_gpu_panic_notify(vgdev);
204 }
205 
206 static void virtio_gpu_resource_flush(struct drm_plane *plane,
207 				      uint32_t x, uint32_t y,
208 				      uint32_t width, uint32_t height)
209 {
210 	struct drm_device *dev = plane->dev;
211 	struct virtio_gpu_device *vgdev = dev->dev_private;
212 	struct virtio_gpu_framebuffer *vgfb;
213 	struct virtio_gpu_plane_state *vgplane_st;
214 	struct virtio_gpu_object *bo;
215 
216 	vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
217 	vgplane_st = to_virtio_gpu_plane_state(plane->state);
218 	bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
219 	if (vgplane_st->fence) {
220 		struct virtio_gpu_object_array *objs;
221 
222 		objs = virtio_gpu_array_alloc(1);
223 		if (!objs)
224 			return;
225 		virtio_gpu_array_add_obj(objs, vgfb->base.obj[0]);
226 		if (virtio_gpu_lock_one_resv_uninterruptible(objs)) {
227 			virtio_gpu_array_put_free(objs);
228 			return;
229 		}
230 		virtio_gpu_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y,
231 					      width, height, objs,
232 					      vgplane_st->fence);
233 		virtio_gpu_notify(vgdev);
234 		dma_fence_wait_timeout(&vgplane_st->fence->f, true,
235 				       msecs_to_jiffies(50));
236 	} else {
237 		virtio_gpu_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y,
238 					      width, height, NULL, NULL);
239 		virtio_gpu_notify(vgdev);
240 	}
241 }
242 
243 static void virtio_gpu_primary_plane_update(struct drm_plane *plane,
244 					    struct drm_atomic_commit *state)
245 {
246 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
247 									   plane);
248 	struct drm_device *dev = plane->dev;
249 	struct virtio_gpu_device *vgdev = dev->dev_private;
250 	struct virtio_gpu_output *output = NULL;
251 	struct virtio_gpu_object *bo;
252 	struct drm_rect rect;
253 
254 	if (plane->state->crtc)
255 		output = drm_crtc_to_virtio_gpu_output(plane->state->crtc);
256 	if (old_state->crtc)
257 		output = drm_crtc_to_virtio_gpu_output(old_state->crtc);
258 	if (WARN_ON(!output))
259 		return;
260 
261 	if (!plane->state->fb || !output->crtc.state->active) {
262 		DRM_DEBUG("nofb\n");
263 		virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
264 					   plane->state->src_w >> 16,
265 					   plane->state->src_h >> 16,
266 					   0, 0);
267 		virtio_gpu_notify(vgdev);
268 		return;
269 	}
270 
271 	if (!drm_atomic_helper_damage_merged(old_state, plane->state, &rect))
272 		return;
273 
274 	bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
275 	if (bo->dumb)
276 		virtio_gpu_update_dumb_bo(vgdev, plane->state, &rect);
277 
278 	if (plane->state->fb != old_state->fb ||
279 	    plane->state->src_w != old_state->src_w ||
280 	    plane->state->src_h != old_state->src_h ||
281 	    plane->state->src_x != old_state->src_x ||
282 	    plane->state->src_y != old_state->src_y ||
283 	    output->needs_modeset) {
284 		output->needs_modeset = false;
285 		DRM_DEBUG("handle 0x%x, crtc %dx%d+%d+%d, src %dx%d+%d+%d\n",
286 			  bo->hw_res_handle,
287 			  plane->state->crtc_w, plane->state->crtc_h,
288 			  plane->state->crtc_x, plane->state->crtc_y,
289 			  plane->state->src_w >> 16,
290 			  plane->state->src_h >> 16,
291 			  plane->state->src_x >> 16,
292 			  plane->state->src_y >> 16);
293 
294 		if (bo->host3d_blob || bo->guest_blob) {
295 			virtio_gpu_cmd_set_scanout_blob
296 						(vgdev, output->index, bo,
297 						 plane->state->fb,
298 						 plane->state->src_w >> 16,
299 						 plane->state->src_h >> 16,
300 						 plane->state->src_x >> 16,
301 						 plane->state->src_y >> 16);
302 		} else {
303 			virtio_gpu_cmd_set_scanout(vgdev, output->index,
304 						   bo->hw_res_handle,
305 						   plane->state->src_w >> 16,
306 						   plane->state->src_h >> 16,
307 						   plane->state->src_x >> 16,
308 						   plane->state->src_y >> 16);
309 		}
310 	}
311 
312 	virtio_gpu_resource_flush(plane,
313 				  rect.x1,
314 				  rect.y1,
315 				  rect.x2 - rect.x1,
316 				  rect.y2 - rect.y1);
317 }
318 
319 static int virtio_gpu_prepare_imported_obj(struct drm_plane *plane,
320 					   struct drm_plane_state *new_state,
321 					   struct drm_gem_object *obj)
322 {
323 	struct virtio_gpu_device *vgdev = plane->dev->dev_private;
324 	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
325 	struct dma_buf_attachment *attach = obj->import_attach;
326 	struct dma_resv *resv = attach->dmabuf->resv;
327 	struct virtio_gpu_mem_entry *ents = NULL;
328 	unsigned int nents;
329 	int ret;
330 
331 	dma_resv_lock(resv, NULL);
332 
333 	ret = dma_buf_pin(attach);
334 	if (ret) {
335 		dma_resv_unlock(resv);
336 		return ret;
337 	}
338 
339 	if (!bo->sgt) {
340 		ret = virtgpu_dma_buf_import_sgt(&ents, &nents,
341 						 bo, attach);
342 		if (ret)
343 			goto err;
344 
345 		virtio_gpu_object_attach(vgdev, bo, ents, nents);
346 	}
347 
348 	dma_resv_unlock(resv);
349 	return 0;
350 
351 err:
352 	dma_buf_unpin(attach);
353 	dma_resv_unlock(resv);
354 	return ret;
355 }
356 
357 static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
358 				       struct drm_plane_state *new_state)
359 {
360 	struct drm_device *dev = plane->dev;
361 	struct virtio_gpu_device *vgdev = dev->dev_private;
362 	struct virtio_gpu_framebuffer *vgfb;
363 	struct virtio_gpu_plane_state *vgplane_st;
364 	struct virtio_gpu_object *bo;
365 	struct drm_gem_object *obj;
366 	int ret;
367 
368 	if (!new_state->fb)
369 		return 0;
370 
371 	vgfb = to_virtio_gpu_framebuffer(new_state->fb);
372 	vgplane_st = to_virtio_gpu_plane_state(new_state);
373 	bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
374 
375 	drm_gem_plane_helper_prepare_fb(plane, new_state);
376 
377 	if (!bo || (plane->type == DRM_PLANE_TYPE_PRIMARY && !bo->guest_blob))
378 		return 0;
379 
380 	obj = new_state->fb->obj[0];
381 	if (bo->dumb || drm_gem_is_imported(obj)) {
382 		vgplane_st->fence = virtio_gpu_fence_alloc(vgdev,
383 						     vgdev->fence_drv.context,
384 						     0);
385 		if (!vgplane_st->fence)
386 			return -ENOMEM;
387 	}
388 
389 	if (drm_gem_is_imported(obj)) {
390 		ret = virtio_gpu_prepare_imported_obj(plane, new_state, obj);
391 		if (ret)
392 			goto err_fence;
393 	}
394 
395 	return 0;
396 
397 err_fence:
398 	if (vgplane_st->fence) {
399 		dma_fence_put(&vgplane_st->fence->f);
400 		vgplane_st->fence = NULL;
401 	}
402 
403 	return ret;
404 }
405 
406 static void virtio_gpu_cleanup_imported_obj(struct drm_gem_object *obj)
407 {
408 	struct dma_buf_attachment *attach = obj->import_attach;
409 	struct dma_resv *resv = attach->dmabuf->resv;
410 
411 	dma_resv_lock(resv, NULL);
412 	dma_buf_unpin(attach);
413 	dma_resv_unlock(resv);
414 }
415 
416 static void virtio_gpu_plane_cleanup_fb(struct drm_plane *plane,
417 					struct drm_plane_state *state)
418 {
419 	struct virtio_gpu_plane_state *vgplane_st;
420 	struct drm_gem_object *obj;
421 
422 	if (!state->fb)
423 		return;
424 
425 	vgplane_st = to_virtio_gpu_plane_state(state);
426 	if (vgplane_st->fence) {
427 		dma_fence_put(&vgplane_st->fence->f);
428 		vgplane_st->fence = NULL;
429 	}
430 
431 	obj = state->fb->obj[0];
432 	if (drm_gem_is_imported(obj))
433 		virtio_gpu_cleanup_imported_obj(obj);
434 }
435 
436 static void virtio_gpu_cursor_plane_update(struct drm_plane *plane,
437 					   struct drm_atomic_commit *state)
438 {
439 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
440 									   plane);
441 	struct drm_device *dev = plane->dev;
442 	struct virtio_gpu_device *vgdev = dev->dev_private;
443 	struct virtio_gpu_output *output = NULL;
444 	struct virtio_gpu_framebuffer *vgfb;
445 	struct virtio_gpu_plane_state *vgplane_st;
446 	struct virtio_gpu_object *bo = NULL;
447 	uint32_t handle;
448 
449 	if (plane->state->crtc)
450 		output = drm_crtc_to_virtio_gpu_output(plane->state->crtc);
451 	if (old_state->crtc)
452 		output = drm_crtc_to_virtio_gpu_output(old_state->crtc);
453 	if (WARN_ON(!output))
454 		return;
455 
456 	if (plane->state->fb) {
457 		vgfb = to_virtio_gpu_framebuffer(plane->state->fb);
458 		vgplane_st = to_virtio_gpu_plane_state(plane->state);
459 		bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
460 		handle = bo->hw_res_handle;
461 	} else {
462 		handle = 0;
463 	}
464 
465 	if (bo && bo->dumb && (plane->state->fb != old_state->fb)) {
466 		/* new cursor -- update & wait */
467 		struct virtio_gpu_object_array *objs;
468 
469 		objs = virtio_gpu_array_alloc(1);
470 		if (!objs)
471 			return;
472 		virtio_gpu_array_add_obj(objs, vgfb->base.obj[0]);
473 		if (virtio_gpu_lock_one_resv_uninterruptible(objs)) {
474 			virtio_gpu_array_put_free(objs);
475 			return;
476 		}
477 		virtio_gpu_cmd_transfer_to_host_2d
478 			(vgdev, 0,
479 			 plane->state->crtc_w,
480 			 plane->state->crtc_h,
481 			 0, 0, objs, vgplane_st->fence);
482 		virtio_gpu_notify(vgdev);
483 		dma_fence_wait(&vgplane_st->fence->f, true);
484 	}
485 
486 	if (plane->state->fb != old_state->fb) {
487 		DRM_DEBUG("update, handle %d, pos +%d+%d, hot %d,%d\n", handle,
488 			  plane->state->crtc_x,
489 			  plane->state->crtc_y,
490 			  plane->state->hotspot_x,
491 			  plane->state->hotspot_y);
492 		output->cursor.hdr.type =
493 			cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR);
494 		output->cursor.resource_id = cpu_to_le32(handle);
495 		if (plane->state->fb) {
496 			output->cursor.hot_x =
497 				cpu_to_le32(plane->state->hotspot_x);
498 			output->cursor.hot_y =
499 				cpu_to_le32(plane->state->hotspot_y);
500 		} else {
501 			output->cursor.hot_x = cpu_to_le32(0);
502 			output->cursor.hot_y = cpu_to_le32(0);
503 		}
504 	} else {
505 		DRM_DEBUG("move +%d+%d\n",
506 			  plane->state->crtc_x,
507 			  plane->state->crtc_y);
508 		output->cursor.hdr.type =
509 			cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR);
510 	}
511 	output->cursor.pos.x = cpu_to_le32(plane->state->crtc_x);
512 	output->cursor.pos.y = cpu_to_le32(plane->state->crtc_y);
513 	virtio_gpu_cursor_ping(vgdev, output);
514 }
515 
516 static int virtio_drm_get_scanout_buffer(struct drm_plane *plane,
517 					 struct drm_scanout_buffer *sb)
518 {
519 	struct virtio_gpu_object *bo;
520 
521 	if (!plane->state || !plane->state->fb || !plane->state->visible)
522 		return -ENODEV;
523 
524 	bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
525 
526 	if (virtio_gpu_is_vram(bo) || drm_gem_is_imported(&bo->base.base))
527 		return -ENODEV;
528 
529 	if (bo->base.vaddr) {
530 		iosys_map_set_vaddr(&sb->map[0], bo->base.vaddr);
531 	} else {
532 		struct drm_gem_shmem_object *shmem = &bo->base;
533 
534 		if (!shmem->pages)
535 			return -ENODEV;
536 		/* map scanout buffer later */
537 		sb->pages = shmem->pages;
538 	}
539 
540 	sb->format = plane->state->fb->format;
541 	sb->height = plane->state->fb->height;
542 	sb->width = plane->state->fb->width;
543 	sb->pitch[0] = plane->state->fb->pitches[0];
544 	return 0;
545 }
546 
547 static void virtio_panic_flush(struct drm_plane *plane)
548 {
549 	struct virtio_gpu_object *bo;
550 	struct drm_device *dev = plane->dev;
551 	struct virtio_gpu_device *vgdev = dev->dev_private;
552 	struct drm_rect rect;
553 
554 	rect.x1 = 0;
555 	rect.y1 = 0;
556 	rect.x2 = plane->state->fb->width;
557 	rect.y2 = plane->state->fb->height;
558 
559 	bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]);
560 
561 	if (bo->dumb) {
562 		if (virtio_gpu_panic_update_dumb_bo(vgdev, plane->state,
563 						    &rect))
564 			return;
565 	}
566 
567 	virtio_gpu_panic_resource_flush(plane,
568 					plane->state->src_x >> 16,
569 					plane->state->src_y >> 16,
570 					plane->state->src_w >> 16,
571 					plane->state->src_h >> 16);
572 }
573 
574 static const struct drm_plane_helper_funcs virtio_gpu_primary_helper_funcs = {
575 	.prepare_fb		= virtio_gpu_plane_prepare_fb,
576 	.cleanup_fb		= virtio_gpu_plane_cleanup_fb,
577 	.atomic_check		= virtio_gpu_plane_atomic_check,
578 	.atomic_update		= virtio_gpu_primary_plane_update,
579 	.get_scanout_buffer	= virtio_drm_get_scanout_buffer,
580 	.panic_flush		= virtio_panic_flush,
581 };
582 
583 static const struct drm_plane_helper_funcs virtio_gpu_cursor_helper_funcs = {
584 	.prepare_fb		= virtio_gpu_plane_prepare_fb,
585 	.cleanup_fb		= virtio_gpu_plane_cleanup_fb,
586 	.atomic_check		= virtio_gpu_plane_atomic_check,
587 	.atomic_update		= virtio_gpu_cursor_plane_update,
588 };
589 
590 struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev,
591 					enum drm_plane_type type,
592 					int index)
593 {
594 	struct drm_device *dev = vgdev->ddev;
595 	const struct drm_plane_helper_funcs *funcs;
596 	struct drm_plane *plane;
597 	const uint32_t *formats;
598 	int nformats;
599 
600 	if (type == DRM_PLANE_TYPE_CURSOR) {
601 		formats = virtio_gpu_cursor_formats;
602 		nformats = ARRAY_SIZE(virtio_gpu_cursor_formats);
603 		funcs = &virtio_gpu_cursor_helper_funcs;
604 	} else {
605 		formats = virtio_gpu_formats;
606 		nformats = ARRAY_SIZE(virtio_gpu_formats);
607 		funcs = &virtio_gpu_primary_helper_funcs;
608 	}
609 
610 	plane = drmm_universal_plane_alloc(dev, struct drm_plane, dev,
611 					   1 << index, &virtio_gpu_plane_funcs,
612 					   formats, nformats, NULL, type, NULL);
613 	if (IS_ERR(plane))
614 		return plane;
615 
616 	drm_plane_helper_add(plane, funcs);
617 
618 	if (type == DRM_PLANE_TYPE_PRIMARY)
619 		drm_plane_enable_fb_damage_clips(plane);
620 	else if (type == DRM_PLANE_TYPE_CURSOR)
621 		drm_plane_create_blend_mode_property(plane,
622 						     BIT(DRM_MODE_BLEND_PREMULTI));
623 
624 	return plane;
625 }
626