xref: /linux/drivers/gpu/drm/arm/display/komeda/komeda_plane.c (revision 04e84545b9805f6a141fce0b1f05fb74551094fc)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
4  * Author: James.Qian.Wang <james.qian.wang@arm.com>
5  *
6  */
7 #include <drm/drm_atomic.h>
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_plane_helper.h>
10 #include <drm/drm_print.h>
11 #include "komeda_dev.h"
12 #include "komeda_kms.h"
13 #include "komeda_framebuffer.h"
14 
15 static int
16 komeda_plane_init_data_flow(struct drm_plane_state *st,
17 			    struct komeda_data_flow_cfg *dflow)
18 {
19 	struct komeda_plane_state *kplane_st = to_kplane_st(st);
20 	struct drm_framebuffer *fb = st->fb;
21 	const struct komeda_format_caps *caps = to_kfb(fb)->format_caps;
22 
23 	memset(dflow, 0, sizeof(*dflow));
24 
25 	dflow->blending_zorder = st->zpos;
26 
27 	/* if format doesn't have alpha, fix blend mode to PIXEL_NONE */
28 	dflow->pixel_blend_mode = fb->format->has_alpha ?
29 			st->pixel_blend_mode : DRM_MODE_BLEND_PIXEL_NONE;
30 	dflow->layer_alpha = st->alpha >> 8;
31 
32 	dflow->out_x = st->crtc_x;
33 	dflow->out_y = st->crtc_y;
34 	dflow->out_w = st->crtc_w;
35 	dflow->out_h = st->crtc_h;
36 
37 	dflow->in_x = st->src_x >> 16;
38 	dflow->in_y = st->src_y >> 16;
39 	dflow->in_w = st->src_w >> 16;
40 	dflow->in_h = st->src_h >> 16;
41 
42 	dflow->en_img_enhancement = kplane_st->img_enhancement;
43 
44 	dflow->rot = drm_rotation_simplify(st->rotation, caps->supported_rots);
45 	if (!has_bits(dflow->rot, caps->supported_rots)) {
46 		DRM_DEBUG_ATOMIC("rotation(0x%x) isn't supported by %s.\n",
47 				 dflow->rot,
48 				 komeda_get_format_name(caps->fourcc,
49 							fb->modifier));
50 		return -EINVAL;
51 	}
52 
53 	komeda_complete_data_flow_cfg(dflow);
54 
55 	return 0;
56 }
57 
58 /**
59  * komeda_plane_atomic_check - build input data flow
60  * @plane: DRM plane
61  * @state: the plane state object
62  *
63  * RETURNS:
64  * Zero for success or -errno
65  */
66 static int
67 komeda_plane_atomic_check(struct drm_plane *plane,
68 			  struct drm_plane_state *state)
69 {
70 	struct komeda_plane *kplane = to_kplane(plane);
71 	struct komeda_plane_state *kplane_st = to_kplane_st(state);
72 	struct komeda_layer *layer = kplane->layer;
73 	struct drm_crtc_state *crtc_st;
74 	struct komeda_crtc_state *kcrtc_st;
75 	struct komeda_data_flow_cfg dflow;
76 	int err;
77 
78 	if (!state->crtc || !state->fb)
79 		return 0;
80 
81 	crtc_st = drm_atomic_get_crtc_state(state->state, state->crtc);
82 	if (IS_ERR(crtc_st) || !crtc_st->enable) {
83 		DRM_DEBUG_ATOMIC("Cannot update plane on a disabled CRTC.\n");
84 		return -EINVAL;
85 	}
86 
87 	/* crtc is inactive, skip the resource assignment */
88 	if (!crtc_st->active)
89 		return 0;
90 
91 	kcrtc_st = to_kcrtc_st(crtc_st);
92 
93 	err = komeda_plane_init_data_flow(state, &dflow);
94 	if (err)
95 		return err;
96 
97 	err = komeda_build_layer_data_flow(layer, kplane_st, kcrtc_st, &dflow);
98 
99 	return err;
100 }
101 
102 /* plane doesn't represent a real HW, so there is no HW update for plane.
103  * komeda handles all the HW update in crtc->atomic_flush
104  */
105 static void
106 komeda_plane_atomic_update(struct drm_plane *plane,
107 			   struct drm_plane_state *old_state)
108 {
109 }
110 
111 static const struct drm_plane_helper_funcs komeda_plane_helper_funcs = {
112 	.atomic_check	= komeda_plane_atomic_check,
113 	.atomic_update	= komeda_plane_atomic_update,
114 };
115 
116 static void komeda_plane_destroy(struct drm_plane *plane)
117 {
118 	drm_plane_cleanup(plane);
119 
120 	kfree(to_kplane(plane));
121 }
122 
123 static void komeda_plane_reset(struct drm_plane *plane)
124 {
125 	struct komeda_plane_state *state;
126 	struct komeda_plane *kplane = to_kplane(plane);
127 
128 	if (plane->state)
129 		__drm_atomic_helper_plane_destroy_state(plane->state);
130 
131 	kfree(plane->state);
132 	plane->state = NULL;
133 
134 	state = kzalloc(sizeof(*state), GFP_KERNEL);
135 	if (state) {
136 		state->base.rotation = DRM_MODE_ROTATE_0;
137 		state->base.pixel_blend_mode = DRM_MODE_BLEND_PREMULTI;
138 		state->base.alpha = DRM_BLEND_ALPHA_OPAQUE;
139 		state->base.zpos = kplane->layer->base.id;
140 		state->base.color_encoding = DRM_COLOR_YCBCR_BT601;
141 		state->base.color_range = DRM_COLOR_YCBCR_LIMITED_RANGE;
142 		plane->state = &state->base;
143 		plane->state->plane = plane;
144 	}
145 }
146 
147 static struct drm_plane_state *
148 komeda_plane_atomic_duplicate_state(struct drm_plane *plane)
149 {
150 	struct komeda_plane_state *new, *old;
151 
152 	if (WARN_ON(!plane->state))
153 		return NULL;
154 
155 	new = kzalloc(sizeof(*new), GFP_KERNEL);
156 	if (!new)
157 		return NULL;
158 
159 	__drm_atomic_helper_plane_duplicate_state(plane, &new->base);
160 
161 	old = to_kplane_st(plane->state);
162 
163 	new->img_enhancement = old->img_enhancement;
164 
165 	return &new->base;
166 }
167 
168 static void
169 komeda_plane_atomic_destroy_state(struct drm_plane *plane,
170 				  struct drm_plane_state *state)
171 {
172 	__drm_atomic_helper_plane_destroy_state(state);
173 	kfree(to_kplane_st(state));
174 }
175 
176 static int
177 komeda_plane_atomic_get_property(struct drm_plane *plane,
178 				 const struct drm_plane_state *state,
179 				 struct drm_property *property,
180 				 uint64_t *val)
181 {
182 	struct komeda_plane *kplane = to_kplane(plane);
183 	struct komeda_plane_state *st = to_kplane_st(state);
184 
185 	if (property == kplane->prop_img_enhancement)
186 		*val = st->img_enhancement;
187 	else
188 		return -EINVAL;
189 
190 	return 0;
191 }
192 
193 static int
194 komeda_plane_atomic_set_property(struct drm_plane *plane,
195 				 struct drm_plane_state *state,
196 				 struct drm_property *property,
197 				 uint64_t val)
198 {
199 	struct komeda_plane *kplane = to_kplane(plane);
200 	struct komeda_plane_state *st = to_kplane_st(state);
201 
202 	if (property == kplane->prop_img_enhancement)
203 		st->img_enhancement = !!val;
204 	else
205 		return -EINVAL;
206 
207 	return 0;
208 }
209 
210 static bool
211 komeda_plane_format_mod_supported(struct drm_plane *plane,
212 				  u32 format, u64 modifier)
213 {
214 	struct komeda_dev *mdev = plane->dev->dev_private;
215 	struct komeda_plane *kplane = to_kplane(plane);
216 	u32 layer_type = kplane->layer->layer_type;
217 
218 	return komeda_format_mod_supported(&mdev->fmt_tbl, layer_type,
219 					   format, modifier, 0);
220 }
221 
222 static const struct drm_plane_funcs komeda_plane_funcs = {
223 	.update_plane		= drm_atomic_helper_update_plane,
224 	.disable_plane		= drm_atomic_helper_disable_plane,
225 	.destroy		= komeda_plane_destroy,
226 	.reset			= komeda_plane_reset,
227 	.atomic_duplicate_state	= komeda_plane_atomic_duplicate_state,
228 	.atomic_destroy_state	= komeda_plane_atomic_destroy_state,
229 	.atomic_get_property	= komeda_plane_atomic_get_property,
230 	.atomic_set_property	= komeda_plane_atomic_set_property,
231 	.format_mod_supported	= komeda_plane_format_mod_supported,
232 };
233 
234 static int
235 komeda_plane_create_layer_properties(struct komeda_plane *kplane,
236 				     struct komeda_layer *layer)
237 {
238 	struct drm_device *drm = kplane->base.dev;
239 	struct drm_plane *plane = &kplane->base;
240 	struct drm_property *prop = NULL;
241 
242 	/* property: layer image_enhancement */
243 	if (layer->base.supported_outputs & KOMEDA_PIPELINE_SCALERS) {
244 		prop = drm_property_create_bool(drm, DRM_MODE_PROP_ATOMIC,
245 						"img_enhancement");
246 		if (!prop)
247 			return -ENOMEM;
248 
249 		drm_object_attach_property(&plane->base, prop, 0);
250 		kplane->prop_img_enhancement = prop;
251 	}
252 
253 	return 0;
254 }
255 
256 /* for komeda, which is pipeline can be share between crtcs */
257 static u32 get_possible_crtcs(struct komeda_kms_dev *kms,
258 			      struct komeda_pipeline *pipe)
259 {
260 	struct komeda_crtc *crtc;
261 	u32 possible_crtcs = 0;
262 	int i;
263 
264 	for (i = 0; i < kms->n_crtcs; i++) {
265 		crtc = &kms->crtcs[i];
266 
267 		if ((pipe == crtc->master) || (pipe == crtc->slave))
268 			possible_crtcs |= BIT(i);
269 	}
270 
271 	return possible_crtcs;
272 }
273 
274 /* use Layer0 as primary */
275 static u32 get_plane_type(struct komeda_kms_dev *kms,
276 			  struct komeda_component *c)
277 {
278 	bool is_primary = (c->id == KOMEDA_COMPONENT_LAYER0);
279 
280 	return is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
281 }
282 
283 static int komeda_plane_add(struct komeda_kms_dev *kms,
284 			    struct komeda_layer *layer)
285 {
286 	struct komeda_dev *mdev = kms->base.dev_private;
287 	struct komeda_component *c = &layer->base;
288 	struct komeda_plane *kplane;
289 	struct drm_plane *plane;
290 	u32 *formats, n_formats = 0;
291 	int err;
292 
293 	kplane = kzalloc(sizeof(*kplane), GFP_KERNEL);
294 	if (!kplane)
295 		return -ENOMEM;
296 
297 	plane = &kplane->base;
298 	kplane->layer = layer;
299 
300 	formats = komeda_get_layer_fourcc_list(&mdev->fmt_tbl,
301 					       layer->layer_type, &n_formats);
302 
303 	err = drm_universal_plane_init(&kms->base, plane,
304 			get_possible_crtcs(kms, c->pipeline),
305 			&komeda_plane_funcs,
306 			formats, n_formats, komeda_supported_modifiers,
307 			get_plane_type(kms, c),
308 			"%s", c->name);
309 
310 	komeda_put_fourcc_list(formats);
311 
312 	if (err)
313 		goto cleanup;
314 
315 	drm_plane_helper_add(plane, &komeda_plane_helper_funcs);
316 
317 	err = drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
318 						 layer->supported_rots);
319 	if (err)
320 		goto cleanup;
321 
322 	err = drm_plane_create_alpha_property(plane);
323 	if (err)
324 		goto cleanup;
325 
326 	err = drm_plane_create_blend_mode_property(plane,
327 			BIT(DRM_MODE_BLEND_PIXEL_NONE) |
328 			BIT(DRM_MODE_BLEND_PREMULTI)   |
329 			BIT(DRM_MODE_BLEND_COVERAGE));
330 	if (err)
331 		goto cleanup;
332 
333 	err = komeda_plane_create_layer_properties(kplane, layer);
334 	if (err)
335 		goto cleanup;
336 
337 	err = drm_plane_create_color_properties(plane,
338 			BIT(DRM_COLOR_YCBCR_BT601) |
339 			BIT(DRM_COLOR_YCBCR_BT709) |
340 			BIT(DRM_COLOR_YCBCR_BT2020),
341 			BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
342 			BIT(DRM_COLOR_YCBCR_FULL_RANGE),
343 			DRM_COLOR_YCBCR_BT601,
344 			DRM_COLOR_YCBCR_LIMITED_RANGE);
345 	if (err)
346 		goto cleanup;
347 
348 	return 0;
349 cleanup:
350 	komeda_plane_destroy(plane);
351 	return err;
352 }
353 
354 int komeda_kms_add_planes(struct komeda_kms_dev *kms, struct komeda_dev *mdev)
355 {
356 	struct komeda_pipeline *pipe;
357 	int i, j, err;
358 
359 	for (i = 0; i < mdev->n_pipelines; i++) {
360 		pipe = mdev->pipelines[i];
361 
362 		for (j = 0; j < pipe->n_layers; j++) {
363 			err = komeda_plane_add(kms, pipe->layers[j]);
364 			if (err)
365 				return err;
366 		}
367 	}
368 
369 	return 0;
370 }
371