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