xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/virtio/virtgpu_plane.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: virtgpu_plane.c,v 1.2 2018/08/27 04:58:37 riastradh Exp $	*/
2 
3 /*
4  * Copyright (C) 2015 Red Hat, Inc.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial
17  * portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: virtgpu_plane.c,v 1.2 2018/08/27 04:58:37 riastradh Exp $");
30 
31 #include "virtgpu_drv.h"
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_atomic_helper.h>
34 
35 static const uint32_t virtio_gpu_formats[] = {
36 	DRM_FORMAT_XRGB8888,
37 	DRM_FORMAT_ARGB8888,
38 	DRM_FORMAT_BGRX8888,
39 	DRM_FORMAT_BGRA8888,
40 	DRM_FORMAT_RGBX8888,
41 	DRM_FORMAT_RGBA8888,
42 	DRM_FORMAT_XBGR8888,
43 	DRM_FORMAT_ABGR8888,
44 };
45 
46 static void virtio_gpu_plane_destroy(struct drm_plane *plane)
47 {
48 	kfree(plane);
49 }
50 
51 static const struct drm_plane_funcs virtio_gpu_plane_funcs = {
52 	.update_plane		= drm_atomic_helper_update_plane,
53 	.disable_plane		= drm_atomic_helper_disable_plane,
54 	.destroy		= virtio_gpu_plane_destroy,
55 	.reset			= drm_atomic_helper_plane_reset,
56 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
57 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
58 };
59 
60 static int virtio_gpu_plane_atomic_check(struct drm_plane *plane,
61 					 struct drm_plane_state *state)
62 {
63 	return 0;
64 }
65 
66 static void virtio_gpu_plane_atomic_update(struct drm_plane *plane,
67 					   struct drm_plane_state *old_state)
68 {
69 	struct drm_device *dev = plane->dev;
70 	struct virtio_gpu_device *vgdev = dev->dev_private;
71 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(plane->crtc);
72 	struct virtio_gpu_framebuffer *vgfb;
73 	struct virtio_gpu_object *bo;
74 	uint32_t handle;
75 
76 	if (plane->fb) {
77 		vgfb = to_virtio_gpu_framebuffer(plane->fb);
78 		bo = gem_to_virtio_gpu_obj(vgfb->obj);
79 		handle = bo->hw_res_handle;
80 	} else {
81 		handle = 0;
82 	}
83 
84 	DRM_DEBUG("handle 0x%x, crtc %dx%d+%d+%d\n", handle,
85 		  plane->state->crtc_w, plane->state->crtc_h,
86 		  plane->state->crtc_x, plane->state->crtc_y);
87 	virtio_gpu_cmd_set_scanout(vgdev, output->index, handle,
88 				   plane->state->crtc_w,
89 				   plane->state->crtc_h,
90 				   plane->state->crtc_x,
91 				   plane->state->crtc_y);
92 }
93 
94 
95 static const struct drm_plane_helper_funcs virtio_gpu_plane_helper_funcs = {
96 	.atomic_check		= virtio_gpu_plane_atomic_check,
97 	.atomic_update		= virtio_gpu_plane_atomic_update,
98 };
99 
100 struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev,
101 					int index)
102 {
103 	struct drm_device *dev = vgdev->ddev;
104 	struct drm_plane *plane;
105 	int ret;
106 
107 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
108 	if (!plane)
109 		return ERR_PTR(-ENOMEM);
110 
111 	ret = drm_universal_plane_init(dev, plane, 1 << index,
112 				       &virtio_gpu_plane_funcs,
113 				       virtio_gpu_formats,
114 				       ARRAY_SIZE(virtio_gpu_formats),
115 				       DRM_PLANE_TYPE_PRIMARY);
116 	if (ret)
117 		goto err_plane_init;
118 
119 	drm_plane_helper_add(plane, &virtio_gpu_plane_helper_funcs);
120 	return plane;
121 
122 err_plane_init:
123 	kfree(plane);
124 	return ERR_PTR(ret);
125 }
126