xref: /openbmc/linux/drivers/gpu/drm/vkms/vkms_plane.c (revision 8fdf9062)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  */
8 
9 #include "vkms_drv.h"
10 #include <drm/drm_plane_helper.h>
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_gem_framebuffer_helper.h>
14 
15 static struct drm_plane_state *
16 vkms_plane_duplicate_state(struct drm_plane *plane)
17 {
18 	struct vkms_plane_state *vkms_state;
19 	struct vkms_crc_data *crc_data;
20 
21 	vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
22 	if (!vkms_state)
23 		return NULL;
24 
25 	crc_data = kzalloc(sizeof(*crc_data), GFP_KERNEL);
26 	if (!crc_data) {
27 		DRM_DEBUG_KMS("Couldn't allocate crc_data\n");
28 		kfree(vkms_state);
29 		return NULL;
30 	}
31 
32 	vkms_state->crc_data = crc_data;
33 
34 	__drm_atomic_helper_plane_duplicate_state(plane,
35 						  &vkms_state->base);
36 
37 	return &vkms_state->base;
38 }
39 
40 static void vkms_plane_destroy_state(struct drm_plane *plane,
41 				     struct drm_plane_state *old_state)
42 {
43 	struct vkms_plane_state *vkms_state = to_vkms_plane_state(old_state);
44 	struct drm_crtc *crtc = vkms_state->base.crtc;
45 
46 	if (crtc) {
47 		/* dropping the reference we acquired in
48 		 * vkms_primary_plane_update()
49 		 */
50 		if (drm_framebuffer_read_refcount(&vkms_state->crc_data->fb))
51 			drm_framebuffer_put(&vkms_state->crc_data->fb);
52 	}
53 
54 	kfree(vkms_state->crc_data);
55 	vkms_state->crc_data = NULL;
56 
57 	__drm_atomic_helper_plane_destroy_state(old_state);
58 	kfree(vkms_state);
59 }
60 
61 static void vkms_plane_reset(struct drm_plane *plane)
62 {
63 	struct vkms_plane_state *vkms_state;
64 
65 	if (plane->state)
66 		vkms_plane_destroy_state(plane, plane->state);
67 
68 	vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
69 	if (!vkms_state) {
70 		DRM_ERROR("Cannot allocate vkms_plane_state\n");
71 		return;
72 	}
73 
74 	plane->state = &vkms_state->base;
75 	plane->state->plane = plane;
76 }
77 
78 static const struct drm_plane_funcs vkms_plane_funcs = {
79 	.update_plane		= drm_atomic_helper_update_plane,
80 	.disable_plane		= drm_atomic_helper_disable_plane,
81 	.destroy		= drm_plane_cleanup,
82 	.reset			= vkms_plane_reset,
83 	.atomic_duplicate_state = vkms_plane_duplicate_state,
84 	.atomic_destroy_state	= vkms_plane_destroy_state,
85 };
86 
87 static void vkms_plane_atomic_update(struct drm_plane *plane,
88 				     struct drm_plane_state *old_state)
89 {
90 	struct vkms_plane_state *vkms_plane_state;
91 	struct drm_framebuffer *fb = plane->state->fb;
92 	struct vkms_crc_data *crc_data;
93 
94 	if (!plane->state->crtc || !fb)
95 		return;
96 
97 	vkms_plane_state = to_vkms_plane_state(plane->state);
98 
99 	crc_data = vkms_plane_state->crc_data;
100 	memcpy(&crc_data->src, &plane->state->src, sizeof(struct drm_rect));
101 	memcpy(&crc_data->dst, &plane->state->dst, sizeof(struct drm_rect));
102 	memcpy(&crc_data->fb, fb, sizeof(struct drm_framebuffer));
103 	drm_framebuffer_get(&crc_data->fb);
104 	crc_data->offset = fb->offsets[0];
105 	crc_data->pitch = fb->pitches[0];
106 	crc_data->cpp = fb->format->cpp[0];
107 }
108 
109 static int vkms_plane_atomic_check(struct drm_plane *plane,
110 				   struct drm_plane_state *state)
111 {
112 	struct drm_crtc_state *crtc_state;
113 	bool can_position = false;
114 	int ret;
115 
116 	if (!state->fb | !state->crtc)
117 		return 0;
118 
119 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
120 	if (IS_ERR(crtc_state))
121 		return PTR_ERR(crtc_state);
122 
123 	if (plane->type == DRM_PLANE_TYPE_CURSOR)
124 		can_position = true;
125 
126 	ret = drm_atomic_helper_check_plane_state(state, crtc_state,
127 						  DRM_PLANE_HELPER_NO_SCALING,
128 						  DRM_PLANE_HELPER_NO_SCALING,
129 						  can_position, true);
130 	if (ret != 0)
131 		return ret;
132 
133 	/* for now primary plane must be visible and full screen */
134 	if (!state->visible && !can_position)
135 		return -EINVAL;
136 
137 	return 0;
138 }
139 
140 static int vkms_prepare_fb(struct drm_plane *plane,
141 			   struct drm_plane_state *state)
142 {
143 	struct drm_gem_object *gem_obj;
144 	int ret;
145 
146 	if (!state->fb)
147 		return 0;
148 
149 	gem_obj = drm_gem_fb_get_obj(state->fb, 0);
150 	ret = vkms_gem_vmap(gem_obj);
151 	if (ret)
152 		DRM_ERROR("vmap failed: %d\n", ret);
153 
154 	return drm_gem_fb_prepare_fb(plane, state);
155 }
156 
157 static void vkms_cleanup_fb(struct drm_plane *plane,
158 			    struct drm_plane_state *old_state)
159 {
160 	struct drm_gem_object *gem_obj;
161 
162 	if (!old_state->fb)
163 		return;
164 
165 	gem_obj = drm_gem_fb_get_obj(old_state->fb, 0);
166 	vkms_gem_vunmap(gem_obj);
167 }
168 
169 static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
170 	.atomic_update		= vkms_plane_atomic_update,
171 	.atomic_check		= vkms_plane_atomic_check,
172 	.prepare_fb		= vkms_prepare_fb,
173 	.cleanup_fb		= vkms_cleanup_fb,
174 };
175 
176 struct drm_plane *vkms_plane_init(struct vkms_device *vkmsdev,
177 				  enum drm_plane_type type)
178 {
179 	struct drm_device *dev = &vkmsdev->drm;
180 	const struct drm_plane_helper_funcs *funcs;
181 	struct drm_plane *plane;
182 	const u32 *formats;
183 	int ret, nformats;
184 
185 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
186 	if (!plane)
187 		return ERR_PTR(-ENOMEM);
188 
189 	if (type == DRM_PLANE_TYPE_CURSOR) {
190 		formats = vkms_cursor_formats;
191 		nformats = ARRAY_SIZE(vkms_cursor_formats);
192 		funcs = &vkms_primary_helper_funcs;
193 	} else {
194 		formats = vkms_formats;
195 		nformats = ARRAY_SIZE(vkms_formats);
196 		funcs = &vkms_primary_helper_funcs;
197 	}
198 
199 	ret = drm_universal_plane_init(dev, plane, 0,
200 				       &vkms_plane_funcs,
201 				       formats, nformats,
202 				       NULL, type, NULL);
203 	if (ret) {
204 		kfree(plane);
205 		return ERR_PTR(ret);
206 	}
207 
208 	drm_plane_helper_add(plane, funcs);
209 
210 	return plane;
211 }
212