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