1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/iosys-map.h>
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>
12 #include "vkms_formats.h"
14 static const u32 vkms_formats[] = {
16 DRM_FORMAT_XRGB16161616,
20 static const u32 vkms_plane_formats[] = {
23 DRM_FORMAT_XRGB16161616,
24 DRM_FORMAT_ARGB16161616,
28 static struct drm_plane_state *
29 vkms_plane_duplicate_state(struct drm_plane *plane)
31 struct vkms_plane_state *vkms_state;
32 struct vkms_frame_info *frame_info;
34 vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
38 frame_info = kzalloc(sizeof(*frame_info), GFP_KERNEL);
40 DRM_DEBUG_KMS("Couldn't allocate frame_info\n");
45 vkms_state->frame_info = frame_info;
47 __drm_gem_duplicate_shadow_plane_state(plane, &vkms_state->base);
49 return &vkms_state->base.base;
52 static void vkms_plane_destroy_state(struct drm_plane *plane,
53 struct drm_plane_state *old_state)
55 struct vkms_plane_state *vkms_state = to_vkms_plane_state(old_state);
56 struct drm_crtc *crtc = vkms_state->base.base.crtc;
58 if (crtc && vkms_state->frame_info->fb) {
59 /* dropping the reference we acquired in
60 * vkms_primary_plane_update()
62 if (drm_framebuffer_read_refcount(vkms_state->frame_info->fb))
63 drm_framebuffer_put(vkms_state->frame_info->fb);
66 kfree(vkms_state->frame_info);
67 vkms_state->frame_info = NULL;
69 __drm_gem_destroy_shadow_plane_state(&vkms_state->base);
73 static void vkms_plane_reset(struct drm_plane *plane)
75 struct vkms_plane_state *vkms_state;
78 vkms_plane_destroy_state(plane, plane->state);
79 plane->state = NULL; /* must be set to NULL here */
82 vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
84 DRM_ERROR("Cannot allocate vkms_plane_state\n");
88 __drm_gem_reset_shadow_plane(plane, &vkms_state->base);
91 static const struct drm_plane_funcs vkms_plane_funcs = {
92 .update_plane = drm_atomic_helper_update_plane,
93 .disable_plane = drm_atomic_helper_disable_plane,
94 .reset = vkms_plane_reset,
95 .atomic_duplicate_state = vkms_plane_duplicate_state,
96 .atomic_destroy_state = vkms_plane_destroy_state,
99 static void vkms_plane_atomic_update(struct drm_plane *plane,
100 struct drm_atomic_state *state)
102 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
104 struct vkms_plane_state *vkms_plane_state;
105 struct drm_shadow_plane_state *shadow_plane_state;
106 struct drm_framebuffer *fb = new_state->fb;
107 struct vkms_frame_info *frame_info;
110 if (!new_state->crtc || !fb)
113 fmt = fb->format->format;
114 vkms_plane_state = to_vkms_plane_state(new_state);
115 shadow_plane_state = &vkms_plane_state->base;
117 frame_info = vkms_plane_state->frame_info;
118 memcpy(&frame_info->src, &new_state->src, sizeof(struct drm_rect));
119 memcpy(&frame_info->dst, &new_state->dst, sizeof(struct drm_rect));
121 memcpy(&frame_info->map, &shadow_plane_state->data, sizeof(frame_info->map));
122 drm_framebuffer_get(frame_info->fb);
123 frame_info->offset = fb->offsets[0];
124 frame_info->pitch = fb->pitches[0];
125 frame_info->cpp = fb->format->cpp[0];
126 vkms_plane_state->plane_read = get_frame_to_line_function(fmt);
129 static int vkms_plane_atomic_check(struct drm_plane *plane,
130 struct drm_atomic_state *state)
132 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
134 struct drm_crtc_state *crtc_state;
135 bool can_position = false;
138 if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc))
141 crtc_state = drm_atomic_get_crtc_state(state,
142 new_plane_state->crtc);
143 if (IS_ERR(crtc_state))
144 return PTR_ERR(crtc_state);
146 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
149 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
150 DRM_PLANE_NO_SCALING,
151 DRM_PLANE_NO_SCALING,
159 static int vkms_prepare_fb(struct drm_plane *plane,
160 struct drm_plane_state *state)
162 struct drm_shadow_plane_state *shadow_plane_state;
163 struct drm_framebuffer *fb = state->fb;
169 shadow_plane_state = to_drm_shadow_plane_state(state);
171 ret = drm_gem_plane_helper_prepare_fb(plane, state);
175 return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
178 static void vkms_cleanup_fb(struct drm_plane *plane,
179 struct drm_plane_state *state)
181 struct drm_shadow_plane_state *shadow_plane_state;
182 struct drm_framebuffer *fb = state->fb;
187 shadow_plane_state = to_drm_shadow_plane_state(state);
189 drm_gem_fb_vunmap(fb, shadow_plane_state->map);
192 static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
193 .atomic_update = vkms_plane_atomic_update,
194 .atomic_check = vkms_plane_atomic_check,
195 .prepare_fb = vkms_prepare_fb,
196 .cleanup_fb = vkms_cleanup_fb,
199 struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
200 enum drm_plane_type type, int index)
202 struct drm_device *dev = &vkmsdev->drm;
203 const struct drm_plane_helper_funcs *funcs;
204 struct vkms_plane *plane;
209 case DRM_PLANE_TYPE_PRIMARY:
210 formats = vkms_formats;
211 nformats = ARRAY_SIZE(vkms_formats);
212 funcs = &vkms_primary_helper_funcs;
214 case DRM_PLANE_TYPE_CURSOR:
215 case DRM_PLANE_TYPE_OVERLAY:
216 formats = vkms_plane_formats;
217 nformats = ARRAY_SIZE(vkms_plane_formats);
218 funcs = &vkms_primary_helper_funcs;
221 formats = vkms_formats;
222 nformats = ARRAY_SIZE(vkms_formats);
223 funcs = &vkms_primary_helper_funcs;
227 plane = drmm_universal_plane_alloc(dev, struct vkms_plane, base, 1 << index,
234 drm_plane_helper_add(&plane->base, funcs);