1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 VeriSilicon Holdings Co., Ltd.
6 #include <drm/drm_atomic.h>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_plane_helper.h>
9 #include <drm/drm_fb_cma_helper.h>
10 #include <drm/drm_gem_cma_helper.h>
11 #include <drm/vs_drm.h>
19 void vs_plane_destory(struct drm_plane *plane)
21 struct vs_plane *vs_plane = to_vs_plane(plane);
23 drm_plane_cleanup(plane);
27 static void vs_plane_reset(struct drm_plane *plane)
29 struct vs_plane_state *state;
30 struct vs_plane *vs_plane = to_vs_plane(plane);
33 __drm_atomic_helper_plane_destroy_state(plane->state);
35 state = to_vs_plane_state(plane->state);
40 state = kzalloc(sizeof(*state), GFP_KERNEL);
44 __drm_atomic_helper_plane_reset(plane, &state->base);
46 state->degamma = VS_DEGAMMA_DISABLE;
47 state->degamma_changed = false;
48 state->base.zpos = vs_plane->id;
50 memset(&state->status, 0, sizeof(state->status));
53 static void _vs_plane_duplicate_blob(struct vs_plane_state *state,
54 struct vs_plane_state *ori_state)
56 state->watermark = ori_state->watermark;
57 state->color_mgmt = ori_state->color_mgmt;
58 state->roi = ori_state->roi;
61 drm_property_blob_get(state->watermark);
62 if (state->color_mgmt)
63 drm_property_blob_get(state->color_mgmt);
65 drm_property_blob_get(state->roi);
69 _vs_plane_set_property_blob_from_id(struct drm_device *dev,
70 struct drm_property_blob **blob,
74 struct drm_property_blob *new_blob = NULL;
77 new_blob = drm_property_lookup_blob(dev, blob_id);
81 if (new_blob->length != expected_size) {
82 drm_property_blob_put(new_blob);
87 drm_property_replace_blob(blob, new_blob);
88 drm_property_blob_put(new_blob);
93 static struct drm_plane_state *
94 vs_plane_atomic_duplicate_state(struct drm_plane *plane)
96 struct vs_plane_state *ori_state;
97 struct vs_plane_state *state;
99 if (WARN_ON(!plane->state))
102 ori_state = to_vs_plane_state(plane->state);
103 state = kzalloc(sizeof(*state), GFP_KERNEL);
107 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
109 state->degamma = ori_state->degamma;
110 state->degamma_changed = ori_state->degamma_changed;
112 _vs_plane_duplicate_blob(state, ori_state);
113 memcpy(&state->status, &ori_state->status, sizeof(ori_state->status));
118 static void vs_plane_atomic_destroy_state(struct drm_plane *plane,
119 struct drm_plane_state *state)
121 struct vs_plane_state *vs_plane_state = to_vs_plane_state(state);
123 __drm_atomic_helper_plane_destroy_state(state);
125 drm_property_blob_put(vs_plane_state->watermark);
126 drm_property_blob_put(vs_plane_state->color_mgmt);
127 drm_property_blob_put(vs_plane_state->roi);
128 kfree(vs_plane_state);
131 static int vs_plane_atomic_set_property(struct drm_plane *plane,
132 struct drm_plane_state *state,
133 struct drm_property *property,
136 struct drm_device *dev = plane->dev;
137 struct vs_plane *vs_plane = to_vs_plane(plane);
138 struct vs_plane_state *vs_plane_state = to_vs_plane_state(state);
141 if (property == vs_plane->degamma_mode) {
142 if (vs_plane_state->degamma != val) {
143 vs_plane_state->degamma = val;
144 vs_plane_state->degamma_changed = true;
146 vs_plane_state->degamma_changed = false;
148 } else if (property == vs_plane->watermark_prop) {
149 ret = _vs_plane_set_property_blob_from_id(dev,
150 &vs_plane_state->watermark,
151 val, sizeof(struct drm_vs_watermark));
153 } else if (property == vs_plane->color_mgmt_prop) {
154 ret = _vs_plane_set_property_blob_from_id(dev,
155 &vs_plane_state->color_mgmt,
156 val, sizeof(struct drm_vs_color_mgmt));
158 } else if (property == vs_plane->roi_prop) {
159 ret = _vs_plane_set_property_blob_from_id(dev,
160 &vs_plane_state->roi,
161 val, sizeof(struct drm_vs_roi));
170 static int vs_plane_atomic_get_property(struct drm_plane *plane,
171 const struct drm_plane_state *state,
172 struct drm_property *property,
175 struct vs_plane *vs_plane = to_vs_plane(plane);
176 const struct vs_plane_state *vs_plane_state =
177 container_of(state, const struct vs_plane_state, base);
179 if (property == vs_plane->degamma_mode)
180 *val = vs_plane_state->degamma;
181 else if (property == vs_plane->watermark_prop)
182 *val = (vs_plane_state->watermark) ?
183 vs_plane_state->watermark->base.id : 0;
184 else if (property == vs_plane->color_mgmt_prop)
185 *val = (vs_plane_state->color_mgmt) ?
186 vs_plane_state->color_mgmt->base.id : 0;
187 else if (property == vs_plane->roi_prop)
188 *val = (vs_plane_state->roi) ?
189 vs_plane_state->roi->base.id : 0;
196 static bool vs_format_mod_supported(struct drm_plane *plane,
203 * We always have to allow these modifiers:
204 * 1. Core DRM checks for LINEAR support if userspace does not provide modifiers.
205 * 2. Not passing any modifiers is the same as explicitly passing INVALID.
207 if (modifier == DRM_FORMAT_MOD_LINEAR) {
211 /* Check that the modifier is on the list of the plane's supported modifiers. */
212 for (i = 0; i < plane->modifier_count; i++) {
213 if (modifier == plane->modifiers[i])
216 if (i == plane->modifier_count)
223 const struct drm_plane_funcs vs_plane_funcs = {
224 .update_plane = drm_atomic_helper_update_plane,
225 .disable_plane = drm_atomic_helper_disable_plane,
226 .destroy = vs_plane_destory,
227 .reset = vs_plane_reset,
228 .atomic_duplicate_state = vs_plane_atomic_duplicate_state,
229 .atomic_destroy_state = vs_plane_atomic_destroy_state,
230 .atomic_set_property = vs_plane_atomic_set_property,
231 .atomic_get_property = vs_plane_atomic_get_property,
232 .format_mod_supported = vs_format_mod_supported,
235 static unsigned char vs_get_plane_number(struct drm_framebuffer *fb)
237 const struct drm_format_info *info;
242 info = drm_format_info(fb->format->format);
243 if (!info || info->num_planes > MAX_NUM_PLANES)
246 return info->num_planes;
250 static int vs_plane_atomic_check(struct drm_plane *plane,
251 struct drm_atomic_state *state)
253 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
255 struct vs_plane *vs_plane = to_vs_plane(plane);
256 struct drm_framebuffer *fb = new_plane_state->fb;
257 struct drm_crtc *crtc = new_plane_state->crtc;
258 struct vs_crtc *vs_crtc = to_vs_crtc(crtc);
263 //return vs_plane->funcs->check(vs_crtc->dev, vs_plane, new_plane_state);
264 return vs_plane->funcs->check(vs_crtc->dev, plane, state);
267 static void vs_plane_atomic_update(struct drm_plane *plane,
268 struct drm_atomic_state *state)
270 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
272 unsigned char i, num_planes;
273 struct drm_framebuffer *fb;
274 struct vs_plane *vs_plane = to_vs_plane(plane);
275 //struct drm_plane_state *state = plane->state;
276 struct vs_crtc *vs_crtc = to_vs_crtc(new_state->crtc);
277 struct vs_plane_state *plane_state = to_vs_plane_state(new_state);
278 //struct drm_format_name_buf *name = &plane_state->status.format_name;
280 if (!new_state->fb || !new_state->crtc)
285 num_planes = vs_get_plane_number(fb);
287 for (i = 0; i < num_planes; i++) {
288 struct vs_gem_object *vs_obj;
290 vs_obj = vs_fb_get_gem_obj(fb, i);
291 vs_plane->dma_addr[i] = vs_obj->iova + fb->offsets[i];
294 plane_state->status.src = drm_plane_state_src(new_state);
295 plane_state->status.dest = drm_plane_state_dest(new_state);
296 //drm_get_format_name(fb->format->format, name);
298 vs_plane->funcs->update(vs_crtc->dev, vs_plane, plane, state);
301 static void vs_plane_atomic_disable(struct drm_plane *plane,
302 struct drm_atomic_state *state)
304 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
306 struct vs_plane *vs_plane = to_vs_plane(plane);
307 struct vs_crtc *vs_crtc = to_vs_crtc(old_state->crtc);
309 vs_plane->funcs->disable(vs_crtc->dev, vs_plane, old_state);
312 const struct drm_plane_helper_funcs vs_plane_helper_funcs = {
313 .atomic_check = vs_plane_atomic_check,
314 .atomic_update = vs_plane_atomic_update,
315 .atomic_disable = vs_plane_atomic_disable,
318 static const struct drm_prop_enum_list vs_degamma_mode_enum_list[] = {
319 { VS_DEGAMMA_DISABLE, "disabled" },
320 { VS_DEGAMMA_BT709, "preset degamma for BT709" },
321 { VS_DEGAMMA_BT2020, "preset degamma for BT2020" },
324 struct vs_plane *vs_plane_create(struct drm_device *drm_dev,
325 struct vs_plane_info *info,
326 unsigned int layer_num,
327 unsigned int possible_crtcs)
329 struct vs_plane *plane;
335 plane = kzalloc(sizeof(struct vs_plane), GFP_KERNEL);
339 ret = drm_universal_plane_init(drm_dev, &plane->base, possible_crtcs,
340 &vs_plane_funcs, info->formats,
341 info->num_formats, info->modifiers, info->type,
342 info->name ? info->name : NULL);
346 drm_plane_helper_add(&plane->base, &vs_plane_helper_funcs);
348 /* Set up the plane properties */
349 if (info->degamma_size) {
350 plane->degamma_mode = drm_property_create_enum(drm_dev, 0,
352 vs_degamma_mode_enum_list,
353 ARRAY_SIZE(vs_degamma_mode_enum_list));
355 if (!plane->degamma_mode)
356 goto error_cleanup_plane;
358 drm_object_attach_property(&plane->base.base,
363 if (info->rotation) {
364 ret = drm_plane_create_rotation_property(&plane->base,
368 goto error_cleanup_plane;
371 if (info->blend_mode) {
372 ret = drm_plane_create_blend_mode_property(&plane->base,
375 goto error_cleanup_plane;
376 ret = drm_plane_create_alpha_property(&plane->base);
378 goto error_cleanup_plane;
381 if (info->color_encoding) {
382 ret = drm_plane_create_color_properties(&plane->base,
383 info->color_encoding,
384 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE),
385 DRM_COLOR_YCBCR_BT709,
386 DRM_COLOR_YCBCR_LIMITED_RANGE);
388 goto error_cleanup_plane;
391 if (info->zpos != 255) {
392 ret = drm_plane_create_zpos_property(&plane->base, info->zpos, 0, layer_num - 1);
394 goto error_cleanup_plane;
396 #if KERNEL_VERSION(5, 8, 0) <= LINUX_VERSION_CODE
398 ret = drm_plane_create_zpos_immutable_property(&plane->base,
401 goto error_cleanup_plane;
405 if (info->watermark) {
406 plane->watermark_prop = drm_property_create(drm_dev, DRM_MODE_PROP_BLOB,
408 if (!plane->watermark_prop)
409 goto error_cleanup_plane;
411 drm_object_attach_property(&plane->base.base, plane->watermark_prop, 0);
414 if (info->color_mgmt) {
415 plane->color_mgmt_prop = drm_property_create(drm_dev, DRM_MODE_PROP_BLOB,
417 if (!plane->color_mgmt_prop)
418 goto error_cleanup_plane;
420 drm_object_attach_property(&plane->base.base, plane->color_mgmt_prop, 0);
424 plane->roi_prop = drm_property_create(drm_dev, DRM_MODE_PROP_BLOB,
426 if (!plane->roi_prop)
427 goto error_cleanup_plane;
429 drm_object_attach_property(&plane->base.base, plane->roi_prop, 0);
435 drm_plane_cleanup(&plane->base);