xref: /linux/drivers/gpu/drm/msm/disp/dpu1/dpu_plane.c (revision e843ca2f30e630675e2d2a75c96f4844f2854430)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014-2018 The Linux Foundation. All rights reserved.
4  * Copyright (C) 2013 Red Hat
5  * Author: Rob Clark <robdclark@gmail.com>
6  */
7 
8 #define pr_fmt(fmt)	"[drm:%s:%d] " fmt, __func__, __LINE__
9 
10 #include <linux/debugfs.h>
11 #include <linux/dma-buf.h>
12 
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_uapi.h>
15 #include <drm/drm_blend.h>
16 #include <drm/drm_damage_helper.h>
17 #include <drm/drm_framebuffer.h>
18 #include <drm/drm_gem_atomic_helper.h>
19 
20 #include "msm_drv.h"
21 #include "dpu_kms.h"
22 #include "dpu_formats.h"
23 #include "dpu_hw_sspp.h"
24 #include "dpu_trace.h"
25 #include "dpu_crtc.h"
26 #include "dpu_vbif.h"
27 #include "dpu_plane.h"
28 
29 #define DPU_DEBUG_PLANE(pl, fmt, ...) DRM_DEBUG_ATOMIC("plane%d " fmt,\
30 		(pl) ? (pl)->base.base.id : -1, ##__VA_ARGS__)
31 
32 #define DPU_ERROR_PLANE(pl, fmt, ...) DPU_ERROR("plane%d " fmt,\
33 		(pl) ? (pl)->base.base.id : -1, ##__VA_ARGS__)
34 
35 #define DECIMATED_DIMENSION(dim, deci) (((dim) + ((1 << (deci)) - 1)) >> (deci))
36 #define PHASE_STEP_SHIFT	21
37 #define PHASE_STEP_UNIT_SCALE   ((int) (1 << PHASE_STEP_SHIFT))
38 #define PHASE_RESIDUAL		15
39 
40 #define SHARP_STRENGTH_DEFAULT	32
41 #define SHARP_EDGE_THR_DEFAULT	112
42 #define SHARP_SMOOTH_THR_DEFAULT	8
43 #define SHARP_NOISE_THR_DEFAULT	2
44 
45 #define DPU_PLANE_COLOR_FILL_FLAG	BIT(31)
46 #define DPU_ZPOS_MAX 255
47 
48 /*
49  * Default Preload Values
50  */
51 #define DPU_QSEED3_DEFAULT_PRELOAD_H 0x4
52 #define DPU_QSEED3_DEFAULT_PRELOAD_V 0x3
53 #define DPU_QSEED4_DEFAULT_PRELOAD_V 0x2
54 #define DPU_QSEED4_DEFAULT_PRELOAD_H 0x4
55 
56 #define DEFAULT_REFRESH_RATE	60
57 
58 static const uint32_t qcom_compressed_supported_formats[] = {
59 	DRM_FORMAT_ABGR8888,
60 	DRM_FORMAT_ARGB8888,
61 	DRM_FORMAT_XBGR8888,
62 	DRM_FORMAT_XRGB8888,
63 	DRM_FORMAT_ARGB2101010,
64 	DRM_FORMAT_XRGB2101010,
65 	DRM_FORMAT_BGR565,
66 
67 	DRM_FORMAT_NV12,
68 	DRM_FORMAT_P010,
69 };
70 
71 /*
72  * struct dpu_plane - local dpu plane structure
73  * @aspace: address space pointer
74  * @csc_ptr: Points to dpu_csc_cfg structure to use for current
75  * @catalog: Points to dpu catalog structure
76  * @revalidate: force revalidation of all the plane properties
77  */
78 struct dpu_plane {
79 	struct drm_plane base;
80 
81 	enum dpu_sspp pipe;
82 
83 	uint32_t color_fill;
84 	bool is_error;
85 	bool is_rt_pipe;
86 	const struct dpu_mdss_cfg *catalog;
87 };
88 
89 static const uint64_t supported_format_modifiers[] = {
90 	DRM_FORMAT_MOD_QCOM_COMPRESSED,
91 	DRM_FORMAT_MOD_LINEAR,
92 	DRM_FORMAT_MOD_INVALID
93 };
94 
95 #define to_dpu_plane(x) container_of(x, struct dpu_plane, base)
96 
97 static struct dpu_kms *_dpu_plane_get_kms(struct drm_plane *plane)
98 {
99 	struct msm_drm_private *priv = plane->dev->dev_private;
100 
101 	return to_dpu_kms(priv->kms);
102 }
103 
104 /**
105  * _dpu_plane_calc_bw - calculate bandwidth required for a plane
106  * @catalog: Points to dpu catalog structure
107  * @fmt: Pointer to source buffer format
108  * @mode: Pointer to drm display mode
109  * @pipe_cfg: Pointer to pipe configuration
110  * Result: Updates calculated bandwidth in the plane state.
111  * BW Equation: src_w * src_h * bpp * fps * (v_total / v_dest)
112  * Prefill BW Equation: line src bytes * line_time
113  */
114 static u64 _dpu_plane_calc_bw(const struct dpu_mdss_cfg *catalog,
115 	const struct dpu_format *fmt,
116 	const struct drm_display_mode *mode,
117 	struct dpu_sw_pipe_cfg *pipe_cfg)
118 {
119 	int src_width, src_height, dst_height, fps;
120 	u64 plane_pixel_rate, plane_bit_rate;
121 	u64 plane_prefill_bw;
122 	u64 plane_bw;
123 	u32 hw_latency_lines;
124 	u64 scale_factor;
125 	int vbp, vpw, vfp;
126 
127 	src_width = drm_rect_width(&pipe_cfg->src_rect);
128 	src_height = drm_rect_height(&pipe_cfg->src_rect);
129 	dst_height = drm_rect_height(&pipe_cfg->dst_rect);
130 	fps = drm_mode_vrefresh(mode);
131 	vbp = mode->vtotal - mode->vsync_end;
132 	vpw = mode->vsync_end - mode->vsync_start;
133 	vfp = mode->vsync_start - mode->vdisplay;
134 	hw_latency_lines =  catalog->perf->min_prefill_lines;
135 	scale_factor = src_height > dst_height ?
136 		mult_frac(src_height, 1, dst_height) : 1;
137 
138 	plane_pixel_rate = src_width * mode->vtotal * fps;
139 	plane_bit_rate = plane_pixel_rate * fmt->bpp;
140 
141 	plane_bw = plane_bit_rate * scale_factor;
142 
143 	plane_prefill_bw = plane_bw * hw_latency_lines;
144 
145 	if ((vbp+vpw) > hw_latency_lines)
146 		do_div(plane_prefill_bw, (vbp+vpw));
147 	else if ((vbp+vpw+vfp) < hw_latency_lines)
148 		do_div(plane_prefill_bw, (vbp+vpw+vfp));
149 	else
150 		do_div(plane_prefill_bw, hw_latency_lines);
151 
152 
153 	return max(plane_bw, plane_prefill_bw);
154 }
155 
156 /**
157  * _dpu_plane_calc_clk - calculate clock required for a plane
158  * @mode: Pointer to drm display mode
159  * @pipe_cfg: Pointer to pipe configuration
160  * Result: Updates calculated clock in the plane state.
161  * Clock equation: dst_w * v_total * fps * (src_h / dst_h)
162  */
163 static u64 _dpu_plane_calc_clk(const struct drm_display_mode *mode,
164 		struct dpu_sw_pipe_cfg *pipe_cfg)
165 {
166 	int dst_width, src_height, dst_height, fps;
167 	u64 plane_clk;
168 
169 	src_height = drm_rect_height(&pipe_cfg->src_rect);
170 	dst_width = drm_rect_width(&pipe_cfg->dst_rect);
171 	dst_height = drm_rect_height(&pipe_cfg->dst_rect);
172 	fps = drm_mode_vrefresh(mode);
173 
174 	plane_clk =
175 		dst_width * mode->vtotal * fps;
176 
177 	if (src_height > dst_height) {
178 		plane_clk *= src_height;
179 		do_div(plane_clk, dst_height);
180 	}
181 
182 	return plane_clk;
183 }
184 
185 /**
186  * _dpu_plane_calc_fill_level - calculate fill level of the given source format
187  * @plane:		Pointer to drm plane
188  * @pipe:		Pointer to software pipe
189  * @lut_usage:		LUT usecase
190  * @fmt:		Pointer to source buffer format
191  * @src_width:		width of source buffer
192  * Return: fill level corresponding to the source buffer/format or 0 if error
193  */
194 static int _dpu_plane_calc_fill_level(struct drm_plane *plane,
195 		struct dpu_sw_pipe *pipe,
196 		enum dpu_qos_lut_usage lut_usage,
197 		const struct dpu_format *fmt, u32 src_width)
198 {
199 	struct dpu_plane *pdpu;
200 	u32 fixed_buff_size;
201 	u32 total_fl;
202 
203 	if (!fmt || !pipe || !src_width || !fmt->bpp) {
204 		DPU_ERROR("invalid arguments\n");
205 		return 0;
206 	}
207 
208 	if (lut_usage == DPU_QOS_LUT_USAGE_NRT)
209 		return 0;
210 
211 	pdpu = to_dpu_plane(plane);
212 	fixed_buff_size = pdpu->catalog->caps->pixel_ram_size;
213 
214 	/* FIXME: in multirect case account for the src_width of all the planes */
215 
216 	if (fmt->fetch_planes == DPU_PLANE_PSEUDO_PLANAR) {
217 		if (fmt->chroma_sample == DPU_CHROMA_420) {
218 			/* NV12 */
219 			total_fl = (fixed_buff_size / 2) /
220 				((src_width + 32) * fmt->bpp);
221 		} else {
222 			/* non NV12 */
223 			total_fl = (fixed_buff_size / 2) * 2 /
224 				((src_width + 32) * fmt->bpp);
225 		}
226 	} else {
227 		if (pipe->multirect_mode == DPU_SSPP_MULTIRECT_PARALLEL) {
228 			total_fl = (fixed_buff_size / 2) * 2 /
229 				((src_width + 32) * fmt->bpp);
230 		} else {
231 			total_fl = (fixed_buff_size) * 2 /
232 				((src_width + 32) * fmt->bpp);
233 		}
234 	}
235 
236 	DPU_DEBUG_PLANE(pdpu, "pnum:%d fmt: %4.4s w:%u fl:%u\n",
237 			pipe->sspp->idx - SSPP_VIG0,
238 			(char *)&fmt->base.pixel_format,
239 			src_width, total_fl);
240 
241 	return total_fl;
242 }
243 
244 /**
245  * _dpu_plane_set_qos_lut - set QoS LUT of the given plane
246  * @plane:		Pointer to drm plane
247  * @pipe:		Pointer to software pipe
248  * @fmt:		Pointer to source buffer format
249  * @pipe_cfg:		Pointer to pipe configuration
250  */
251 static void _dpu_plane_set_qos_lut(struct drm_plane *plane,
252 		struct dpu_sw_pipe *pipe,
253 		const struct dpu_format *fmt, struct dpu_sw_pipe_cfg *pipe_cfg)
254 {
255 	struct dpu_plane *pdpu = to_dpu_plane(plane);
256 	struct dpu_hw_qos_cfg cfg;
257 	u32 total_fl, lut_usage;
258 
259 	if (!pdpu->is_rt_pipe) {
260 		lut_usage = DPU_QOS_LUT_USAGE_NRT;
261 	} else {
262 		if (fmt && DPU_FORMAT_IS_LINEAR(fmt))
263 			lut_usage = DPU_QOS_LUT_USAGE_LINEAR;
264 		else
265 			lut_usage = DPU_QOS_LUT_USAGE_MACROTILE;
266 	}
267 
268 	total_fl = _dpu_plane_calc_fill_level(plane, pipe, lut_usage, fmt,
269 				drm_rect_width(&pipe_cfg->src_rect));
270 
271 	cfg.creq_lut = _dpu_hw_get_qos_lut(&pdpu->catalog->perf->qos_lut_tbl[lut_usage], total_fl);
272 	cfg.danger_lut = pdpu->catalog->perf->danger_lut_tbl[lut_usage];
273 	cfg.safe_lut = pdpu->catalog->perf->safe_lut_tbl[lut_usage];
274 
275 	if (pipe->sspp->idx != SSPP_CURSOR0 &&
276 	    pipe->sspp->idx != SSPP_CURSOR1 &&
277 	    pdpu->is_rt_pipe)
278 		cfg.danger_safe_en = true;
279 
280 	DPU_DEBUG_PLANE(pdpu, "pnum:%d ds:%d is_rt:%d\n",
281 		pdpu->pipe - SSPP_VIG0,
282 		cfg.danger_safe_en,
283 		pdpu->is_rt_pipe);
284 
285 	trace_dpu_perf_set_qos_luts(pipe->sspp->idx - SSPP_VIG0,
286 			(fmt) ? fmt->base.pixel_format : 0,
287 			pdpu->is_rt_pipe, total_fl, cfg.creq_lut, lut_usage);
288 
289 	DPU_DEBUG_PLANE(pdpu, "pnum:%d fmt: %4.4s rt:%d fl:%u lut:0x%llx\n",
290 			pdpu->pipe - SSPP_VIG0,
291 			fmt ? (char *)&fmt->base.pixel_format : NULL,
292 			pdpu->is_rt_pipe, total_fl, cfg.creq_lut);
293 
294 	trace_dpu_perf_set_danger_luts(pdpu->pipe - SSPP_VIG0,
295 			(fmt) ? fmt->base.pixel_format : 0,
296 			(fmt) ? fmt->fetch_mode : 0,
297 			cfg.danger_lut,
298 			cfg.safe_lut);
299 
300 	DPU_DEBUG_PLANE(pdpu, "pnum:%d fmt: %4.4s mode:%d luts[0x%x, 0x%x]\n",
301 		pdpu->pipe - SSPP_VIG0,
302 		fmt ? (char *)&fmt->base.pixel_format : NULL,
303 		fmt ? fmt->fetch_mode : -1,
304 		cfg.danger_lut,
305 		cfg.safe_lut);
306 
307 	pipe->sspp->ops.setup_qos_lut(pipe->sspp, &cfg);
308 }
309 
310 /**
311  * _dpu_plane_set_qos_ctrl - set QoS control of the given plane
312  * @plane:		Pointer to drm plane
313  * @pipe:		Pointer to software pipe
314  * @enable:		true to enable QoS control
315  */
316 static void _dpu_plane_set_qos_ctrl(struct drm_plane *plane,
317 	struct dpu_sw_pipe *pipe,
318 	bool enable)
319 {
320 	struct dpu_plane *pdpu = to_dpu_plane(plane);
321 
322 	if (!pdpu->is_rt_pipe)
323 		enable = false;
324 
325 	DPU_DEBUG_PLANE(pdpu, "pnum:%d ds:%d is_rt:%d\n",
326 		pdpu->pipe - SSPP_VIG0,
327 		enable,
328 		pdpu->is_rt_pipe);
329 
330 	pipe->sspp->ops.setup_qos_ctrl(pipe->sspp,
331 				       enable);
332 }
333 
334 static bool _dpu_plane_sspp_clk_force_ctrl(struct dpu_hw_sspp *sspp,
335 					   struct dpu_hw_mdp *mdp,
336 					   bool enable, bool *forced_on)
337 {
338 	if (sspp->ops.setup_clk_force_ctrl) {
339 		*forced_on = sspp->ops.setup_clk_force_ctrl(sspp, enable);
340 		return true;
341 	}
342 
343 	if (mdp->ops.setup_clk_force_ctrl) {
344 		*forced_on = mdp->ops.setup_clk_force_ctrl(mdp, sspp->cap->clk_ctrl, enable);
345 		return true;
346 	}
347 
348 	return false;
349 }
350 
351 /**
352  * _dpu_plane_set_ot_limit - set OT limit for the given plane
353  * @plane:		Pointer to drm plane
354  * @pipe:		Pointer to software pipe
355  * @pipe_cfg:		Pointer to pipe configuration
356  * @frame_rate:		CRTC's frame rate
357  */
358 static void _dpu_plane_set_ot_limit(struct drm_plane *plane,
359 		struct dpu_sw_pipe *pipe,
360 		struct dpu_sw_pipe_cfg *pipe_cfg,
361 		int frame_rate)
362 {
363 	struct dpu_plane *pdpu = to_dpu_plane(plane);
364 	struct dpu_vbif_set_ot_params ot_params;
365 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
366 	bool forced_on = false;
367 
368 	memset(&ot_params, 0, sizeof(ot_params));
369 	ot_params.xin_id = pipe->sspp->cap->xin_id;
370 	ot_params.num = pipe->sspp->idx - SSPP_NONE;
371 	ot_params.width = drm_rect_width(&pipe_cfg->src_rect);
372 	ot_params.height = drm_rect_height(&pipe_cfg->src_rect);
373 	ot_params.is_wfd = !pdpu->is_rt_pipe;
374 	ot_params.frame_rate = frame_rate;
375 	ot_params.vbif_idx = VBIF_RT;
376 	ot_params.rd = true;
377 
378 	if (!_dpu_plane_sspp_clk_force_ctrl(pipe->sspp, dpu_kms->hw_mdp,
379 					    true, &forced_on))
380 		return;
381 
382 	dpu_vbif_set_ot_limit(dpu_kms, &ot_params);
383 
384 	if (forced_on)
385 		_dpu_plane_sspp_clk_force_ctrl(pipe->sspp, dpu_kms->hw_mdp,
386 					       false, &forced_on);
387 }
388 
389 /**
390  * _dpu_plane_set_qos_remap - set vbif QoS for the given plane
391  * @plane:		Pointer to drm plane
392  * @pipe:		Pointer to software pipe
393  */
394 static void _dpu_plane_set_qos_remap(struct drm_plane *plane,
395 		struct dpu_sw_pipe *pipe)
396 {
397 	struct dpu_plane *pdpu = to_dpu_plane(plane);
398 	struct dpu_vbif_set_qos_params qos_params;
399 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
400 	bool forced_on = false;
401 
402 	memset(&qos_params, 0, sizeof(qos_params));
403 	qos_params.vbif_idx = VBIF_RT;
404 	qos_params.xin_id = pipe->sspp->cap->xin_id;
405 	qos_params.num = pipe->sspp->idx - SSPP_VIG0;
406 	qos_params.is_rt = pdpu->is_rt_pipe;
407 
408 	DPU_DEBUG_PLANE(pdpu, "pipe:%d vbif:%d xin:%d rt:%d\n",
409 			qos_params.num,
410 			qos_params.vbif_idx,
411 			qos_params.xin_id, qos_params.is_rt);
412 
413 	if (!_dpu_plane_sspp_clk_force_ctrl(pipe->sspp, dpu_kms->hw_mdp,
414 					    true, &forced_on))
415 		return;
416 
417 	dpu_vbif_set_qos_remap(dpu_kms, &qos_params);
418 
419 	if (forced_on)
420 		_dpu_plane_sspp_clk_force_ctrl(pipe->sspp, dpu_kms->hw_mdp,
421 					       false, &forced_on);
422 }
423 
424 static void _dpu_plane_setup_scaler3(struct dpu_hw_sspp *pipe_hw,
425 		uint32_t src_w, uint32_t src_h, uint32_t dst_w, uint32_t dst_h,
426 		struct dpu_hw_scaler3_cfg *scale_cfg,
427 		const struct dpu_format *fmt,
428 		uint32_t chroma_subsmpl_h, uint32_t chroma_subsmpl_v,
429 		unsigned int rotation)
430 {
431 	uint32_t i;
432 	bool inline_rotation = rotation & DRM_MODE_ROTATE_90;
433 
434 	/*
435 	 * For inline rotation cases, scaler config is post-rotation,
436 	 * so swap the dimensions here. However, pixel extension will
437 	 * need pre-rotation settings.
438 	 */
439 	if (inline_rotation)
440 		swap(src_w, src_h);
441 
442 	scale_cfg->phase_step_x[DPU_SSPP_COMP_0] =
443 		mult_frac((1 << PHASE_STEP_SHIFT), src_w, dst_w);
444 	scale_cfg->phase_step_y[DPU_SSPP_COMP_0] =
445 		mult_frac((1 << PHASE_STEP_SHIFT), src_h, dst_h);
446 
447 
448 	scale_cfg->phase_step_y[DPU_SSPP_COMP_1_2] =
449 		scale_cfg->phase_step_y[DPU_SSPP_COMP_0] / chroma_subsmpl_v;
450 	scale_cfg->phase_step_x[DPU_SSPP_COMP_1_2] =
451 		scale_cfg->phase_step_x[DPU_SSPP_COMP_0] / chroma_subsmpl_h;
452 
453 	scale_cfg->phase_step_x[DPU_SSPP_COMP_2] =
454 		scale_cfg->phase_step_x[DPU_SSPP_COMP_1_2];
455 	scale_cfg->phase_step_y[DPU_SSPP_COMP_2] =
456 		scale_cfg->phase_step_y[DPU_SSPP_COMP_1_2];
457 
458 	scale_cfg->phase_step_x[DPU_SSPP_COMP_3] =
459 		scale_cfg->phase_step_x[DPU_SSPP_COMP_0];
460 	scale_cfg->phase_step_y[DPU_SSPP_COMP_3] =
461 		scale_cfg->phase_step_y[DPU_SSPP_COMP_0];
462 
463 	for (i = 0; i < DPU_MAX_PLANES; i++) {
464 		scale_cfg->src_width[i] = src_w;
465 		scale_cfg->src_height[i] = src_h;
466 		if (i == DPU_SSPP_COMP_1_2 || i == DPU_SSPP_COMP_2) {
467 			scale_cfg->src_width[i] /= chroma_subsmpl_h;
468 			scale_cfg->src_height[i] /= chroma_subsmpl_v;
469 		}
470 
471 		if (pipe_hw->cap->sblk->scaler_blk.version >= 0x3000) {
472 			scale_cfg->preload_x[i] = DPU_QSEED4_DEFAULT_PRELOAD_H;
473 			scale_cfg->preload_y[i] = DPU_QSEED4_DEFAULT_PRELOAD_V;
474 		} else {
475 			scale_cfg->preload_x[i] = DPU_QSEED3_DEFAULT_PRELOAD_H;
476 			scale_cfg->preload_y[i] = DPU_QSEED3_DEFAULT_PRELOAD_V;
477 		}
478 	}
479 	if (!(DPU_FORMAT_IS_YUV(fmt)) && (src_h == dst_h)
480 		&& (src_w == dst_w))
481 		return;
482 
483 	scale_cfg->dst_width = dst_w;
484 	scale_cfg->dst_height = dst_h;
485 	scale_cfg->y_rgb_filter_cfg = DPU_SCALE_BIL;
486 	scale_cfg->uv_filter_cfg = DPU_SCALE_BIL;
487 	scale_cfg->alpha_filter_cfg = DPU_SCALE_ALPHA_BIL;
488 	scale_cfg->lut_flag = 0;
489 	scale_cfg->blend_cfg = 1;
490 	scale_cfg->enable = 1;
491 }
492 
493 static void _dpu_plane_setup_pixel_ext(struct dpu_hw_scaler3_cfg *scale_cfg,
494 				struct dpu_hw_pixel_ext *pixel_ext,
495 				uint32_t src_w, uint32_t src_h,
496 				uint32_t chroma_subsmpl_h, uint32_t chroma_subsmpl_v)
497 {
498 	int i;
499 
500 	for (i = 0; i < DPU_MAX_PLANES; i++) {
501 		if (i == DPU_SSPP_COMP_1_2 || i == DPU_SSPP_COMP_2) {
502 			src_w /= chroma_subsmpl_h;
503 			src_h /= chroma_subsmpl_v;
504 		}
505 
506 		pixel_ext->num_ext_pxls_top[i] = src_h;
507 		pixel_ext->num_ext_pxls_left[i] = src_w;
508 	}
509 }
510 
511 static const struct dpu_csc_cfg dpu_csc_YUV2RGB_601L = {
512 	{
513 		/* S15.16 format */
514 		0x00012A00, 0x00000000, 0x00019880,
515 		0x00012A00, 0xFFFF9B80, 0xFFFF3000,
516 		0x00012A00, 0x00020480, 0x00000000,
517 	},
518 	/* signed bias */
519 	{ 0xfff0, 0xff80, 0xff80,},
520 	{ 0x0, 0x0, 0x0,},
521 	/* unsigned clamp */
522 	{ 0x10, 0xeb, 0x10, 0xf0, 0x10, 0xf0,},
523 	{ 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,},
524 };
525 
526 static const struct dpu_csc_cfg dpu_csc10_YUV2RGB_601L = {
527 	{
528 		/* S15.16 format */
529 		0x00012A00, 0x00000000, 0x00019880,
530 		0x00012A00, 0xFFFF9B80, 0xFFFF3000,
531 		0x00012A00, 0x00020480, 0x00000000,
532 		},
533 	/* signed bias */
534 	{ 0xffc0, 0xfe00, 0xfe00,},
535 	{ 0x0, 0x0, 0x0,},
536 	/* unsigned clamp */
537 	{ 0x40, 0x3ac, 0x40, 0x3c0, 0x40, 0x3c0,},
538 	{ 0x00, 0x3ff, 0x00, 0x3ff, 0x00, 0x3ff,},
539 };
540 
541 static const struct dpu_csc_cfg *_dpu_plane_get_csc(struct dpu_sw_pipe *pipe,
542 						    const struct dpu_format *fmt)
543 {
544 	const struct dpu_csc_cfg *csc_ptr;
545 
546 	if (!DPU_FORMAT_IS_YUV(fmt))
547 		return NULL;
548 
549 	if (BIT(DPU_SSPP_CSC_10BIT) & pipe->sspp->cap->features)
550 		csc_ptr = &dpu_csc10_YUV2RGB_601L;
551 	else
552 		csc_ptr = &dpu_csc_YUV2RGB_601L;
553 
554 	return csc_ptr;
555 }
556 
557 static void _dpu_plane_setup_scaler(struct dpu_sw_pipe *pipe,
558 		const struct dpu_format *fmt, bool color_fill,
559 		struct dpu_sw_pipe_cfg *pipe_cfg,
560 		unsigned int rotation)
561 {
562 	struct dpu_hw_sspp *pipe_hw = pipe->sspp;
563 	const struct drm_format_info *info = drm_format_info(fmt->base.pixel_format);
564 	struct dpu_hw_scaler3_cfg scaler3_cfg;
565 	struct dpu_hw_pixel_ext pixel_ext;
566 	u32 src_width = drm_rect_width(&pipe_cfg->src_rect);
567 	u32 src_height = drm_rect_height(&pipe_cfg->src_rect);
568 	u32 dst_width = drm_rect_width(&pipe_cfg->dst_rect);
569 	u32 dst_height = drm_rect_height(&pipe_cfg->dst_rect);
570 
571 	memset(&scaler3_cfg, 0, sizeof(scaler3_cfg));
572 	memset(&pixel_ext, 0, sizeof(pixel_ext));
573 
574 	/* don't chroma subsample if decimating */
575 	/* update scaler. calculate default config for QSEED3 */
576 	_dpu_plane_setup_scaler3(pipe_hw,
577 			src_width,
578 			src_height,
579 			dst_width,
580 			dst_height,
581 			&scaler3_cfg, fmt,
582 			info->hsub, info->vsub,
583 			rotation);
584 
585 	/* configure pixel extension based on scalar config */
586 	_dpu_plane_setup_pixel_ext(&scaler3_cfg, &pixel_ext,
587 			src_width, src_height, info->hsub, info->vsub);
588 
589 	if (pipe_hw->ops.setup_pe)
590 		pipe_hw->ops.setup_pe(pipe_hw,
591 				&pixel_ext);
592 
593 	/**
594 	 * when programmed in multirect mode, scalar block will be
595 	 * bypassed. Still we need to update alpha and bitwidth
596 	 * ONLY for RECT0
597 	 */
598 	if (pipe_hw->ops.setup_scaler &&
599 			pipe->multirect_index != DPU_SSPP_RECT_1)
600 		pipe_hw->ops.setup_scaler(pipe_hw,
601 				&scaler3_cfg,
602 				fmt);
603 }
604 
605 static void _dpu_plane_color_fill_pipe(struct dpu_plane_state *pstate,
606 				       struct dpu_sw_pipe *pipe,
607 				       struct drm_rect *dst_rect,
608 				       u32 fill_color,
609 				       const struct dpu_format *fmt)
610 {
611 	struct dpu_sw_pipe_cfg pipe_cfg;
612 
613 	/* update sspp */
614 	if (!pipe->sspp->ops.setup_solidfill)
615 		return;
616 
617 	pipe->sspp->ops.setup_solidfill(pipe, fill_color);
618 
619 	/* override scaler/decimation if solid fill */
620 	pipe_cfg.dst_rect = *dst_rect;
621 
622 	pipe_cfg.src_rect.x1 = 0;
623 	pipe_cfg.src_rect.y1 = 0;
624 	pipe_cfg.src_rect.x2 =
625 		drm_rect_width(&pipe_cfg.dst_rect);
626 	pipe_cfg.src_rect.y2 =
627 		drm_rect_height(&pipe_cfg.dst_rect);
628 
629 	if (pipe->sspp->ops.setup_format)
630 		pipe->sspp->ops.setup_format(pipe, fmt, DPU_SSPP_SOLID_FILL);
631 
632 	if (pipe->sspp->ops.setup_rects)
633 		pipe->sspp->ops.setup_rects(pipe, &pipe_cfg);
634 
635 	_dpu_plane_setup_scaler(pipe, fmt, true, &pipe_cfg, pstate->rotation);
636 }
637 
638 /**
639  * _dpu_plane_color_fill - enables color fill on plane
640  * @pdpu:   Pointer to DPU plane object
641  * @color:  RGB fill color value, [23..16] Blue, [15..8] Green, [7..0] Red
642  * @alpha:  8-bit fill alpha value, 255 selects 100% alpha
643  */
644 static void _dpu_plane_color_fill(struct dpu_plane *pdpu,
645 		uint32_t color, uint32_t alpha)
646 {
647 	const struct dpu_format *fmt;
648 	const struct drm_plane *plane = &pdpu->base;
649 	struct dpu_plane_state *pstate = to_dpu_plane_state(plane->state);
650 	u32 fill_color = (color & 0xFFFFFF) | ((alpha & 0xFF) << 24);
651 
652 	DPU_DEBUG_PLANE(pdpu, "\n");
653 
654 	/*
655 	 * select fill format to match user property expectation,
656 	 * h/w only supports RGB variants
657 	 */
658 	fmt = dpu_get_dpu_format(DRM_FORMAT_ABGR8888);
659 	/* should not happen ever */
660 	if (!fmt)
661 		return;
662 
663 	/* update sspp */
664 	_dpu_plane_color_fill_pipe(pstate, &pstate->pipe, &pstate->pipe_cfg.dst_rect,
665 				   fill_color, fmt);
666 
667 	if (pstate->r_pipe.sspp)
668 		_dpu_plane_color_fill_pipe(pstate, &pstate->r_pipe, &pstate->r_pipe_cfg.dst_rect,
669 					   fill_color, fmt);
670 }
671 
672 static int dpu_plane_prepare_fb(struct drm_plane *plane,
673 		struct drm_plane_state *new_state)
674 {
675 	struct drm_framebuffer *fb = new_state->fb;
676 	struct dpu_plane *pdpu = to_dpu_plane(plane);
677 	struct dpu_plane_state *pstate = to_dpu_plane_state(new_state);
678 	struct dpu_hw_fmt_layout layout;
679 	struct dpu_kms *kms = _dpu_plane_get_kms(&pdpu->base);
680 	int ret;
681 
682 	if (!new_state->fb)
683 		return 0;
684 
685 	DPU_DEBUG_PLANE(pdpu, "FB[%u]\n", fb->base.id);
686 
687 	/* cache aspace */
688 	pstate->aspace = kms->base.aspace;
689 
690 	/*
691 	 * TODO: Need to sort out the msm_framebuffer_prepare() call below so
692 	 *       we can use msm_atomic_prepare_fb() instead of doing the
693 	 *       implicit fence and fb prepare by hand here.
694 	 */
695 	drm_gem_plane_helper_prepare_fb(plane, new_state);
696 
697 	if (pstate->aspace) {
698 		ret = msm_framebuffer_prepare(new_state->fb,
699 				pstate->aspace, pstate->needs_dirtyfb);
700 		if (ret) {
701 			DPU_ERROR("failed to prepare framebuffer\n");
702 			return ret;
703 		}
704 	}
705 
706 	/* validate framebuffer layout before commit */
707 	ret = dpu_format_populate_layout(pstate->aspace,
708 			new_state->fb, &layout);
709 	if (ret) {
710 		DPU_ERROR_PLANE(pdpu, "failed to get format layout, %d\n", ret);
711 		return ret;
712 	}
713 
714 	return 0;
715 }
716 
717 static void dpu_plane_cleanup_fb(struct drm_plane *plane,
718 		struct drm_plane_state *old_state)
719 {
720 	struct dpu_plane *pdpu = to_dpu_plane(plane);
721 	struct dpu_plane_state *old_pstate;
722 
723 	if (!old_state || !old_state->fb)
724 		return;
725 
726 	old_pstate = to_dpu_plane_state(old_state);
727 
728 	DPU_DEBUG_PLANE(pdpu, "FB[%u]\n", old_state->fb->base.id);
729 
730 	msm_framebuffer_cleanup(old_state->fb, old_pstate->aspace,
731 				old_pstate->needs_dirtyfb);
732 }
733 
734 static int dpu_plane_check_inline_rotation(struct dpu_plane *pdpu,
735 						const struct dpu_sspp_sub_blks *sblk,
736 						struct drm_rect src, const struct dpu_format *fmt)
737 {
738 	size_t num_formats;
739 	const u32 *supported_formats;
740 
741 	if (!sblk->rotation_cfg) {
742 		DPU_ERROR("invalid rotation cfg\n");
743 		return -EINVAL;
744 	}
745 
746 	if (drm_rect_width(&src) > sblk->rotation_cfg->rot_maxheight) {
747 		DPU_DEBUG_PLANE(pdpu, "invalid height for inline rot:%d max:%d\n",
748 				src.y2, sblk->rotation_cfg->rot_maxheight);
749 		return -EINVAL;
750 	}
751 
752 	supported_formats = sblk->rotation_cfg->rot_format_list;
753 	num_formats = sblk->rotation_cfg->rot_num_formats;
754 
755 	if (!DPU_FORMAT_IS_UBWC(fmt) ||
756 		!dpu_find_format(fmt->base.pixel_format, supported_formats, num_formats))
757 		return -EINVAL;
758 
759 	return 0;
760 }
761 
762 static int dpu_plane_atomic_check_pipe(struct dpu_plane *pdpu,
763 		struct dpu_sw_pipe *pipe,
764 		struct dpu_sw_pipe_cfg *pipe_cfg,
765 		const struct dpu_format *fmt,
766 		const struct drm_display_mode *mode)
767 {
768 	uint32_t min_src_size;
769 	struct dpu_kms *kms = _dpu_plane_get_kms(&pdpu->base);
770 
771 	min_src_size = DPU_FORMAT_IS_YUV(fmt) ? 2 : 1;
772 
773 	if (DPU_FORMAT_IS_YUV(fmt) &&
774 	    (!pipe->sspp->cap->sblk->scaler_blk.len ||
775 	     !pipe->sspp->cap->sblk->csc_blk.len)) {
776 		DPU_DEBUG_PLANE(pdpu,
777 				"plane doesn't have scaler/csc for yuv\n");
778 		return -EINVAL;
779 	}
780 
781 	/* check src bounds */
782 	if (drm_rect_width(&pipe_cfg->src_rect) < min_src_size ||
783 	    drm_rect_height(&pipe_cfg->src_rect) < min_src_size) {
784 		DPU_DEBUG_PLANE(pdpu, "invalid source " DRM_RECT_FMT "\n",
785 				DRM_RECT_ARG(&pipe_cfg->src_rect));
786 		return -E2BIG;
787 	}
788 
789 	/* valid yuv image */
790 	if (DPU_FORMAT_IS_YUV(fmt) &&
791 	    (pipe_cfg->src_rect.x1 & 0x1 ||
792 	     pipe_cfg->src_rect.y1 & 0x1 ||
793 	     drm_rect_width(&pipe_cfg->src_rect) & 0x1 ||
794 	     drm_rect_height(&pipe_cfg->src_rect) & 0x1)) {
795 		DPU_DEBUG_PLANE(pdpu, "invalid yuv source " DRM_RECT_FMT "\n",
796 				DRM_RECT_ARG(&pipe_cfg->src_rect));
797 		return -EINVAL;
798 	}
799 
800 	/* min dst support */
801 	if (drm_rect_width(&pipe_cfg->dst_rect) < 0x1 ||
802 	    drm_rect_height(&pipe_cfg->dst_rect) < 0x1) {
803 		DPU_DEBUG_PLANE(pdpu, "invalid dest rect " DRM_RECT_FMT "\n",
804 				DRM_RECT_ARG(&pipe_cfg->dst_rect));
805 		return -EINVAL;
806 	}
807 
808 	/* max clk check */
809 	if (_dpu_plane_calc_clk(mode, pipe_cfg) > kms->perf.max_core_clk_rate) {
810 		DPU_DEBUG_PLANE(pdpu, "plane exceeds max mdp core clk limits\n");
811 		return -E2BIG;
812 	}
813 
814 	return 0;
815 }
816 
817 static int dpu_plane_atomic_check(struct drm_plane *plane,
818 				  struct drm_atomic_state *state)
819 {
820 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
821 										 plane);
822 	int ret = 0, min_scale;
823 	struct dpu_plane *pdpu = to_dpu_plane(plane);
824 	struct dpu_kms *kms = _dpu_plane_get_kms(&pdpu->base);
825 	u64 max_mdp_clk_rate = kms->perf.max_core_clk_rate;
826 	struct dpu_plane_state *pstate = to_dpu_plane_state(new_plane_state);
827 	struct dpu_sw_pipe *pipe = &pstate->pipe;
828 	struct dpu_sw_pipe *r_pipe = &pstate->r_pipe;
829 	const struct drm_crtc_state *crtc_state = NULL;
830 	const struct dpu_format *fmt;
831 	struct dpu_sw_pipe_cfg *pipe_cfg = &pstate->pipe_cfg;
832 	struct dpu_sw_pipe_cfg *r_pipe_cfg = &pstate->r_pipe_cfg;
833 	struct drm_rect fb_rect = { 0 };
834 	uint32_t max_linewidth;
835 	unsigned int rotation;
836 	uint32_t supported_rotations;
837 	const struct dpu_sspp_cfg *pipe_hw_caps = pstate->pipe.sspp->cap;
838 	const struct dpu_sspp_sub_blks *sblk = pstate->pipe.sspp->cap->sblk;
839 
840 	if (new_plane_state->crtc)
841 		crtc_state = drm_atomic_get_new_crtc_state(state,
842 							   new_plane_state->crtc);
843 
844 	min_scale = FRAC_16_16(1, sblk->maxupscale);
845 	ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
846 						  min_scale,
847 						  sblk->maxdwnscale << 16,
848 						  true, true);
849 	if (ret) {
850 		DPU_DEBUG_PLANE(pdpu, "Check plane state failed (%d)\n", ret);
851 		return ret;
852 	}
853 	if (!new_plane_state->visible)
854 		return 0;
855 
856 	pipe->multirect_index = DPU_SSPP_RECT_SOLO;
857 	pipe->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
858 	r_pipe->multirect_index = DPU_SSPP_RECT_SOLO;
859 	r_pipe->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
860 	r_pipe->sspp = NULL;
861 
862 	pstate->stage = DPU_STAGE_0 + pstate->base.normalized_zpos;
863 	if (pstate->stage >= pdpu->catalog->caps->max_mixer_blendstages) {
864 		DPU_ERROR("> %d plane stages assigned\n",
865 			  pdpu->catalog->caps->max_mixer_blendstages - DPU_STAGE_0);
866 		return -EINVAL;
867 	}
868 
869 	pipe_cfg->src_rect = new_plane_state->src;
870 
871 	/* state->src is 16.16, src_rect is not */
872 	pipe_cfg->src_rect.x1 >>= 16;
873 	pipe_cfg->src_rect.x2 >>= 16;
874 	pipe_cfg->src_rect.y1 >>= 16;
875 	pipe_cfg->src_rect.y2 >>= 16;
876 
877 	pipe_cfg->dst_rect = new_plane_state->dst;
878 
879 	fb_rect.x2 = new_plane_state->fb->width;
880 	fb_rect.y2 = new_plane_state->fb->height;
881 
882 	/* Ensure fb size is supported */
883 	if (drm_rect_width(&fb_rect) > MAX_IMG_WIDTH ||
884 	    drm_rect_height(&fb_rect) > MAX_IMG_HEIGHT) {
885 		DPU_DEBUG_PLANE(pdpu, "invalid framebuffer " DRM_RECT_FMT "\n",
886 				DRM_RECT_ARG(&fb_rect));
887 		return -E2BIG;
888 	}
889 
890 	fmt = to_dpu_format(msm_framebuffer_format(new_plane_state->fb));
891 
892 	max_linewidth = pdpu->catalog->caps->max_linewidth;
893 
894 	if ((drm_rect_width(&pipe_cfg->src_rect) > max_linewidth) ||
895 	     _dpu_plane_calc_clk(&crtc_state->adjusted_mode, pipe_cfg) > max_mdp_clk_rate) {
896 		/*
897 		 * In parallel multirect case only the half of the usual width
898 		 * is supported for tiled formats. If we are here, we know that
899 		 * full width is more than max_linewidth, thus each rect is
900 		 * wider than allowed.
901 		 */
902 		if (DPU_FORMAT_IS_UBWC(fmt) &&
903 		    drm_rect_width(&pipe_cfg->src_rect) > max_linewidth) {
904 			DPU_DEBUG_PLANE(pdpu, "invalid src " DRM_RECT_FMT " line:%u, tiled format\n",
905 					DRM_RECT_ARG(&pipe_cfg->src_rect), max_linewidth);
906 			return -E2BIG;
907 		}
908 
909 		if (drm_rect_width(&pipe_cfg->src_rect) > 2 * max_linewidth) {
910 			DPU_DEBUG_PLANE(pdpu, "invalid src " DRM_RECT_FMT " line:%u\n",
911 					DRM_RECT_ARG(&pipe_cfg->src_rect), max_linewidth);
912 			return -E2BIG;
913 		}
914 
915 		if (drm_rect_width(&pipe_cfg->src_rect) != drm_rect_width(&pipe_cfg->dst_rect) ||
916 		    drm_rect_height(&pipe_cfg->src_rect) != drm_rect_height(&pipe_cfg->dst_rect) ||
917 		    (!test_bit(DPU_SSPP_SMART_DMA_V1, &pipe->sspp->cap->features) &&
918 		     !test_bit(DPU_SSPP_SMART_DMA_V2, &pipe->sspp->cap->features)) ||
919 		    DPU_FORMAT_IS_YUV(fmt)) {
920 			DPU_DEBUG_PLANE(pdpu, "invalid src " DRM_RECT_FMT " line:%u, can't use split source\n",
921 					DRM_RECT_ARG(&pipe_cfg->src_rect), max_linewidth);
922 			return -E2BIG;
923 		}
924 
925 		/*
926 		 * Use multirect for wide plane. We do not support dynamic
927 		 * assignment of SSPPs, so we know the configuration.
928 		 */
929 		pipe->multirect_index = DPU_SSPP_RECT_0;
930 		pipe->multirect_mode = DPU_SSPP_MULTIRECT_PARALLEL;
931 
932 		r_pipe->sspp = pipe->sspp;
933 		r_pipe->multirect_index = DPU_SSPP_RECT_1;
934 		r_pipe->multirect_mode = DPU_SSPP_MULTIRECT_PARALLEL;
935 
936 		*r_pipe_cfg = *pipe_cfg;
937 		pipe_cfg->src_rect.x2 = (pipe_cfg->src_rect.x1 + pipe_cfg->src_rect.x2) >> 1;
938 		pipe_cfg->dst_rect.x2 = (pipe_cfg->dst_rect.x1 + pipe_cfg->dst_rect.x2) >> 1;
939 		r_pipe_cfg->src_rect.x1 = pipe_cfg->src_rect.x2;
940 		r_pipe_cfg->dst_rect.x1 = pipe_cfg->dst_rect.x2;
941 	}
942 
943 	ret = dpu_plane_atomic_check_pipe(pdpu, pipe, pipe_cfg, fmt, &crtc_state->adjusted_mode);
944 	if (ret)
945 		return ret;
946 
947 	if (r_pipe->sspp) {
948 		ret = dpu_plane_atomic_check_pipe(pdpu, r_pipe, r_pipe_cfg, fmt,
949 						  &crtc_state->adjusted_mode);
950 		if (ret)
951 			return ret;
952 	}
953 
954 	supported_rotations = DRM_MODE_REFLECT_MASK | DRM_MODE_ROTATE_0;
955 
956 	if (pipe_hw_caps->features & BIT(DPU_SSPP_INLINE_ROTATION))
957 		supported_rotations |= DRM_MODE_ROTATE_90;
958 
959 	rotation = drm_rotation_simplify(new_plane_state->rotation,
960 					supported_rotations);
961 
962 	if ((pipe_hw_caps->features & BIT(DPU_SSPP_INLINE_ROTATION)) &&
963 		(rotation & DRM_MODE_ROTATE_90)) {
964 		ret = dpu_plane_check_inline_rotation(pdpu, sblk, pipe_cfg->src_rect, fmt);
965 		if (ret)
966 			return ret;
967 	}
968 
969 	pstate->rotation = rotation;
970 	pstate->needs_qos_remap = drm_atomic_crtc_needs_modeset(crtc_state);
971 
972 	return 0;
973 }
974 
975 static void dpu_plane_flush_csc(struct dpu_plane *pdpu, struct dpu_sw_pipe *pipe)
976 {
977 	const struct dpu_format *format =
978 		to_dpu_format(msm_framebuffer_format(pdpu->base.state->fb));
979 	const struct dpu_csc_cfg *csc_ptr;
980 
981 	if (!pipe->sspp || !pipe->sspp->ops.setup_csc)
982 		return;
983 
984 	csc_ptr = _dpu_plane_get_csc(pipe, format);
985 	if (!csc_ptr)
986 		return;
987 
988 	DPU_DEBUG_PLANE(pdpu, "using 0x%X 0x%X 0x%X...\n",
989 			csc_ptr->csc_mv[0],
990 			csc_ptr->csc_mv[1],
991 			csc_ptr->csc_mv[2]);
992 
993 	pipe->sspp->ops.setup_csc(pipe->sspp, csc_ptr);
994 
995 }
996 
997 void dpu_plane_flush(struct drm_plane *plane)
998 {
999 	struct dpu_plane *pdpu;
1000 	struct dpu_plane_state *pstate;
1001 
1002 	if (!plane || !plane->state) {
1003 		DPU_ERROR("invalid plane\n");
1004 		return;
1005 	}
1006 
1007 	pdpu = to_dpu_plane(plane);
1008 	pstate = to_dpu_plane_state(plane->state);
1009 
1010 	/*
1011 	 * These updates have to be done immediately before the plane flush
1012 	 * timing, and may not be moved to the atomic_update/mode_set functions.
1013 	 */
1014 	if (pdpu->is_error)
1015 		/* force white frame with 100% alpha pipe output on error */
1016 		_dpu_plane_color_fill(pdpu, 0xFFFFFF, 0xFF);
1017 	else if (pdpu->color_fill & DPU_PLANE_COLOR_FILL_FLAG)
1018 		/* force 100% alpha */
1019 		_dpu_plane_color_fill(pdpu, pdpu->color_fill, 0xFF);
1020 	else {
1021 		dpu_plane_flush_csc(pdpu, &pstate->pipe);
1022 		dpu_plane_flush_csc(pdpu, &pstate->r_pipe);
1023 	}
1024 
1025 	/* flag h/w flush complete */
1026 	if (plane->state)
1027 		pstate->pending = false;
1028 }
1029 
1030 /**
1031  * dpu_plane_set_error: enable/disable error condition
1032  * @plane: pointer to drm_plane structure
1033  * @error: error value to set
1034  */
1035 void dpu_plane_set_error(struct drm_plane *plane, bool error)
1036 {
1037 	struct dpu_plane *pdpu;
1038 
1039 	if (!plane)
1040 		return;
1041 
1042 	pdpu = to_dpu_plane(plane);
1043 	pdpu->is_error = error;
1044 }
1045 
1046 static void dpu_plane_sspp_update_pipe(struct drm_plane *plane,
1047 				       struct dpu_sw_pipe *pipe,
1048 				       struct dpu_sw_pipe_cfg *pipe_cfg,
1049 				       const struct dpu_format *fmt,
1050 				       int frame_rate,
1051 				       struct dpu_hw_fmt_layout *layout)
1052 {
1053 	uint32_t src_flags;
1054 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1055 	struct drm_plane_state *state = plane->state;
1056 	struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1057 
1058 	if (layout && pipe->sspp->ops.setup_sourceaddress) {
1059 		trace_dpu_plane_set_scanout(pipe, layout);
1060 		pipe->sspp->ops.setup_sourceaddress(pipe, layout);
1061 	}
1062 
1063 	/* override for color fill */
1064 	if (pdpu->color_fill & DPU_PLANE_COLOR_FILL_FLAG) {
1065 		_dpu_plane_set_qos_ctrl(plane, pipe, false);
1066 
1067 		/* skip remaining processing on color fill */
1068 		return;
1069 	}
1070 
1071 	if (pipe->sspp->ops.setup_rects) {
1072 		pipe->sspp->ops.setup_rects(pipe,
1073 				pipe_cfg);
1074 	}
1075 
1076 	_dpu_plane_setup_scaler(pipe, fmt, false, pipe_cfg, pstate->rotation);
1077 
1078 	if (pipe->sspp->ops.setup_multirect)
1079 		pipe->sspp->ops.setup_multirect(
1080 				pipe);
1081 
1082 	if (pipe->sspp->ops.setup_format) {
1083 		unsigned int rotation = pstate->rotation;
1084 
1085 		src_flags = 0x0;
1086 
1087 		if (rotation & DRM_MODE_REFLECT_X)
1088 			src_flags |= DPU_SSPP_FLIP_LR;
1089 
1090 		if (rotation & DRM_MODE_REFLECT_Y)
1091 			src_flags |= DPU_SSPP_FLIP_UD;
1092 
1093 		if (rotation & DRM_MODE_ROTATE_90)
1094 			src_flags |= DPU_SSPP_ROT_90;
1095 
1096 		/* update format */
1097 		pipe->sspp->ops.setup_format(pipe, fmt, src_flags);
1098 
1099 		if (pipe->sspp->ops.setup_cdp) {
1100 			const struct dpu_perf_cfg *perf = pdpu->catalog->perf;
1101 
1102 			pipe->sspp->ops.setup_cdp(pipe, fmt,
1103 						  perf->cdp_cfg[DPU_PERF_CDP_USAGE_RT].rd_enable);
1104 		}
1105 	}
1106 
1107 	_dpu_plane_set_qos_lut(plane, pipe, fmt, pipe_cfg);
1108 
1109 	if (pipe->sspp->idx != SSPP_CURSOR0 &&
1110 	    pipe->sspp->idx != SSPP_CURSOR1)
1111 		_dpu_plane_set_ot_limit(plane, pipe, pipe_cfg, frame_rate);
1112 
1113 	if (pstate->needs_qos_remap)
1114 		_dpu_plane_set_qos_remap(plane, pipe);
1115 }
1116 
1117 static void dpu_plane_sspp_atomic_update(struct drm_plane *plane)
1118 {
1119 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1120 	struct drm_plane_state *state = plane->state;
1121 	struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1122 	struct dpu_sw_pipe *pipe = &pstate->pipe;
1123 	struct dpu_sw_pipe *r_pipe = &pstate->r_pipe;
1124 	struct drm_crtc *crtc = state->crtc;
1125 	struct drm_framebuffer *fb = state->fb;
1126 	bool is_rt_pipe;
1127 	const struct dpu_format *fmt =
1128 		to_dpu_format(msm_framebuffer_format(fb));
1129 	struct dpu_sw_pipe_cfg *pipe_cfg = &pstate->pipe_cfg;
1130 	struct dpu_sw_pipe_cfg *r_pipe_cfg = &pstate->r_pipe_cfg;
1131 	struct dpu_kms *kms = _dpu_plane_get_kms(&pdpu->base);
1132 	struct msm_gem_address_space *aspace = kms->base.aspace;
1133 	struct dpu_hw_fmt_layout layout;
1134 	bool layout_valid = false;
1135 	int ret;
1136 
1137 	ret = dpu_format_populate_layout(aspace, fb, &layout);
1138 	if (ret)
1139 		DPU_ERROR_PLANE(pdpu, "failed to get format layout, %d\n", ret);
1140 	else
1141 		layout_valid = true;
1142 
1143 	pstate->pending = true;
1144 
1145 	is_rt_pipe = (dpu_crtc_get_client_type(crtc) != NRT_CLIENT);
1146 	pstate->needs_qos_remap |= (is_rt_pipe != pdpu->is_rt_pipe);
1147 	pdpu->is_rt_pipe = is_rt_pipe;
1148 
1149 	DPU_DEBUG_PLANE(pdpu, "FB[%u] " DRM_RECT_FP_FMT "->crtc%u " DRM_RECT_FMT
1150 			", %4.4s ubwc %d\n", fb->base.id, DRM_RECT_FP_ARG(&state->src),
1151 			crtc->base.id, DRM_RECT_ARG(&state->dst),
1152 			(char *)&fmt->base.pixel_format, DPU_FORMAT_IS_UBWC(fmt));
1153 
1154 	dpu_plane_sspp_update_pipe(plane, pipe, pipe_cfg, fmt,
1155 				   drm_mode_vrefresh(&crtc->mode),
1156 				   layout_valid ? &layout : NULL);
1157 
1158 	if (r_pipe->sspp) {
1159 		dpu_plane_sspp_update_pipe(plane, r_pipe, r_pipe_cfg, fmt,
1160 					   drm_mode_vrefresh(&crtc->mode),
1161 					   layout_valid ? &layout : NULL);
1162 	}
1163 
1164 	if (pstate->needs_qos_remap)
1165 		pstate->needs_qos_remap = false;
1166 
1167 	pstate->plane_fetch_bw = _dpu_plane_calc_bw(pdpu->catalog, fmt,
1168 						    &crtc->mode, pipe_cfg);
1169 
1170 	pstate->plane_clk = _dpu_plane_calc_clk(&crtc->mode, pipe_cfg);
1171 
1172 	if (r_pipe->sspp) {
1173 		pstate->plane_fetch_bw += _dpu_plane_calc_bw(pdpu->catalog, fmt, &crtc->mode, r_pipe_cfg);
1174 
1175 		pstate->plane_clk = max(pstate->plane_clk, _dpu_plane_calc_clk(&crtc->mode, r_pipe_cfg));
1176 	}
1177 }
1178 
1179 static void _dpu_plane_atomic_disable(struct drm_plane *plane)
1180 {
1181 	struct drm_plane_state *state = plane->state;
1182 	struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1183 	struct dpu_sw_pipe *r_pipe = &pstate->r_pipe;
1184 
1185 	trace_dpu_plane_disable(DRMID(plane), false,
1186 				pstate->pipe.multirect_mode);
1187 
1188 	if (r_pipe->sspp) {
1189 		r_pipe->multirect_index = DPU_SSPP_RECT_SOLO;
1190 		r_pipe->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
1191 
1192 		if (r_pipe->sspp->ops.setup_multirect)
1193 			r_pipe->sspp->ops.setup_multirect(r_pipe);
1194 	}
1195 
1196 	pstate->pending = true;
1197 }
1198 
1199 static void dpu_plane_atomic_update(struct drm_plane *plane,
1200 				struct drm_atomic_state *state)
1201 {
1202 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1203 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
1204 									   plane);
1205 
1206 	pdpu->is_error = false;
1207 
1208 	DPU_DEBUG_PLANE(pdpu, "\n");
1209 
1210 	if (!new_state->visible) {
1211 		_dpu_plane_atomic_disable(plane);
1212 	} else {
1213 		dpu_plane_sspp_atomic_update(plane);
1214 	}
1215 }
1216 
1217 static void dpu_plane_destroy_state(struct drm_plane *plane,
1218 		struct drm_plane_state *state)
1219 {
1220 	__drm_atomic_helper_plane_destroy_state(state);
1221 	kfree(to_dpu_plane_state(state));
1222 }
1223 
1224 static struct drm_plane_state *
1225 dpu_plane_duplicate_state(struct drm_plane *plane)
1226 {
1227 	struct dpu_plane *pdpu;
1228 	struct dpu_plane_state *pstate;
1229 	struct dpu_plane_state *old_state;
1230 
1231 	if (!plane) {
1232 		DPU_ERROR("invalid plane\n");
1233 		return NULL;
1234 	} else if (!plane->state) {
1235 		DPU_ERROR("invalid plane state\n");
1236 		return NULL;
1237 	}
1238 
1239 	old_state = to_dpu_plane_state(plane->state);
1240 	pdpu = to_dpu_plane(plane);
1241 	pstate = kmemdup(old_state, sizeof(*old_state), GFP_KERNEL);
1242 	if (!pstate) {
1243 		DPU_ERROR_PLANE(pdpu, "failed to allocate state\n");
1244 		return NULL;
1245 	}
1246 
1247 	DPU_DEBUG_PLANE(pdpu, "\n");
1248 
1249 	pstate->pending = false;
1250 
1251 	__drm_atomic_helper_plane_duplicate_state(plane, &pstate->base);
1252 
1253 	return &pstate->base;
1254 }
1255 
1256 static const char * const multirect_mode_name[] = {
1257 	[DPU_SSPP_MULTIRECT_NONE] = "none",
1258 	[DPU_SSPP_MULTIRECT_PARALLEL] = "parallel",
1259 	[DPU_SSPP_MULTIRECT_TIME_MX] = "time_mx",
1260 };
1261 
1262 static const char * const multirect_index_name[] = {
1263 	[DPU_SSPP_RECT_SOLO] = "solo",
1264 	[DPU_SSPP_RECT_0] = "rect_0",
1265 	[DPU_SSPP_RECT_1] = "rect_1",
1266 };
1267 
1268 static const char *dpu_get_multirect_mode(enum dpu_sspp_multirect_mode mode)
1269 {
1270 	if (WARN_ON(mode >= ARRAY_SIZE(multirect_mode_name)))
1271 		return "unknown";
1272 
1273 	return multirect_mode_name[mode];
1274 }
1275 
1276 static const char *dpu_get_multirect_index(enum dpu_sspp_multirect_index index)
1277 {
1278 	if (WARN_ON(index >= ARRAY_SIZE(multirect_index_name)))
1279 		return "unknown";
1280 
1281 	return multirect_index_name[index];
1282 }
1283 
1284 static void dpu_plane_atomic_print_state(struct drm_printer *p,
1285 		const struct drm_plane_state *state)
1286 {
1287 	const struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1288 	const struct dpu_sw_pipe *pipe = &pstate->pipe;
1289 	const struct dpu_sw_pipe_cfg *pipe_cfg = &pstate->pipe_cfg;
1290 	const struct dpu_sw_pipe *r_pipe = &pstate->r_pipe;
1291 	const struct dpu_sw_pipe_cfg *r_pipe_cfg = &pstate->r_pipe_cfg;
1292 
1293 	drm_printf(p, "\tstage=%d\n", pstate->stage);
1294 
1295 	drm_printf(p, "\tsspp[0]=%s\n", pipe->sspp->cap->name);
1296 	drm_printf(p, "\tmultirect_mode[0]=%s\n", dpu_get_multirect_mode(pipe->multirect_mode));
1297 	drm_printf(p, "\tmultirect_index[0]=%s\n",
1298 		   dpu_get_multirect_index(pipe->multirect_index));
1299 	drm_printf(p, "\tsrc[0]=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&pipe_cfg->src_rect));
1300 	drm_printf(p, "\tdst[0]=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&pipe_cfg->dst_rect));
1301 
1302 	if (r_pipe->sspp) {
1303 		drm_printf(p, "\tsspp[1]=%s\n", r_pipe->sspp->cap->name);
1304 		drm_printf(p, "\tmultirect_mode[1]=%s\n",
1305 			   dpu_get_multirect_mode(r_pipe->multirect_mode));
1306 		drm_printf(p, "\tmultirect_index[1]=%s\n",
1307 			   dpu_get_multirect_index(r_pipe->multirect_index));
1308 		drm_printf(p, "\tsrc[1]=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&r_pipe_cfg->src_rect));
1309 		drm_printf(p, "\tdst[1]=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&r_pipe_cfg->dst_rect));
1310 	}
1311 }
1312 
1313 static void dpu_plane_reset(struct drm_plane *plane)
1314 {
1315 	struct dpu_plane *pdpu;
1316 	struct dpu_plane_state *pstate;
1317 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
1318 
1319 	if (!plane) {
1320 		DPU_ERROR("invalid plane\n");
1321 		return;
1322 	}
1323 
1324 	pdpu = to_dpu_plane(plane);
1325 	DPU_DEBUG_PLANE(pdpu, "\n");
1326 
1327 	/* remove previous state, if present */
1328 	if (plane->state) {
1329 		dpu_plane_destroy_state(plane, plane->state);
1330 		plane->state = NULL;
1331 	}
1332 
1333 	pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
1334 	if (!pstate) {
1335 		DPU_ERROR_PLANE(pdpu, "failed to allocate state\n");
1336 		return;
1337 	}
1338 
1339 	/*
1340 	 * Set the SSPP here until we have proper virtualized DPU planes.
1341 	 * This is the place where the state is allocated, so fill it fully.
1342 	 */
1343 	pstate->pipe.sspp = dpu_rm_get_sspp(&dpu_kms->rm, pdpu->pipe);
1344 	pstate->pipe.multirect_index = DPU_SSPP_RECT_SOLO;
1345 	pstate->pipe.multirect_mode = DPU_SSPP_MULTIRECT_NONE;
1346 
1347 	pstate->r_pipe.sspp = NULL;
1348 
1349 	__drm_atomic_helper_plane_reset(plane, &pstate->base);
1350 }
1351 
1352 #ifdef CONFIG_DEBUG_FS
1353 void dpu_plane_danger_signal_ctrl(struct drm_plane *plane, bool enable)
1354 {
1355 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1356 	struct dpu_plane_state *pstate = to_dpu_plane_state(plane->state);
1357 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
1358 
1359 	if (!pdpu->is_rt_pipe)
1360 		return;
1361 
1362 	pm_runtime_get_sync(&dpu_kms->pdev->dev);
1363 	_dpu_plane_set_qos_ctrl(plane, &pstate->pipe, enable);
1364 	if (pstate->r_pipe.sspp)
1365 		_dpu_plane_set_qos_ctrl(plane, &pstate->r_pipe, enable);
1366 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
1367 }
1368 #endif
1369 
1370 static bool dpu_plane_format_mod_supported(struct drm_plane *plane,
1371 		uint32_t format, uint64_t modifier)
1372 {
1373 	if (modifier == DRM_FORMAT_MOD_LINEAR)
1374 		return true;
1375 
1376 	if (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED)
1377 		return dpu_find_format(format, qcom_compressed_supported_formats,
1378 				ARRAY_SIZE(qcom_compressed_supported_formats));
1379 
1380 	return false;
1381 }
1382 
1383 static const struct drm_plane_funcs dpu_plane_funcs = {
1384 		.update_plane = drm_atomic_helper_update_plane,
1385 		.disable_plane = drm_atomic_helper_disable_plane,
1386 		.reset = dpu_plane_reset,
1387 		.atomic_duplicate_state = dpu_plane_duplicate_state,
1388 		.atomic_destroy_state = dpu_plane_destroy_state,
1389 		.atomic_print_state = dpu_plane_atomic_print_state,
1390 		.format_mod_supported = dpu_plane_format_mod_supported,
1391 };
1392 
1393 static const struct drm_plane_helper_funcs dpu_plane_helper_funcs = {
1394 		.prepare_fb = dpu_plane_prepare_fb,
1395 		.cleanup_fb = dpu_plane_cleanup_fb,
1396 		.atomic_check = dpu_plane_atomic_check,
1397 		.atomic_update = dpu_plane_atomic_update,
1398 };
1399 
1400 /* initialize plane */
1401 struct drm_plane *dpu_plane_init(struct drm_device *dev,
1402 		uint32_t pipe, enum drm_plane_type type,
1403 		unsigned long possible_crtcs)
1404 {
1405 	struct drm_plane *plane = NULL;
1406 	const uint32_t *format_list;
1407 	struct dpu_plane *pdpu;
1408 	struct msm_drm_private *priv = dev->dev_private;
1409 	struct dpu_kms *kms = to_dpu_kms(priv->kms);
1410 	struct dpu_hw_sspp *pipe_hw;
1411 	uint32_t num_formats;
1412 	uint32_t supported_rotations;
1413 	int ret;
1414 
1415 	/* initialize underlying h/w driver */
1416 	pipe_hw = dpu_rm_get_sspp(&kms->rm, pipe);
1417 	if (!pipe_hw || !pipe_hw->cap || !pipe_hw->cap->sblk) {
1418 		DPU_ERROR("[%u]SSPP is invalid\n", pipe);
1419 		return ERR_PTR(-EINVAL);
1420 	}
1421 
1422 	format_list = pipe_hw->cap->sblk->format_list;
1423 	num_formats = pipe_hw->cap->sblk->num_formats;
1424 
1425 	pdpu = drmm_universal_plane_alloc(dev, struct dpu_plane, base,
1426 				0xff, &dpu_plane_funcs,
1427 				format_list, num_formats,
1428 				supported_format_modifiers, type, NULL);
1429 	if (IS_ERR(pdpu))
1430 		return ERR_CAST(pdpu);
1431 
1432 	/* cache local stuff for later */
1433 	plane = &pdpu->base;
1434 	pdpu->pipe = pipe;
1435 
1436 	pdpu->catalog = kms->catalog;
1437 
1438 	ret = drm_plane_create_zpos_property(plane, 0, 0, DPU_ZPOS_MAX);
1439 	if (ret)
1440 		DPU_ERROR("failed to install zpos property, rc = %d\n", ret);
1441 
1442 	drm_plane_create_alpha_property(plane);
1443 	drm_plane_create_blend_mode_property(plane,
1444 			BIT(DRM_MODE_BLEND_PIXEL_NONE) |
1445 			BIT(DRM_MODE_BLEND_PREMULTI) |
1446 			BIT(DRM_MODE_BLEND_COVERAGE));
1447 
1448 	supported_rotations = DRM_MODE_REFLECT_MASK | DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
1449 
1450 	if (pipe_hw->cap->features & BIT(DPU_SSPP_INLINE_ROTATION))
1451 		supported_rotations |= DRM_MODE_ROTATE_MASK;
1452 
1453 	drm_plane_create_rotation_property(plane,
1454 		    DRM_MODE_ROTATE_0, supported_rotations);
1455 
1456 	drm_plane_enable_fb_damage_clips(plane);
1457 
1458 	/* success! finalize initialization */
1459 	drm_plane_helper_add(plane, &dpu_plane_helper_funcs);
1460 
1461 	DPU_DEBUG("%s created for pipe:%u id:%u\n", plane->name,
1462 					pipe, plane->base.id);
1463 	return plane;
1464 }
1465