xref: /linux/drivers/gpu/drm/msm/disp/dpu1/dpu_plane.c (revision fbf5df34a4dbcd09d433dd4f0916bf9b2ddb16de)
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 <linux/soc/qcom/ubwc.h>
21 
22 #include "msm_drv.h"
23 #include "dpu_kms.h"
24 #include "dpu_hw_sspp.h"
25 #include "dpu_hw_util.h"
26 #include "dpu_trace.h"
27 #include "dpu_crtc.h"
28 #include "dpu_vbif.h"
29 #include "dpu_plane.h"
30 
31 #define DPU_DEBUG_PLANE(pl, fmt, ...) DRM_DEBUG_ATOMIC("plane%d " fmt,\
32 		(pl) ? (pl)->base.base.id : -1, ##__VA_ARGS__)
33 
34 #define DPU_ERROR_PLANE(pl, fmt, ...) DPU_ERROR("plane%d " fmt,\
35 		(pl) ? (pl)->base.base.id : -1, ##__VA_ARGS__)
36 
37 #define DECIMATED_DIMENSION(dim, deci) (((dim) + ((1 << (deci)) - 1)) >> (deci))
38 #define PHASE_STEP_SHIFT	21
39 #define PHASE_STEP_UNIT_SCALE   ((int) (1 << PHASE_STEP_SHIFT))
40 #define PHASE_RESIDUAL		15
41 
42 #define SHARP_STRENGTH_DEFAULT	32
43 #define SHARP_EDGE_THR_DEFAULT	112
44 #define SHARP_SMOOTH_THR_DEFAULT	8
45 #define SHARP_NOISE_THR_DEFAULT	2
46 
47 #define DPU_PLANE_COLOR_FILL_FLAG	BIT(31)
48 #define DPU_ZPOS_MAX 255
49 
50 /*
51  * Default Preload Values
52  */
53 #define DPU_QSEED3_DEFAULT_PRELOAD_H 0x4
54 #define DPU_QSEED3_DEFAULT_PRELOAD_V 0x3
55 #define DPU_QSEED4_DEFAULT_PRELOAD_V 0x2
56 #define DPU_QSEED4_DEFAULT_PRELOAD_H 0x4
57 
58 #define DEFAULT_REFRESH_RATE	60
59 
60 static const uint32_t qcom_compressed_supported_formats[] = {
61 	DRM_FORMAT_ABGR8888,
62 	DRM_FORMAT_ARGB8888,
63 	DRM_FORMAT_XBGR8888,
64 	DRM_FORMAT_XRGB8888,
65 	DRM_FORMAT_ARGB2101010,
66 	DRM_FORMAT_XRGB2101010,
67 	DRM_FORMAT_BGR565,
68 
69 	DRM_FORMAT_NV12,
70 	DRM_FORMAT_P010,
71 };
72 
73 /*
74  * struct dpu_plane - local dpu plane structure
75  * @vm: address space pointer
76  * @csc_ptr: Points to dpu_csc_cfg structure to use for current
77  * @catalog: Points to dpu catalog structure
78  * @revalidate: force revalidation of all the plane properties
79  */
80 struct dpu_plane {
81 	struct drm_plane base;
82 
83 	enum dpu_sspp pipe;
84 
85 	uint32_t color_fill;
86 	bool is_error;
87 	bool is_rt_pipe;
88 	const struct dpu_mdss_cfg *catalog;
89 };
90 
91 static const uint64_t supported_format_modifiers[] = {
92 	DRM_FORMAT_MOD_QCOM_COMPRESSED,
93 	DRM_FORMAT_MOD_LINEAR,
94 	DRM_FORMAT_MOD_INVALID
95 };
96 
97 #define to_dpu_plane(x) container_of(x, struct dpu_plane, base)
98 
99 static struct dpu_kms *_dpu_plane_get_kms(struct drm_plane *plane)
100 {
101 	struct msm_drm_private *priv = plane->dev->dev_private;
102 
103 	return to_dpu_kms(priv->kms);
104 }
105 
106 /**
107  * _dpu_plane_calc_bw - calculate bandwidth required for a plane
108  * @catalog: Points to dpu catalog structure
109  * @fmt: Pointer to source buffer format
110  * @mode: Pointer to drm display mode
111  * @pipe_cfg: Pointer to pipe configuration
112  * Result: Updates calculated bandwidth in the plane state.
113  * BW Equation: src_w * src_h * bpp * fps * (v_total / v_dest)
114  * Prefill BW Equation: line src bytes * line_time
115  */
116 static u64 _dpu_plane_calc_bw(const struct dpu_mdss_cfg *catalog,
117 	const struct msm_format *fmt,
118 	const struct drm_display_mode *mode,
119 	struct dpu_sw_pipe_cfg *pipe_cfg)
120 {
121 	int src_width, src_height, dst_height, fps;
122 	u64 plane_pixel_rate, plane_bit_rate;
123 	u64 plane_prefill_bw;
124 	u64 plane_bw;
125 	u32 hw_latency_lines;
126 	u64 scale_factor;
127 	int vbp, vpw, vfp;
128 
129 	src_width = drm_rect_width(&pipe_cfg->src_rect);
130 	src_height = drm_rect_height(&pipe_cfg->src_rect);
131 	dst_height = drm_rect_height(&pipe_cfg->dst_rect);
132 	fps = drm_mode_vrefresh(mode);
133 	vbp = mode->vtotal - mode->vsync_end;
134 	vpw = mode->vsync_end - mode->vsync_start;
135 	vfp = mode->vsync_start - mode->vdisplay;
136 	hw_latency_lines =  catalog->perf->min_prefill_lines;
137 	scale_factor = src_height > dst_height ?
138 		mult_frac(src_height, 1, dst_height) : 1;
139 
140 	plane_pixel_rate = src_width * mode->vtotal * fps;
141 	plane_bit_rate = plane_pixel_rate * fmt->bpp;
142 
143 	plane_bw = plane_bit_rate * scale_factor;
144 
145 	plane_prefill_bw = plane_bw * hw_latency_lines;
146 
147 	if ((vbp+vpw) > hw_latency_lines)
148 		do_div(plane_prefill_bw, (vbp+vpw));
149 	else if ((vbp+vpw+vfp) < hw_latency_lines)
150 		do_div(plane_prefill_bw, (vbp+vpw+vfp));
151 	else
152 		do_div(plane_prefill_bw, hw_latency_lines);
153 
154 
155 	return max(plane_bw, plane_prefill_bw);
156 }
157 
158 /**
159  * _dpu_plane_calc_clk - calculate clock required for a plane
160  * @mode: Pointer to drm display mode
161  * @pipe_cfg: Pointer to pipe configuration
162  * Result: Updates calculated clock in the plane state.
163  * Clock equation: dst_w * v_total * fps * (src_h / dst_h)
164  */
165 static u64 _dpu_plane_calc_clk(const struct drm_display_mode *mode,
166 		struct dpu_sw_pipe_cfg *pipe_cfg)
167 {
168 	int dst_width, src_height, dst_height, fps;
169 	u64 plane_clk;
170 
171 	src_height = drm_rect_height(&pipe_cfg->src_rect);
172 	dst_width = drm_rect_width(&pipe_cfg->dst_rect);
173 	dst_height = drm_rect_height(&pipe_cfg->dst_rect);
174 	fps = drm_mode_vrefresh(mode);
175 
176 	plane_clk =
177 		dst_width * mode->vtotal * fps;
178 
179 	if (src_height > dst_height) {
180 		plane_clk *= src_height;
181 		do_div(plane_clk, dst_height);
182 	}
183 
184 	return plane_clk;
185 }
186 
187 /**
188  * _dpu_plane_calc_fill_level - calculate fill level of the given source format
189  * @plane:		Pointer to drm plane
190  * @pipe:		Pointer to software pipe
191  * @lut_usage:		LUT usecase
192  * @fmt:		Pointer to source buffer format
193  * @src_width:		width of source buffer
194  * Return: fill level corresponding to the source buffer/format or 0 if error
195  */
196 static int _dpu_plane_calc_fill_level(struct drm_plane *plane,
197 		struct dpu_sw_pipe *pipe,
198 		enum dpu_qos_lut_usage lut_usage,
199 		const struct msm_format *fmt, u32 src_width)
200 {
201 	struct dpu_plane *pdpu;
202 	u32 fixed_buff_size;
203 	u32 total_fl;
204 
205 	if (!fmt || !pipe || !src_width || !fmt->bpp) {
206 		DPU_ERROR("invalid arguments\n");
207 		return 0;
208 	}
209 
210 	if (lut_usage == DPU_QOS_LUT_USAGE_NRT)
211 		return 0;
212 
213 	pdpu = to_dpu_plane(plane);
214 	fixed_buff_size = pdpu->catalog->caps->pixel_ram_size;
215 
216 	/* FIXME: in multirect case account for the src_width of all the planes */
217 
218 	if (fmt->fetch_type == MDP_PLANE_PSEUDO_PLANAR) {
219 		if (fmt->chroma_sample == CHROMA_420) {
220 			/* NV12 */
221 			total_fl = (fixed_buff_size / 2) /
222 				((src_width + 32) * fmt->bpp);
223 		} else {
224 			/* non NV12 */
225 			total_fl = (fixed_buff_size / 2) * 2 /
226 				((src_width + 32) * fmt->bpp);
227 		}
228 	} else {
229 		if (pipe->multirect_mode == DPU_SSPP_MULTIRECT_PARALLEL) {
230 			total_fl = (fixed_buff_size / 2) * 2 /
231 				((src_width + 32) * fmt->bpp);
232 		} else {
233 			total_fl = (fixed_buff_size) * 2 /
234 				((src_width + 32) * fmt->bpp);
235 		}
236 	}
237 
238 	DPU_DEBUG_PLANE(pdpu, "pnum:%d fmt: %p4cc w:%u fl:%u\n",
239 			pipe->sspp->idx - SSPP_VIG0,
240 			&fmt->pixel_format,
241 			src_width, total_fl);
242 
243 	return total_fl;
244 }
245 
246 /**
247  * _dpu_plane_set_qos_lut - set QoS LUT of the given plane
248  * @plane:		Pointer to drm plane
249  * @pipe:		Pointer to software pipe
250  * @fmt:		Pointer to source buffer format
251  * @pipe_cfg:		Pointer to pipe configuration
252  */
253 static void _dpu_plane_set_qos_lut(struct drm_plane *plane,
254 		struct dpu_sw_pipe *pipe,
255 		const struct msm_format *fmt, struct dpu_sw_pipe_cfg *pipe_cfg)
256 {
257 	struct dpu_plane *pdpu = to_dpu_plane(plane);
258 	struct dpu_hw_qos_cfg cfg;
259 	u32 total_fl, lut_usage;
260 
261 	if (!pdpu->is_rt_pipe) {
262 		lut_usage = DPU_QOS_LUT_USAGE_NRT;
263 	} else {
264 		if (fmt && MSM_FORMAT_IS_LINEAR(fmt))
265 			lut_usage = DPU_QOS_LUT_USAGE_LINEAR;
266 		else
267 			lut_usage = DPU_QOS_LUT_USAGE_MACROTILE;
268 	}
269 
270 	total_fl = _dpu_plane_calc_fill_level(plane, pipe, lut_usage, fmt,
271 				drm_rect_width(&pipe_cfg->src_rect));
272 
273 	cfg.creq_lut = _dpu_hw_get_qos_lut(&pdpu->catalog->perf->qos_lut_tbl[lut_usage], total_fl);
274 	cfg.danger_lut = pdpu->catalog->perf->danger_lut_tbl[lut_usage];
275 	cfg.safe_lut = pdpu->catalog->perf->safe_lut_tbl[lut_usage];
276 
277 	if (pipe->sspp->idx != SSPP_CURSOR0 &&
278 	    pipe->sspp->idx != SSPP_CURSOR1 &&
279 	    pdpu->is_rt_pipe)
280 		cfg.danger_safe_en = true;
281 
282 	DPU_DEBUG_PLANE(pdpu, "pnum:%d ds:%d is_rt:%d\n",
283 		pdpu->pipe - SSPP_VIG0,
284 		cfg.danger_safe_en,
285 		pdpu->is_rt_pipe);
286 
287 	trace_dpu_perf_set_qos_luts(pipe->sspp->idx - SSPP_VIG0,
288 			(fmt) ? fmt->pixel_format : 0,
289 			pdpu->is_rt_pipe, total_fl, cfg.creq_lut, lut_usage);
290 
291 	DPU_DEBUG_PLANE(pdpu, "pnum:%d fmt: %p4cc rt:%d fl:%u lut:0x%llx\n",
292 			pdpu->pipe - SSPP_VIG0,
293 			fmt ? &fmt->pixel_format : NULL,
294 			pdpu->is_rt_pipe, total_fl, cfg.creq_lut);
295 
296 	trace_dpu_perf_set_danger_luts(pdpu->pipe - SSPP_VIG0,
297 			(fmt) ? fmt->pixel_format : 0,
298 			(fmt) ? fmt->fetch_mode : 0,
299 			cfg.danger_lut,
300 			cfg.safe_lut);
301 
302 	DPU_DEBUG_PLANE(pdpu, "pnum:%d fmt: %p4cc mode:%d luts[0x%x, 0x%x]\n",
303 			pdpu->pipe - SSPP_VIG0,
304 			fmt ? &fmt->pixel_format : NULL,
305 			fmt ? fmt->fetch_mode : -1,
306 			cfg.danger_lut,
307 			cfg.safe_lut);
308 
309 	pipe->sspp->ops.setup_qos_lut(pipe->sspp, &cfg);
310 }
311 
312 /**
313  * _dpu_plane_set_qos_ctrl - set QoS control of the given plane
314  * @plane:		Pointer to drm plane
315  * @pipe:		Pointer to software pipe
316  * @enable:		true to enable QoS control
317  */
318 static void _dpu_plane_set_qos_ctrl(struct drm_plane *plane,
319 	struct dpu_sw_pipe *pipe,
320 	bool enable)
321 {
322 	struct dpu_plane *pdpu = to_dpu_plane(plane);
323 
324 	if (!pdpu->is_rt_pipe)
325 		enable = false;
326 
327 	DPU_DEBUG_PLANE(pdpu, "pnum:%d ds:%d is_rt:%d\n",
328 		pdpu->pipe - SSPP_VIG0,
329 		enable,
330 		pdpu->is_rt_pipe);
331 
332 	pipe->sspp->ops.setup_qos_ctrl(pipe->sspp,
333 				       enable);
334 }
335 
336 static bool _dpu_plane_sspp_clk_force_ctrl(struct dpu_hw_sspp *sspp,
337 					   struct dpu_hw_mdp *mdp,
338 					   bool enable, bool *forced_on)
339 {
340 	if (sspp->ops.setup_clk_force_ctrl) {
341 		*forced_on = sspp->ops.setup_clk_force_ctrl(sspp, enable);
342 		return true;
343 	}
344 
345 	if (mdp->ops.setup_clk_force_ctrl) {
346 		*forced_on = mdp->ops.setup_clk_force_ctrl(mdp, sspp->cap->clk_ctrl, enable);
347 		return true;
348 	}
349 
350 	return false;
351 }
352 
353 /**
354  * _dpu_plane_set_ot_limit - set OT limit for the given plane
355  * @plane:		Pointer to drm plane
356  * @pipe:		Pointer to software pipe
357  * @pipe_cfg:		Pointer to pipe configuration
358  * @frame_rate:		CRTC's frame rate
359  */
360 static void _dpu_plane_set_ot_limit(struct drm_plane *plane,
361 		struct dpu_sw_pipe *pipe,
362 		struct dpu_sw_pipe_cfg *pipe_cfg,
363 		int frame_rate)
364 {
365 	struct dpu_plane *pdpu = to_dpu_plane(plane);
366 	struct dpu_vbif_set_ot_params ot_params;
367 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
368 	bool forced_on = false;
369 
370 	memset(&ot_params, 0, sizeof(ot_params));
371 	ot_params.xin_id = pipe->sspp->cap->xin_id;
372 	ot_params.num = pipe->sspp->idx - SSPP_NONE;
373 	ot_params.width = drm_rect_width(&pipe_cfg->src_rect);
374 	ot_params.height = drm_rect_height(&pipe_cfg->src_rect);
375 	ot_params.is_wfd = !pdpu->is_rt_pipe;
376 	ot_params.frame_rate = frame_rate;
377 	ot_params.rd = true;
378 
379 	if (!_dpu_plane_sspp_clk_force_ctrl(pipe->sspp, dpu_kms->hw_mdp,
380 					    true, &forced_on))
381 		return;
382 
383 	dpu_vbif_set_ot_limit(dpu_kms, &ot_params);
384 
385 	if (forced_on)
386 		_dpu_plane_sspp_clk_force_ctrl(pipe->sspp, dpu_kms->hw_mdp,
387 					       false, &forced_on);
388 }
389 
390 /**
391  * _dpu_plane_set_qos_remap - set vbif QoS for the given plane
392  * @plane:		Pointer to drm plane
393  * @pipe:		Pointer to software pipe
394  */
395 static void _dpu_plane_set_qos_remap(struct drm_plane *plane,
396 		struct dpu_sw_pipe *pipe)
397 {
398 	struct dpu_plane *pdpu = to_dpu_plane(plane);
399 	struct dpu_vbif_set_qos_params qos_params;
400 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
401 	bool forced_on = false;
402 
403 	memset(&qos_params, 0, sizeof(qos_params));
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 xin:%d rt:%d\n",
409 			qos_params.num,
410 			qos_params.xin_id, qos_params.is_rt);
411 
412 	if (!_dpu_plane_sspp_clk_force_ctrl(pipe->sspp, dpu_kms->hw_mdp,
413 					    true, &forced_on))
414 		return;
415 
416 	dpu_vbif_set_qos_remap(dpu_kms, &qos_params);
417 
418 	if (forced_on)
419 		_dpu_plane_sspp_clk_force_ctrl(pipe->sspp, dpu_kms->hw_mdp,
420 					       false, &forced_on);
421 }
422 
423 static void _dpu_plane_setup_scaler3(struct dpu_hw_sspp *pipe_hw,
424 		uint32_t src_w, uint32_t src_h, uint32_t dst_w, uint32_t dst_h,
425 		struct dpu_hw_scaler3_cfg *scale_cfg,
426 		const struct msm_format *fmt,
427 		uint32_t chroma_subsmpl_h, uint32_t chroma_subsmpl_v,
428 		unsigned int rotation)
429 {
430 	uint32_t i;
431 	bool inline_rotation = rotation & DRM_MODE_ROTATE_90;
432 
433 	/*
434 	 * For inline rotation cases, scaler config is post-rotation,
435 	 * so swap the dimensions here. However, pixel extension will
436 	 * need pre-rotation settings.
437 	 */
438 	if (inline_rotation)
439 		swap(src_w, src_h);
440 
441 	scale_cfg->phase_step_x[DPU_SSPP_COMP_0] =
442 		mult_frac((1 << PHASE_STEP_SHIFT), src_w, dst_w);
443 	scale_cfg->phase_step_y[DPU_SSPP_COMP_0] =
444 		mult_frac((1 << PHASE_STEP_SHIFT), src_h, dst_h);
445 
446 
447 	scale_cfg->phase_step_y[DPU_SSPP_COMP_1_2] =
448 		scale_cfg->phase_step_y[DPU_SSPP_COMP_0] / chroma_subsmpl_v;
449 	scale_cfg->phase_step_x[DPU_SSPP_COMP_1_2] =
450 		scale_cfg->phase_step_x[DPU_SSPP_COMP_0] / chroma_subsmpl_h;
451 
452 	scale_cfg->phase_step_x[DPU_SSPP_COMP_2] =
453 		scale_cfg->phase_step_x[DPU_SSPP_COMP_1_2];
454 	scale_cfg->phase_step_y[DPU_SSPP_COMP_2] =
455 		scale_cfg->phase_step_y[DPU_SSPP_COMP_1_2];
456 
457 	scale_cfg->phase_step_x[DPU_SSPP_COMP_3] =
458 		scale_cfg->phase_step_x[DPU_SSPP_COMP_0];
459 	scale_cfg->phase_step_y[DPU_SSPP_COMP_3] =
460 		scale_cfg->phase_step_y[DPU_SSPP_COMP_0];
461 
462 	for (i = 0; i < DPU_MAX_PLANES; i++) {
463 		scale_cfg->src_width[i] = src_w;
464 		scale_cfg->src_height[i] = src_h;
465 		if (i == DPU_SSPP_COMP_1_2 || i == DPU_SSPP_COMP_2) {
466 			scale_cfg->src_width[i] /= chroma_subsmpl_h;
467 			scale_cfg->src_height[i] /= chroma_subsmpl_v;
468 		}
469 
470 		if (pipe_hw->cap->sblk->scaler_blk.version >= 0x3000) {
471 			scale_cfg->preload_x[i] = DPU_QSEED4_DEFAULT_PRELOAD_H;
472 			scale_cfg->preload_y[i] = DPU_QSEED4_DEFAULT_PRELOAD_V;
473 		} else {
474 			scale_cfg->preload_x[i] = DPU_QSEED3_DEFAULT_PRELOAD_H;
475 			scale_cfg->preload_y[i] = DPU_QSEED3_DEFAULT_PRELOAD_V;
476 		}
477 	}
478 	if (!(MSM_FORMAT_IS_YUV(fmt)) && (src_h == dst_h)
479 		&& (src_w == dst_w))
480 		return;
481 
482 	scale_cfg->dst_width = dst_w;
483 	scale_cfg->dst_height = dst_h;
484 	scale_cfg->y_rgb_filter_cfg = DPU_SCALE_BIL;
485 	scale_cfg->uv_filter_cfg = DPU_SCALE_BIL;
486 	scale_cfg->alpha_filter_cfg = DPU_SCALE_ALPHA_BIL;
487 	scale_cfg->lut_flag = 0;
488 	scale_cfg->blend_cfg = 1;
489 	scale_cfg->enable = 1;
490 }
491 
492 static void _dpu_plane_setup_pixel_ext(struct dpu_hw_scaler3_cfg *scale_cfg,
493 				struct dpu_hw_pixel_ext *pixel_ext,
494 				uint32_t src_w, uint32_t src_h,
495 				uint32_t chroma_subsmpl_h, uint32_t chroma_subsmpl_v)
496 {
497 	int i;
498 
499 	for (i = 0; i < DPU_MAX_PLANES; i++) {
500 		uint32_t w = src_w, h = src_h;
501 
502 		if (i == DPU_SSPP_COMP_1_2 || i == DPU_SSPP_COMP_2) {
503 			w /= chroma_subsmpl_h;
504 			h /= chroma_subsmpl_v;
505 		}
506 
507 		pixel_ext->num_ext_pxls_top[i] = h;
508 		pixel_ext->num_ext_pxls_left[i] = w;
509 	}
510 }
511 
512 static const struct dpu_csc_cfg *_dpu_plane_get_csc(struct dpu_sw_pipe *pipe,
513 						    const struct msm_format *fmt)
514 {
515 	const struct dpu_csc_cfg *csc_ptr;
516 
517 	if (!MSM_FORMAT_IS_YUV(fmt))
518 		return NULL;
519 
520 	if (BIT(DPU_SSPP_CSC_10BIT) & pipe->sspp->cap->features)
521 		csc_ptr = &dpu_csc10_YUV2RGB_601L;
522 	else
523 		csc_ptr = &dpu_csc_YUV2RGB_601L;
524 
525 	return csc_ptr;
526 }
527 
528 static void _dpu_plane_setup_scaler(struct dpu_sw_pipe *pipe,
529 		const struct msm_format *fmt, bool color_fill,
530 		struct dpu_sw_pipe_cfg *pipe_cfg)
531 {
532 	struct dpu_hw_sspp *pipe_hw = pipe->sspp;
533 	const struct drm_format_info *info = drm_format_info(fmt->pixel_format);
534 	struct dpu_hw_scaler3_cfg scaler3_cfg;
535 	struct dpu_hw_pixel_ext pixel_ext;
536 	u32 src_width = drm_rect_width(&pipe_cfg->src_rect);
537 	u32 src_height = drm_rect_height(&pipe_cfg->src_rect);
538 	u32 dst_width = drm_rect_width(&pipe_cfg->dst_rect);
539 	u32 dst_height = drm_rect_height(&pipe_cfg->dst_rect);
540 
541 	memset(&scaler3_cfg, 0, sizeof(scaler3_cfg));
542 	memset(&pixel_ext, 0, sizeof(pixel_ext));
543 
544 	/* don't chroma subsample if decimating */
545 	/* update scaler. calculate default config for QSEED3 */
546 	_dpu_plane_setup_scaler3(pipe_hw,
547 			src_width,
548 			src_height,
549 			dst_width,
550 			dst_height,
551 			&scaler3_cfg, fmt,
552 			info->hsub, info->vsub,
553 			pipe_cfg->rotation);
554 
555 	/* configure pixel extension based on scalar config */
556 	_dpu_plane_setup_pixel_ext(&scaler3_cfg, &pixel_ext,
557 			src_width, src_height, info->hsub, info->vsub);
558 
559 	if (pipe_hw->ops.setup_pe)
560 		pipe_hw->ops.setup_pe(pipe_hw,
561 				&pixel_ext);
562 
563 	/**
564 	 * when programmed in multirect mode, scalar block will be
565 	 * bypassed. Still we need to update alpha and bitwidth
566 	 * ONLY for RECT0
567 	 */
568 	if (pipe_hw->ops.setup_scaler &&
569 			pipe->multirect_index != DPU_SSPP_RECT_1)
570 		pipe_hw->ops.setup_scaler(pipe_hw,
571 				&scaler3_cfg,
572 				fmt);
573 }
574 
575 static void _dpu_plane_color_fill_pipe(struct dpu_plane_state *pstate,
576 				       struct dpu_sw_pipe *pipe,
577 				       struct drm_rect *dst_rect,
578 				       u32 fill_color,
579 				       const struct msm_format *fmt)
580 {
581 	struct dpu_sw_pipe_cfg pipe_cfg;
582 
583 	/* update sspp */
584 	if (!pipe->sspp->ops.setup_solidfill)
585 		return;
586 
587 	pipe->sspp->ops.setup_solidfill(pipe, fill_color);
588 
589 	/* override scaler/decimation if solid fill */
590 	pipe_cfg.dst_rect = *dst_rect;
591 
592 	pipe_cfg.src_rect.x1 = 0;
593 	pipe_cfg.src_rect.y1 = 0;
594 	pipe_cfg.src_rect.x2 =
595 		drm_rect_width(&pipe_cfg.dst_rect);
596 	pipe_cfg.src_rect.y2 =
597 		drm_rect_height(&pipe_cfg.dst_rect);
598 
599 	if (pipe->sspp->ops.setup_format)
600 		pipe->sspp->ops.setup_format(pipe, fmt, DPU_SSPP_SOLID_FILL);
601 
602 	if (pipe->sspp->ops.setup_rects)
603 		pipe->sspp->ops.setup_rects(pipe, &pipe_cfg);
604 
605 	_dpu_plane_setup_scaler(pipe, fmt, true, &pipe_cfg);
606 }
607 
608 /**
609  * _dpu_plane_color_fill - enables color fill on plane
610  * @pdpu:   Pointer to DPU plane object
611  * @color:  RGB fill color value, [23..16] Blue, [15..8] Green, [7..0] Red
612  * @alpha:  8-bit fill alpha value, 255 selects 100% alpha
613  */
614 static void _dpu_plane_color_fill(struct dpu_plane *pdpu,
615 		uint32_t color, uint32_t alpha)
616 {
617 	const struct msm_format *fmt;
618 	const struct drm_plane *plane = &pdpu->base;
619 	struct msm_drm_private *priv = plane->dev->dev_private;
620 	struct dpu_plane_state *pstate = to_dpu_plane_state(plane->state);
621 	u32 fill_color = (color & 0xFFFFFF) | ((alpha & 0xFF) << 24);
622 	int i;
623 
624 	DPU_DEBUG_PLANE(pdpu, "\n");
625 
626 	/*
627 	 * select fill format to match user property expectation,
628 	 * h/w only supports RGB variants
629 	 */
630 	fmt = mdp_get_format(priv->kms, DRM_FORMAT_ABGR8888, 0);
631 	/* should not happen ever */
632 	if (!fmt)
633 		return;
634 
635 	/* update sspp */
636 	for (i = 0; i < PIPES_PER_PLANE; i++) {
637 		if (!pstate->pipe[i].sspp)
638 			continue;
639 		_dpu_plane_color_fill_pipe(pstate, &pstate->pipe[i],
640 					   &pstate->pipe_cfg[i].dst_rect,
641 					   fill_color, fmt);
642 	}
643 }
644 
645 static int dpu_plane_prepare_fb(struct drm_plane *plane,
646 		struct drm_plane_state *new_state)
647 {
648 	struct drm_framebuffer *fb = new_state->fb;
649 	struct dpu_plane *pdpu = to_dpu_plane(plane);
650 	struct dpu_plane_state *pstate = to_dpu_plane_state(new_state);
651 	int ret;
652 
653 	if (!new_state->fb)
654 		return 0;
655 
656 	DPU_DEBUG_PLANE(pdpu, "FB[%u]\n", fb->base.id);
657 
658 	/*
659 	 * TODO: Need to sort out the msm_framebuffer_prepare() call below so
660 	 *       we can use msm_atomic_prepare_fb() instead of doing the
661 	 *       implicit fence and fb prepare by hand here.
662 	 */
663 	drm_gem_plane_helper_prepare_fb(plane, new_state);
664 
665 	ret = msm_framebuffer_prepare(new_state->fb, pstate->needs_dirtyfb);
666 	if (ret) {
667 		DPU_ERROR("failed to prepare framebuffer\n");
668 		return ret;
669 	}
670 
671 	return 0;
672 }
673 
674 static void dpu_plane_cleanup_fb(struct drm_plane *plane,
675 		struct drm_plane_state *old_state)
676 {
677 	struct dpu_plane *pdpu = to_dpu_plane(plane);
678 	struct dpu_plane_state *old_pstate;
679 
680 	if (!old_state || !old_state->fb)
681 		return;
682 
683 	old_pstate = to_dpu_plane_state(old_state);
684 
685 	DPU_DEBUG_PLANE(pdpu, "FB[%u]\n", old_state->fb->base.id);
686 
687 	msm_framebuffer_cleanup(old_state->fb, old_pstate->needs_dirtyfb);
688 }
689 
690 static int dpu_plane_check_inline_rotation(struct dpu_plane *pdpu,
691 					   struct dpu_sw_pipe *pipe,
692 					   struct drm_rect src,
693 					   const struct msm_format *fmt)
694 {
695 	const struct dpu_sspp_sub_blks *sblk = pipe->sspp->cap->sblk;
696 	size_t num_formats;
697 	const u32 *supported_formats;
698 
699 	if (!test_bit(DPU_SSPP_INLINE_ROTATION, &pipe->sspp->cap->features))
700 		return -EINVAL;
701 
702 	if (!sblk->rotation_cfg) {
703 		DPU_ERROR("invalid rotation cfg\n");
704 		return -EINVAL;
705 	}
706 
707 	if (drm_rect_width(&src) > sblk->rotation_cfg->rot_maxheight) {
708 		DPU_DEBUG_PLANE(pdpu, "invalid height for inline rot:%d max:%d\n",
709 				src.y2, sblk->rotation_cfg->rot_maxheight);
710 		return -EINVAL;
711 	}
712 
713 	supported_formats = sblk->rotation_cfg->rot_format_list;
714 	num_formats = sblk->rotation_cfg->rot_num_formats;
715 
716 	if (!MSM_FORMAT_IS_UBWC(fmt) ||
717 		!dpu_find_format(fmt->pixel_format, supported_formats, num_formats))
718 		return -EINVAL;
719 
720 	return 0;
721 }
722 
723 static int dpu_plane_atomic_check_pipe(struct dpu_plane *pdpu,
724 		struct dpu_sw_pipe *pipe,
725 		struct dpu_sw_pipe_cfg *pipe_cfg,
726 		const struct drm_display_mode *mode,
727 		struct drm_plane_state *new_plane_state)
728 {
729 	uint32_t min_src_size;
730 	struct dpu_kms *kms = _dpu_plane_get_kms(&pdpu->base);
731 	int ret;
732 	const struct msm_format *fmt;
733 	uint32_t supported_rotations;
734 	const struct dpu_sspp_cfg *pipe_hw_caps;
735 	const struct dpu_sspp_sub_blks *sblk;
736 
737 	pipe_hw_caps = pipe->sspp->cap;
738 	sblk = pipe->sspp->cap->sblk;
739 
740 	/*
741 	 * We already have verified scaling against platform limitations.
742 	 * Now check if the SSPP supports scaling at all.
743 	 */
744 	if (!(sblk->scaler_blk.len && pipe->sspp->ops.setup_scaler) &&
745 	    ((drm_rect_width(&new_plane_state->src) >> 16 !=
746 	      drm_rect_width(&new_plane_state->dst)) ||
747 	     (drm_rect_height(&new_plane_state->src) >> 16 !=
748 	      drm_rect_height(&new_plane_state->dst))))
749 		return -ERANGE;
750 
751 	fmt = msm_framebuffer_format(new_plane_state->fb);
752 
753 	supported_rotations = DRM_MODE_REFLECT_MASK | DRM_MODE_ROTATE_0;
754 
755 	if (pipe_hw_caps->features & BIT(DPU_SSPP_INLINE_ROTATION))
756 		supported_rotations |= DRM_MODE_ROTATE_90;
757 
758 	pipe_cfg->rotation = drm_rotation_simplify(new_plane_state->rotation,
759 						   supported_rotations);
760 
761 	min_src_size = MSM_FORMAT_IS_YUV(fmt) ? 2 : 1;
762 
763 	if (MSM_FORMAT_IS_YUV(fmt) &&
764 	    !pipe->sspp->cap->sblk->csc_blk.len) {
765 		DPU_DEBUG_PLANE(pdpu,
766 				"plane doesn't have csc for yuv\n");
767 		return -EINVAL;
768 	}
769 
770 	/* check src bounds */
771 	if (drm_rect_width(&pipe_cfg->src_rect) < min_src_size ||
772 	    drm_rect_height(&pipe_cfg->src_rect) < min_src_size) {
773 		DPU_DEBUG_PLANE(pdpu, "invalid source " DRM_RECT_FMT "\n",
774 				DRM_RECT_ARG(&pipe_cfg->src_rect));
775 		return -E2BIG;
776 	}
777 
778 	/* valid yuv image */
779 	if (MSM_FORMAT_IS_YUV(fmt) &&
780 	    (pipe_cfg->src_rect.x1 & 0x1 ||
781 	     pipe_cfg->src_rect.y1 & 0x1 ||
782 	     drm_rect_width(&pipe_cfg->src_rect) & 0x1 ||
783 	     drm_rect_height(&pipe_cfg->src_rect) & 0x1)) {
784 		DPU_DEBUG_PLANE(pdpu, "invalid yuv source " DRM_RECT_FMT "\n",
785 				DRM_RECT_ARG(&pipe_cfg->src_rect));
786 		return -EINVAL;
787 	}
788 
789 	/* min dst support */
790 	if (drm_rect_width(&pipe_cfg->dst_rect) < 0x1 ||
791 	    drm_rect_height(&pipe_cfg->dst_rect) < 0x1) {
792 		DPU_DEBUG_PLANE(pdpu, "invalid dest rect " DRM_RECT_FMT "\n",
793 				DRM_RECT_ARG(&pipe_cfg->dst_rect));
794 		return -EINVAL;
795 	}
796 
797 	if (pipe_cfg->rotation & DRM_MODE_ROTATE_90) {
798 		ret = dpu_plane_check_inline_rotation(pdpu, pipe, pipe_cfg->src_rect, fmt);
799 		if (ret)
800 			return ret;
801 	}
802 
803 	/* max clk check */
804 	if (_dpu_plane_calc_clk(mode, pipe_cfg) > kms->perf.max_core_clk_rate) {
805 		DPU_DEBUG_PLANE(pdpu, "plane exceeds max mdp core clk limits\n");
806 		return -E2BIG;
807 	}
808 
809 	return 0;
810 }
811 
812 #define MAX_UPSCALE_RATIO	20
813 #define MAX_DOWNSCALE_RATIO	4
814 
815 static int dpu_plane_atomic_check_nosspp(struct drm_plane *plane,
816 					 struct drm_plane_state *new_plane_state,
817 					 const struct drm_crtc_state *crtc_state)
818 {
819 	int i, ret = 0, min_scale, max_scale;
820 	struct dpu_plane *pdpu = to_dpu_plane(plane);
821 	struct dpu_plane_state *pstate = to_dpu_plane_state(new_plane_state);
822 	struct drm_rect fb_rect = { 0 };
823 
824 	min_scale = FRAC_16_16(1, MAX_UPSCALE_RATIO);
825 	max_scale = MAX_DOWNSCALE_RATIO << 16;
826 
827 	ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
828 						  min_scale,
829 						  max_scale,
830 						  true, true);
831 	if (ret) {
832 		DPU_DEBUG_PLANE(pdpu, "Check plane state failed (%d)\n", ret);
833 		return ret;
834 	}
835 	if (!new_plane_state->visible)
836 		return 0;
837 
838 	pstate->stage = DPU_STAGE_0 + pstate->base.normalized_zpos;
839 	if (pstate->stage >= pdpu->catalog->caps->max_mixer_blendstages) {
840 		DPU_ERROR("> %d plane stages assigned\n",
841 			  pdpu->catalog->caps->max_mixer_blendstages - DPU_STAGE_0);
842 		return -EINVAL;
843 	}
844 
845 	fb_rect.x2 = new_plane_state->fb->width;
846 	fb_rect.y2 = new_plane_state->fb->height;
847 
848 	/* Ensure fb size is supported */
849 	if (drm_rect_width(&fb_rect) > DPU_MAX_IMG_WIDTH ||
850 	    drm_rect_height(&fb_rect) > DPU_MAX_IMG_HEIGHT) {
851 		DPU_DEBUG_PLANE(pdpu, "invalid framebuffer " DRM_RECT_FMT "\n",
852 				DRM_RECT_ARG(&fb_rect));
853 		return -E2BIG;
854 	}
855 
856 	ret = dpu_format_populate_plane_sizes(new_plane_state->fb, &pstate->layout);
857 	if (ret) {
858 		DPU_ERROR_PLANE(pdpu, "failed to get format plane sizes, %d\n", ret);
859 		return ret;
860 	}
861 
862 	for (i = 0; i < pstate->layout.num_planes; i++)
863 		if (pstate->layout.plane_pitch[i] > DPU_SSPP_MAX_PITCH_SIZE)
864 			return -E2BIG;
865 
866 	pstate->needs_qos_remap = drm_atomic_crtc_needs_modeset(crtc_state);
867 
868 	return 0;
869 }
870 
871 static int dpu_plane_split(struct drm_plane *plane,
872 			   struct drm_plane_state *new_plane_state,
873 			   const struct drm_crtc_state *crtc_state)
874 {
875 	struct dpu_plane *pdpu = to_dpu_plane(plane);
876 	struct dpu_kms *kms = _dpu_plane_get_kms(&pdpu->base);
877 	u64 max_mdp_clk_rate = kms->perf.max_core_clk_rate;
878 	struct dpu_plane_state *pstate = to_dpu_plane_state(new_plane_state);
879 	struct dpu_sw_pipe_cfg *pipe_cfg;
880 	struct dpu_sw_pipe_cfg *r_pipe_cfg;
881 	const struct drm_display_mode *mode = &crtc_state->adjusted_mode;
882 	uint32_t max_linewidth;
883 	u32 num_lm;
884 	int stage_id, num_stages;
885 
886 	max_linewidth = pdpu->catalog->caps->max_linewidth;
887 
888 	/* In non-virtual plane case, one mixer pair is always needed. */
889 	num_lm = dpu_crtc_get_num_lm(crtc_state);
890 	if (dpu_use_virtual_planes)
891 		num_stages = (num_lm + 1) / 2;
892 	else
893 		num_stages = 1;
894 
895 	/*
896 	 * For wide plane that exceeds SSPP rectangle constrain, it needed to
897 	 * be split and mapped to 2 rectangles with 1 config for 2:2:1.
898 	 * For 2 interfaces cases, such as dual DSI, 2:2:2 topology is needed.
899 	 * If the width or clock exceeds hardware limitation in every half of
900 	 * screen, 4:4:2 topology is needed and virtual plane feature should
901 	 * be enabled to map plane to more than 1 SSPP. 2 stage configs are
902 	 * needed to serve 2 mixer pairs in this 4:4:2 case. So both left/right
903 	 * half of plane splitting, and splitting within the half of screen is
904 	 * needed in quad-pipe case. Check dest rectangle left/right clipping
905 	 * and iterate mixer configs for this plane first, then check wide
906 	 * rectangle splitting in every half next.
907 	 */
908 	for (stage_id = 0; stage_id < num_stages; stage_id++) {
909 		struct drm_rect mixer_rect = {
910 			.x1 = stage_id * mode->hdisplay / num_stages,
911 			.y1 = 0,
912 			.x2 = (stage_id + 1) * mode->hdisplay / num_stages,
913 			.y2 = mode->vdisplay
914 			};
915 		int cfg_idx = stage_id * PIPES_PER_STAGE;
916 
917 		pipe_cfg = &pstate->pipe_cfg[cfg_idx];
918 		r_pipe_cfg = &pstate->pipe_cfg[cfg_idx + 1];
919 
920 		drm_rect_fp_to_int(&pipe_cfg->src_rect, &new_plane_state->src);
921 
922 		drm_rect_rotate(&pipe_cfg->src_rect,
923 				new_plane_state->fb->width, new_plane_state->fb->height,
924 				new_plane_state->rotation);
925 
926 		pipe_cfg->dst_rect = new_plane_state->dst;
927 
928 		DPU_DEBUG_PLANE(pdpu, "checking src " DRM_RECT_FMT
929 				" vs clip window " DRM_RECT_FMT "\n",
930 				DRM_RECT_ARG(&pipe_cfg->src_rect),
931 				DRM_RECT_ARG(&mixer_rect));
932 
933 		/*
934 		 * If this plane does not fall into mixer rect, check next
935 		 * mixer rect.
936 		 */
937 		if (!drm_rect_clip_scaled(&pipe_cfg->src_rect,
938 					  &pipe_cfg->dst_rect,
939 					  &mixer_rect)) {
940 			memset(pipe_cfg, 0, 2 * sizeof(struct dpu_sw_pipe_cfg));
941 
942 			continue;
943 		}
944 
945 		pipe_cfg->dst_rect.x1 -= mixer_rect.x1;
946 		pipe_cfg->dst_rect.x2 -= mixer_rect.x1;
947 
948 		DPU_DEBUG_PLANE(pdpu, "Got clip src:" DRM_RECT_FMT " dst: " DRM_RECT_FMT "\n",
949 				DRM_RECT_ARG(&pipe_cfg->src_rect), DRM_RECT_ARG(&pipe_cfg->dst_rect));
950 
951 		/* Split wide rect into 2 rect */
952 		if ((drm_rect_width(&pipe_cfg->src_rect) > max_linewidth) ||
953 		     _dpu_plane_calc_clk(mode, pipe_cfg) > max_mdp_clk_rate) {
954 
955 			if (drm_rect_width(&pipe_cfg->src_rect) > 2 * max_linewidth) {
956 				DPU_DEBUG_PLANE(pdpu, "invalid src " DRM_RECT_FMT " line:%u\n",
957 						DRM_RECT_ARG(&pipe_cfg->src_rect), max_linewidth);
958 				return -E2BIG;
959 			}
960 
961 			memcpy(r_pipe_cfg, pipe_cfg, sizeof(struct dpu_sw_pipe_cfg));
962 			pipe_cfg->src_rect.x2 = (pipe_cfg->src_rect.x1 + pipe_cfg->src_rect.x2) >> 1;
963 			pipe_cfg->dst_rect.x2 = (pipe_cfg->dst_rect.x1 + pipe_cfg->dst_rect.x2) >> 1;
964 			r_pipe_cfg->src_rect.x1 = pipe_cfg->src_rect.x2;
965 			r_pipe_cfg->dst_rect.x1 = pipe_cfg->dst_rect.x2;
966 			DPU_DEBUG_PLANE(pdpu, "Split wide plane into:"
967 					DRM_RECT_FMT " and " DRM_RECT_FMT "\n",
968 					DRM_RECT_ARG(&pipe_cfg->src_rect),
969 					DRM_RECT_ARG(&r_pipe_cfg->src_rect));
970 		} else {
971 			memset(r_pipe_cfg, 0, sizeof(struct dpu_sw_pipe_cfg));
972 		}
973 
974 		drm_rect_rotate_inv(&pipe_cfg->src_rect,
975 				    new_plane_state->fb->width,
976 				    new_plane_state->fb->height,
977 				    new_plane_state->rotation);
978 
979 		if (drm_rect_width(&r_pipe_cfg->src_rect) != 0)
980 			drm_rect_rotate_inv(&r_pipe_cfg->src_rect,
981 					    new_plane_state->fb->width,
982 					    new_plane_state->fb->height,
983 					    new_plane_state->rotation);
984 	}
985 
986 	return 0;
987 }
988 
989 static int dpu_plane_is_multirect_capable(struct dpu_hw_sspp *sspp,
990 					  struct dpu_sw_pipe_cfg *pipe_cfg,
991 					  const struct msm_format *fmt)
992 {
993 	if (drm_rect_width(&pipe_cfg->src_rect) != drm_rect_width(&pipe_cfg->dst_rect) ||
994 	    drm_rect_height(&pipe_cfg->src_rect) != drm_rect_height(&pipe_cfg->dst_rect))
995 		return false;
996 
997 	if (pipe_cfg->rotation & DRM_MODE_ROTATE_90)
998 		return false;
999 
1000 	if (MSM_FORMAT_IS_YUV(fmt))
1001 		return false;
1002 
1003 	if (!sspp)
1004 		return true;
1005 
1006 	if (!test_bit(DPU_SSPP_SMART_DMA_V1, &sspp->cap->features) &&
1007 	    !test_bit(DPU_SSPP_SMART_DMA_V2, &sspp->cap->features))
1008 		return false;
1009 
1010 	return true;
1011 }
1012 
1013 static int dpu_plane_is_parallel_capable(struct dpu_sw_pipe_cfg *pipe_cfg,
1014 					 const struct msm_format *fmt,
1015 					 uint32_t max_linewidth)
1016 {
1017 	if (MSM_FORMAT_IS_UBWC(fmt) &&
1018 	    drm_rect_width(&pipe_cfg->src_rect) > max_linewidth / 2)
1019 		return false;
1020 
1021 	return true;
1022 }
1023 
1024 static int dpu_plane_is_multirect_parallel_capable(struct dpu_hw_sspp *sspp,
1025 						   struct dpu_sw_pipe_cfg *pipe_cfg,
1026 						   const struct msm_format *fmt,
1027 						   uint32_t max_linewidth)
1028 {
1029 	return dpu_plane_is_multirect_capable(sspp, pipe_cfg, fmt) &&
1030 		dpu_plane_is_parallel_capable(pipe_cfg, fmt, max_linewidth);
1031 }
1032 
1033 static bool dpu_plane_get_single_pipe_in_stage(struct dpu_plane_state *pstate,
1034 					       struct dpu_sw_pipe **single_pipe,
1035 					       struct dpu_sw_pipe_cfg **single_pipe_cfg,
1036 					       int stage_index)
1037 {
1038 	int pipe_idx;
1039 
1040 	pipe_idx = stage_index * PIPES_PER_STAGE;
1041 	if (drm_rect_width(&pstate->pipe_cfg[pipe_idx].src_rect) != 0 &&
1042 	    drm_rect_width(&pstate->pipe_cfg[pipe_idx + 1].src_rect) == 0) {
1043 		*single_pipe = &pstate->pipe[pipe_idx];
1044 		*single_pipe_cfg = &pstate->pipe_cfg[pipe_idx];
1045 		return true;
1046 	}
1047 
1048 	return false;
1049 }
1050 
1051 static int dpu_plane_atomic_check_sspp(struct drm_plane *plane,
1052 				       struct drm_atomic_state *state,
1053 				       const struct drm_crtc_state *crtc_state)
1054 {
1055 	struct drm_plane_state *new_plane_state =
1056 		drm_atomic_get_new_plane_state(state, plane);
1057 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1058 	struct dpu_plane_state *pstate = to_dpu_plane_state(new_plane_state);
1059 
1060 	struct dpu_sw_pipe *pipe;
1061 	struct dpu_sw_pipe_cfg *pipe_cfg;
1062 	int ret = 0, i;
1063 
1064 	for (i = 0; i < PIPES_PER_PLANE; i++) {
1065 		pipe = &pstate->pipe[i];
1066 		pipe_cfg = &pstate->pipe_cfg[i];
1067 		if (!drm_rect_width(&pipe_cfg->src_rect))
1068 			continue;
1069 		DPU_DEBUG_PLANE(pdpu, "pipe %d is in use, validate it\n", i);
1070 		ret = dpu_plane_atomic_check_pipe(pdpu, pipe, pipe_cfg,
1071 						  &crtc_state->adjusted_mode,
1072 						  new_plane_state);
1073 		if (ret)
1074 			return ret;
1075 	}
1076 
1077 	return 0;
1078 }
1079 
1080 static bool dpu_plane_try_multirect_parallel(struct dpu_sw_pipe *pipe, struct dpu_sw_pipe_cfg *pipe_cfg,
1081 					     struct dpu_sw_pipe *r_pipe, struct dpu_sw_pipe_cfg *r_pipe_cfg,
1082 					     struct dpu_hw_sspp *sspp, const struct msm_format *fmt,
1083 					     uint32_t max_linewidth)
1084 {
1085 	r_pipe->sspp = NULL;
1086 
1087 	pipe->multirect_index = DPU_SSPP_RECT_SOLO;
1088 	pipe->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
1089 
1090 	r_pipe->multirect_index = DPU_SSPP_RECT_SOLO;
1091 	r_pipe->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
1092 
1093 	if (drm_rect_width(&r_pipe_cfg->src_rect) != 0) {
1094 		if (!dpu_plane_is_multirect_parallel_capable(pipe->sspp, pipe_cfg, fmt, max_linewidth) ||
1095 		    !dpu_plane_is_multirect_parallel_capable(pipe->sspp, r_pipe_cfg, fmt, max_linewidth))
1096 			return false;
1097 
1098 		r_pipe->sspp = pipe->sspp;
1099 
1100 		pipe->multirect_index = DPU_SSPP_RECT_0;
1101 		pipe->multirect_mode = DPU_SSPP_MULTIRECT_PARALLEL;
1102 
1103 		r_pipe->multirect_index = DPU_SSPP_RECT_1;
1104 		r_pipe->multirect_mode = DPU_SSPP_MULTIRECT_PARALLEL;
1105 	}
1106 
1107 	return true;
1108 }
1109 
1110 static int dpu_plane_try_multirect_shared(struct dpu_plane_state *pstate,
1111 					  struct dpu_plane_state *prev_adjacent_pstate,
1112 					  const struct msm_format *fmt,
1113 					  uint32_t max_linewidth, int stage_index)
1114 {
1115 	struct dpu_sw_pipe *pipe, *prev_pipe;
1116 	struct dpu_sw_pipe_cfg *pipe_cfg, *prev_pipe_cfg;
1117 	const struct msm_format *prev_fmt = msm_framebuffer_format(prev_adjacent_pstate->base.fb);
1118 	u16 max_tile_height = 1;
1119 
1120 	if (!dpu_plane_get_single_pipe_in_stage(pstate, &pipe,
1121 						&pipe_cfg, stage_index))
1122 		return false;
1123 
1124 	if (!dpu_plane_get_single_pipe_in_stage(prev_adjacent_pstate,
1125 						&prev_pipe, &prev_pipe_cfg,
1126 						stage_index) ||
1127 	    prev_pipe->multirect_mode != DPU_SSPP_MULTIRECT_NONE)
1128 		return false;
1129 
1130 	/* Do not validate SSPP of current plane when it is not ready */
1131 	if (!dpu_plane_is_multirect_capable(pipe->sspp, pipe_cfg, fmt) ||
1132 	    !dpu_plane_is_multirect_capable(prev_pipe->sspp, prev_pipe_cfg, prev_fmt))
1133 		return false;
1134 
1135 	if (MSM_FORMAT_IS_UBWC(fmt))
1136 		max_tile_height = max(max_tile_height, fmt->tile_height);
1137 
1138 	if (MSM_FORMAT_IS_UBWC(prev_fmt))
1139 		max_tile_height = max(max_tile_height, prev_fmt->tile_height);
1140 
1141 	if (dpu_plane_is_parallel_capable(pipe_cfg, fmt, max_linewidth) &&
1142 	    dpu_plane_is_parallel_capable(prev_pipe_cfg, prev_fmt, max_linewidth) &&
1143 	    (pipe_cfg->dst_rect.x1 >= prev_pipe_cfg->dst_rect.x2 ||
1144 	     prev_pipe_cfg->dst_rect.x1 >= pipe_cfg->dst_rect.x2)) {
1145 		pipe->sspp = prev_pipe->sspp;
1146 
1147 		pipe->multirect_index = DPU_SSPP_RECT_1;
1148 		pipe->multirect_mode = DPU_SSPP_MULTIRECT_PARALLEL;
1149 
1150 		prev_pipe->multirect_index = DPU_SSPP_RECT_0;
1151 		prev_pipe->multirect_mode = DPU_SSPP_MULTIRECT_PARALLEL;
1152 
1153 		return true;
1154 	}
1155 
1156 	if (pipe_cfg->dst_rect.y1 >= prev_pipe_cfg->dst_rect.y2 + 2 * max_tile_height ||
1157 	    prev_pipe_cfg->dst_rect.y1 >= pipe_cfg->dst_rect.y2 + 2 * max_tile_height) {
1158 		pipe->sspp = prev_pipe->sspp;
1159 
1160 		pipe->multirect_index = DPU_SSPP_RECT_1;
1161 		pipe->multirect_mode = DPU_SSPP_MULTIRECT_TIME_MX;
1162 
1163 		prev_pipe->multirect_index = DPU_SSPP_RECT_0;
1164 		prev_pipe->multirect_mode = DPU_SSPP_MULTIRECT_TIME_MX;
1165 
1166 		return true;
1167 	}
1168 
1169 	return false;
1170 }
1171 
1172 static int dpu_plane_atomic_check(struct drm_plane *plane,
1173 				  struct drm_atomic_state *state)
1174 {
1175 	struct drm_plane_state *plane_state =
1176 		drm_atomic_get_plane_state(state, plane);
1177 	struct drm_plane_state *old_plane_state =
1178 		drm_atomic_get_old_plane_state(state, plane);
1179 	int ret = 0;
1180 	struct drm_crtc_state *crtc_state = NULL;
1181 
1182 	if (IS_ERR(plane_state))
1183 		return PTR_ERR(plane_state);
1184 
1185 	if (plane_state->crtc)
1186 		crtc_state = drm_atomic_get_new_crtc_state(state,
1187 							   plane_state->crtc);
1188 
1189 	ret = dpu_plane_atomic_check_nosspp(plane, plane_state, crtc_state);
1190 	if (ret)
1191 		return ret;
1192 
1193 	if (!plane_state->visible)
1194 		return 0;
1195 
1196 	/*
1197 	 * Force resource reallocation if the format of FB or src/dst have
1198 	 * changed. We might need to allocate different SSPP or SSPPs for this
1199 	 * plane than the one used previously.
1200 	 */
1201 	if (!old_plane_state || !old_plane_state->fb ||
1202 	    old_plane_state->src_w != plane_state->src_w ||
1203 	    old_plane_state->src_h != plane_state->src_h ||
1204 	    old_plane_state->crtc_w != plane_state->crtc_w ||
1205 	    old_plane_state->crtc_h != plane_state->crtc_h ||
1206 	    msm_framebuffer_format(old_plane_state->fb) !=
1207 	    msm_framebuffer_format(plane_state->fb))
1208 		crtc_state->planes_changed = true;
1209 	return 0;
1210 }
1211 
1212 static int dpu_plane_assign_resource_in_stage(struct dpu_sw_pipe *pipe,
1213 					      struct dpu_sw_pipe_cfg *pipe_cfg,
1214 					      struct drm_plane_state *plane_state,
1215 					      struct dpu_global_state *global_state,
1216 					      struct drm_crtc *crtc,
1217 					      struct dpu_rm_sspp_requirements *reqs)
1218 {
1219 	struct drm_plane *plane = plane_state->plane;
1220 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
1221 	struct dpu_sw_pipe *r_pipe = pipe + 1;
1222 	struct dpu_sw_pipe_cfg *r_pipe_cfg = pipe_cfg + 1;
1223 
1224 	if (drm_rect_width(&pipe_cfg->src_rect) == 0)
1225 		return 0;
1226 
1227 	pipe->sspp = dpu_rm_reserve_sspp(&dpu_kms->rm, global_state, crtc, reqs);
1228 	if (!pipe->sspp)
1229 		return -ENODEV;
1230 	pipe->multirect_index = DPU_SSPP_RECT_SOLO;
1231 	pipe->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
1232 
1233 	if (drm_rect_width(&r_pipe_cfg->src_rect) == 0)
1234 		return 0;
1235 
1236 	if (dpu_plane_try_multirect_parallel(pipe, pipe_cfg, r_pipe, r_pipe_cfg,
1237 					      pipe->sspp,
1238 					      msm_framebuffer_format(plane_state->fb),
1239 					      dpu_kms->catalog->caps->max_linewidth))
1240 		return 0;
1241 
1242 	r_pipe->sspp = dpu_rm_reserve_sspp(&dpu_kms->rm, global_state, crtc, reqs);
1243 	if (!r_pipe->sspp)
1244 		return -ENODEV;
1245 	r_pipe->multirect_index = DPU_SSPP_RECT_SOLO;
1246 	r_pipe->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
1247 
1248 	return 0;
1249 }
1250 
1251 static int dpu_plane_virtual_assign_resources(struct drm_crtc *crtc,
1252 					      struct dpu_global_state *global_state,
1253 					      struct drm_atomic_state *state,
1254 					      struct drm_plane_state *plane_state,
1255 					      const struct drm_crtc_state *crtc_state,
1256 					      struct drm_plane_state **prev_adjacent_plane_state)
1257 {
1258 	struct drm_plane *plane = plane_state->plane;
1259 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
1260 	struct dpu_rm_sspp_requirements reqs;
1261 	struct dpu_plane_state *pstate, *prev_adjacent_pstate[STAGES_PER_PLANE];
1262 	struct dpu_sw_pipe *pipe;
1263 	struct dpu_sw_pipe_cfg *pipe_cfg;
1264 	const struct msm_format *fmt;
1265 	int i, ret;
1266 
1267 	pstate = to_dpu_plane_state(plane_state);
1268 	for (i = 0; i < STAGES_PER_PLANE; i++)
1269 		prev_adjacent_pstate[i] = prev_adjacent_plane_state[i] ?
1270 			to_dpu_plane_state(prev_adjacent_plane_state[i]) : NULL;
1271 
1272 	for (i = 0; i < PIPES_PER_PLANE; i++)
1273 		pstate->pipe[i].sspp = NULL;
1274 
1275 	if (!plane_state->fb)
1276 		return -EINVAL;
1277 
1278 	ret = dpu_plane_split(plane, plane_state, crtc_state);
1279 	if (ret)
1280 		return ret;
1281 
1282 	fmt = msm_framebuffer_format(plane_state->fb);
1283 	reqs.yuv = MSM_FORMAT_IS_YUV(fmt);
1284 	reqs.scale = (plane_state->src_w >> 16 != plane_state->crtc_w) ||
1285 		(plane_state->src_h >> 16 != plane_state->crtc_h);
1286 
1287 	reqs.rot90 = drm_rotation_90_or_270(plane_state->rotation);
1288 
1289 	for (i = 0; i < STAGES_PER_PLANE; i++) {
1290 		if (prev_adjacent_pstate[i] &&
1291 		    dpu_plane_try_multirect_shared(pstate, prev_adjacent_pstate[i], fmt,
1292 						   dpu_kms->catalog->caps->max_linewidth,
1293 						   i))
1294 			continue;
1295 
1296 		if (dpu_plane_get_single_pipe_in_stage(pstate, &pipe, &pipe_cfg, i))
1297 			prev_adjacent_plane_state[i] = plane_state;
1298 
1299 		pipe = &pstate->pipe[i * PIPES_PER_STAGE];
1300 		pipe_cfg = &pstate->pipe_cfg[i * PIPES_PER_STAGE];
1301 		ret = dpu_plane_assign_resource_in_stage(pipe, pipe_cfg,
1302 							 plane_state,
1303 							 global_state,
1304 							 crtc, &reqs);
1305 		if (ret)
1306 			return ret;
1307 	}
1308 
1309 	return dpu_plane_atomic_check_sspp(plane, state, crtc_state);
1310 }
1311 
1312 static int dpu_plane_assign_resources(struct drm_crtc *crtc,
1313 				      struct dpu_global_state *global_state,
1314 				      struct drm_atomic_state *state,
1315 				      struct drm_plane_state *plane_state,
1316 				      const struct drm_crtc_state *crtc_state)
1317 {
1318 	struct drm_plane *plane = plane_state->plane;
1319 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
1320 	struct dpu_plane_state *pstate = to_dpu_plane_state(plane_state);
1321 	struct dpu_sw_pipe *pipe = &pstate->pipe[0];
1322 	struct dpu_sw_pipe *r_pipe = &pstate->pipe[1];
1323 	struct dpu_sw_pipe_cfg *pipe_cfg = &pstate->pipe_cfg[0];
1324 	struct dpu_sw_pipe_cfg *r_pipe_cfg = &pstate->pipe_cfg[1];
1325 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1326 	int ret;
1327 
1328 	pipe->sspp = dpu_rm_get_sspp(&dpu_kms->rm, pdpu->pipe);
1329 	if (!pipe->sspp)
1330 		return -EINVAL;
1331 
1332 	ret = dpu_plane_split(plane, plane_state, crtc_state);
1333 	if (ret)
1334 		return ret;
1335 
1336 	if (!dpu_plane_try_multirect_parallel(pipe, pipe_cfg, r_pipe, r_pipe_cfg,
1337 					      pipe->sspp,
1338 					      msm_framebuffer_format(plane_state->fb),
1339 					      dpu_kms->catalog->caps->max_linewidth)) {
1340 		DPU_DEBUG_PLANE(pdpu, "invalid " DRM_RECT_FMT " /" DRM_RECT_FMT
1341 				" max_line:%u, can't use split source\n",
1342 				DRM_RECT_ARG(&pipe_cfg->src_rect),
1343 				DRM_RECT_ARG(&r_pipe_cfg->src_rect),
1344 				dpu_kms->catalog->caps->max_linewidth);
1345 		return -E2BIG;
1346 	}
1347 
1348 	return dpu_plane_atomic_check_sspp(plane, state, crtc_state);
1349 }
1350 
1351 int dpu_assign_plane_resources(struct dpu_global_state *global_state,
1352 			       struct drm_atomic_state *state,
1353 			       struct drm_crtc *crtc,
1354 			       struct drm_plane_state **states,
1355 			       unsigned int num_planes)
1356 {
1357 	struct drm_plane_state *prev_adjacent_plane_state[STAGES_PER_PLANE] = { NULL };
1358 	const struct drm_crtc_state *crtc_state = NULL;
1359 	unsigned int i;
1360 	int ret;
1361 
1362 	for (i = 0; i < num_planes; i++) {
1363 		struct drm_plane_state *plane_state = states[i];
1364 
1365 		if (!plane_state ||
1366 		    !plane_state->visible)
1367 			continue;
1368 
1369 		if (plane_state->crtc)
1370 			crtc_state = drm_atomic_get_new_crtc_state(state,
1371 								   plane_state->crtc);
1372 
1373 		if (!dpu_use_virtual_planes)
1374 			ret = dpu_plane_assign_resources(crtc, global_state,
1375 							 state, plane_state,
1376 							 crtc_state);
1377 		else
1378 			ret = dpu_plane_virtual_assign_resources(crtc, global_state,
1379 							     state, plane_state,
1380 							     crtc_state,
1381 							     prev_adjacent_plane_state);
1382 		if (ret)
1383 			return ret;
1384 	}
1385 
1386 	return 0;
1387 }
1388 
1389 static void dpu_plane_flush_csc(struct dpu_plane *pdpu, struct dpu_sw_pipe *pipe)
1390 {
1391 	const struct msm_format *format =
1392 		msm_framebuffer_format(pdpu->base.state->fb);
1393 	const struct dpu_csc_cfg *csc_ptr;
1394 
1395 	if (!pipe->sspp || !pipe->sspp->ops.setup_csc)
1396 		return;
1397 
1398 	csc_ptr = _dpu_plane_get_csc(pipe, format);
1399 	if (!csc_ptr)
1400 		return;
1401 
1402 	DPU_DEBUG_PLANE(pdpu, "using 0x%X 0x%X 0x%X...\n",
1403 			csc_ptr->csc_mv[0],
1404 			csc_ptr->csc_mv[1],
1405 			csc_ptr->csc_mv[2]);
1406 
1407 	pipe->sspp->ops.setup_csc(pipe->sspp, csc_ptr);
1408 
1409 }
1410 
1411 /**
1412  * dpu_plane_flush - final plane operations before commit flush
1413  * @plane: Pointer to drm plane structure
1414  */
1415 void dpu_plane_flush(struct drm_plane *plane)
1416 {
1417 	struct dpu_plane *pdpu;
1418 	struct dpu_plane_state *pstate;
1419 	int i;
1420 
1421 	if (!plane || !plane->state) {
1422 		DPU_ERROR("invalid plane\n");
1423 		return;
1424 	}
1425 
1426 	pdpu = to_dpu_plane(plane);
1427 	pstate = to_dpu_plane_state(plane->state);
1428 
1429 	/*
1430 	 * These updates have to be done immediately before the plane flush
1431 	 * timing, and may not be moved to the atomic_update/mode_set functions.
1432 	 */
1433 	if (pdpu->is_error)
1434 		/* force white frame with 100% alpha pipe output on error */
1435 		_dpu_plane_color_fill(pdpu, 0xFFFFFF, 0xFF);
1436 	else if (pdpu->color_fill & DPU_PLANE_COLOR_FILL_FLAG)
1437 		/* force 100% alpha */
1438 		_dpu_plane_color_fill(pdpu, pdpu->color_fill, 0xFF);
1439 	else {
1440 		for (i = 0; i < PIPES_PER_PLANE; i++)
1441 			dpu_plane_flush_csc(pdpu, &pstate->pipe[i]);
1442 	}
1443 
1444 	/* flag h/w flush complete */
1445 	if (plane->state)
1446 		pstate->pending = false;
1447 }
1448 
1449 /**
1450  * dpu_plane_set_error: enable/disable error condition
1451  * @plane: pointer to drm_plane structure
1452  * @error: error value to set
1453  */
1454 void dpu_plane_set_error(struct drm_plane *plane, bool error)
1455 {
1456 	struct dpu_plane *pdpu;
1457 
1458 	if (!plane)
1459 		return;
1460 
1461 	pdpu = to_dpu_plane(plane);
1462 	pdpu->is_error = error;
1463 }
1464 
1465 static void dpu_plane_sspp_update_pipe(struct drm_plane *plane,
1466 				       struct dpu_sw_pipe *pipe,
1467 				       struct dpu_sw_pipe_cfg *pipe_cfg,
1468 				       const struct msm_format *fmt,
1469 				       int frame_rate,
1470 				       struct dpu_hw_fmt_layout *layout)
1471 {
1472 	uint32_t src_flags;
1473 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1474 	struct drm_plane_state *state = plane->state;
1475 	struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1476 
1477 	if (layout && pipe->sspp->ops.setup_sourceaddress) {
1478 		trace_dpu_plane_set_scanout(pipe, layout);
1479 		pipe->sspp->ops.setup_sourceaddress(pipe, layout);
1480 	}
1481 
1482 	/* override for color fill */
1483 	if (pdpu->color_fill & DPU_PLANE_COLOR_FILL_FLAG) {
1484 		_dpu_plane_set_qos_ctrl(plane, pipe, false);
1485 
1486 		/* skip remaining processing on color fill */
1487 		return;
1488 	}
1489 
1490 	if (pipe->sspp->ops.setup_rects) {
1491 		pipe->sspp->ops.setup_rects(pipe,
1492 				pipe_cfg);
1493 	}
1494 
1495 	_dpu_plane_setup_scaler(pipe, fmt, false, pipe_cfg);
1496 
1497 	if (pipe->sspp->ops.setup_multirect)
1498 		pipe->sspp->ops.setup_multirect(
1499 				pipe);
1500 
1501 	if (pipe->sspp->ops.setup_format) {
1502 		unsigned int rotation = pipe_cfg->rotation;
1503 
1504 		src_flags = 0x0;
1505 
1506 		if (rotation & DRM_MODE_REFLECT_X)
1507 			src_flags |= DPU_SSPP_FLIP_LR;
1508 
1509 		if (rotation & DRM_MODE_REFLECT_Y)
1510 			src_flags |= DPU_SSPP_FLIP_UD;
1511 
1512 		if (rotation & DRM_MODE_ROTATE_90)
1513 			src_flags |= DPU_SSPP_ROT_90;
1514 
1515 		/* update format */
1516 		pipe->sspp->ops.setup_format(pipe, fmt, src_flags);
1517 
1518 		if (pipe->sspp->ops.setup_cdp) {
1519 			const struct dpu_perf_cfg *perf = pdpu->catalog->perf;
1520 
1521 			pipe->sspp->ops.setup_cdp(pipe, fmt,
1522 						  perf->cdp_cfg[DPU_PERF_CDP_USAGE_RT].rd_enable);
1523 		}
1524 	}
1525 
1526 	_dpu_plane_set_qos_lut(plane, pipe, fmt, pipe_cfg);
1527 
1528 	if (pipe->sspp->idx != SSPP_CURSOR0 &&
1529 	    pipe->sspp->idx != SSPP_CURSOR1)
1530 		_dpu_plane_set_ot_limit(plane, pipe, pipe_cfg, frame_rate);
1531 
1532 	if (pstate->needs_qos_remap)
1533 		_dpu_plane_set_qos_remap(plane, pipe);
1534 }
1535 
1536 static void dpu_plane_sspp_atomic_update(struct drm_plane *plane,
1537 					 struct drm_plane_state *new_state)
1538 {
1539 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1540 	struct drm_plane_state *state = plane->state;
1541 	struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1542 	struct drm_crtc *crtc = state->crtc;
1543 	struct drm_framebuffer *fb = state->fb;
1544 	bool is_rt_pipe;
1545 	const struct msm_format *fmt =
1546 		msm_framebuffer_format(fb);
1547 	int i;
1548 
1549 	pstate->pending = true;
1550 
1551 	is_rt_pipe = (dpu_crtc_get_client_type(crtc) != NRT_CLIENT);
1552 	pstate->needs_qos_remap |= (is_rt_pipe != pdpu->is_rt_pipe);
1553 	pdpu->is_rt_pipe = is_rt_pipe;
1554 
1555 	dpu_format_populate_addrs(new_state->fb, &pstate->layout);
1556 
1557 	DPU_DEBUG_PLANE(pdpu, "FB[%u] " DRM_RECT_FP_FMT "->crtc%u " DRM_RECT_FMT
1558 			", %p4cc ubwc %d\n", fb->base.id, DRM_RECT_FP_ARG(&state->src),
1559 			crtc->base.id, DRM_RECT_ARG(&state->dst),
1560 			&fmt->pixel_format, MSM_FORMAT_IS_UBWC(fmt));
1561 
1562 	for (i = 0; i < PIPES_PER_PLANE; i++) {
1563 		if (!drm_rect_width(&pstate->pipe_cfg[i].src_rect))
1564 			continue;
1565 		dpu_plane_sspp_update_pipe(plane, &pstate->pipe[i],
1566 					   &pstate->pipe_cfg[i], fmt,
1567 					   drm_mode_vrefresh(&crtc->mode),
1568 					   &pstate->layout);
1569 	}
1570 
1571 	if (pstate->needs_qos_remap)
1572 		pstate->needs_qos_remap = false;
1573 
1574 	pstate->plane_fetch_bw = 0;
1575 	pstate->plane_clk = 0;
1576 	for (i = 0; i < PIPES_PER_PLANE; i++) {
1577 		if (!drm_rect_width(&pstate->pipe_cfg[i].src_rect))
1578 			continue;
1579 		pstate->plane_fetch_bw += _dpu_plane_calc_bw(pdpu->catalog, fmt,
1580 							     &crtc->mode, &pstate->pipe_cfg[i]);
1581 
1582 		pstate->plane_clk = max(pstate->plane_clk,
1583 					_dpu_plane_calc_clk(&crtc->mode,
1584 							    &pstate->pipe_cfg[i]));
1585 	}
1586 }
1587 
1588 static void _dpu_plane_atomic_disable(struct drm_plane *plane)
1589 {
1590 	struct drm_plane_state *state = plane->state;
1591 	struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1592 	struct dpu_sw_pipe *pipe;
1593 	int i;
1594 
1595 	for (i = 0; i < PIPES_PER_PLANE; i += 1) {
1596 		pipe = &pstate->pipe[i];
1597 		if (!pipe->sspp)
1598 			continue;
1599 
1600 		trace_dpu_plane_disable(DRMID(plane), false,
1601 					pstate->pipe[i].multirect_mode);
1602 
1603 		if (i % PIPES_PER_STAGE == 0)
1604 			continue;
1605 
1606 		/*
1607 		 * clear multirect for the right pipe so that the SSPP
1608 		 * can be further reused in the solo mode
1609 		 */
1610 		pipe->multirect_index = DPU_SSPP_RECT_SOLO;
1611 		pipe->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
1612 		if (pipe->sspp->ops.setup_multirect)
1613 			pipe->sspp->ops.setup_multirect(pipe);
1614 	}
1615 
1616 	pstate->pending = true;
1617 }
1618 
1619 static void dpu_plane_atomic_update(struct drm_plane *plane,
1620 				struct drm_atomic_state *state)
1621 {
1622 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1623 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
1624 									   plane);
1625 
1626 	pdpu->is_error = false;
1627 
1628 	DPU_DEBUG_PLANE(pdpu, "\n");
1629 
1630 	if (!new_state->visible) {
1631 		_dpu_plane_atomic_disable(plane);
1632 	} else {
1633 		dpu_plane_sspp_atomic_update(plane, new_state);
1634 	}
1635 }
1636 
1637 static void dpu_plane_destroy_state(struct drm_plane *plane,
1638 		struct drm_plane_state *state)
1639 {
1640 	__drm_atomic_helper_plane_destroy_state(state);
1641 	kfree(to_dpu_plane_state(state));
1642 }
1643 
1644 static struct drm_plane_state *
1645 dpu_plane_duplicate_state(struct drm_plane *plane)
1646 {
1647 	struct dpu_plane *pdpu;
1648 	struct dpu_plane_state *pstate;
1649 	struct dpu_plane_state *old_state;
1650 
1651 	if (!plane) {
1652 		DPU_ERROR("invalid plane\n");
1653 		return NULL;
1654 	} else if (!plane->state) {
1655 		DPU_ERROR("invalid plane state\n");
1656 		return NULL;
1657 	}
1658 
1659 	old_state = to_dpu_plane_state(plane->state);
1660 	pdpu = to_dpu_plane(plane);
1661 	pstate = kmemdup(old_state, sizeof(*old_state), GFP_KERNEL);
1662 	if (!pstate) {
1663 		DPU_ERROR_PLANE(pdpu, "failed to allocate state\n");
1664 		return NULL;
1665 	}
1666 
1667 	DPU_DEBUG_PLANE(pdpu, "\n");
1668 
1669 	pstate->pending = false;
1670 
1671 	__drm_atomic_helper_plane_duplicate_state(plane, &pstate->base);
1672 
1673 	return &pstate->base;
1674 }
1675 
1676 static const char * const multirect_mode_name[] = {
1677 	[DPU_SSPP_MULTIRECT_NONE] = "none",
1678 	[DPU_SSPP_MULTIRECT_PARALLEL] = "parallel",
1679 	[DPU_SSPP_MULTIRECT_TIME_MX] = "time_mx",
1680 };
1681 
1682 static const char * const multirect_index_name[] = {
1683 	[DPU_SSPP_RECT_SOLO] = "solo",
1684 	[DPU_SSPP_RECT_0] = "rect_0",
1685 	[DPU_SSPP_RECT_1] = "rect_1",
1686 };
1687 
1688 static const char *dpu_get_multirect_mode(enum dpu_sspp_multirect_mode mode)
1689 {
1690 	if (WARN_ON(mode >= ARRAY_SIZE(multirect_mode_name)))
1691 		return "unknown";
1692 
1693 	return multirect_mode_name[mode];
1694 }
1695 
1696 static const char *dpu_get_multirect_index(enum dpu_sspp_multirect_index index)
1697 {
1698 	if (WARN_ON(index >= ARRAY_SIZE(multirect_index_name)))
1699 		return "unknown";
1700 
1701 	return multirect_index_name[index];
1702 }
1703 
1704 static void dpu_plane_atomic_print_state(struct drm_printer *p,
1705 		const struct drm_plane_state *state)
1706 {
1707 	const struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1708 	const struct dpu_sw_pipe *pipe;
1709 	const struct dpu_sw_pipe_cfg *pipe_cfg;
1710 	int i;
1711 
1712 	drm_printf(p, "\tstage=%d\n", pstate->stage);
1713 
1714 	for (i = 0; i < PIPES_PER_PLANE; i++) {
1715 		pipe = &pstate->pipe[i];
1716 		if (!pipe->sspp)
1717 			continue;
1718 		pipe_cfg = &pstate->pipe_cfg[i];
1719 		drm_printf(p, "\tsspp[%d]=%s\n", i, pipe->sspp->cap->name);
1720 		drm_printf(p, "\tmultirect_mode[%d]=%s\n", i,
1721 			   dpu_get_multirect_mode(pipe->multirect_mode));
1722 		drm_printf(p, "\tmultirect_index[%d]=%s\n", i,
1723 			   dpu_get_multirect_index(pipe->multirect_index));
1724 		drm_printf(p, "\tsrc[%d]=" DRM_RECT_FMT "\n", i,
1725 			   DRM_RECT_ARG(&pipe_cfg->src_rect));
1726 		drm_printf(p, "\tdst[%d]=" DRM_RECT_FMT "\n", i,
1727 			   DRM_RECT_ARG(&pipe_cfg->dst_rect));
1728 	}
1729 }
1730 
1731 static void dpu_plane_reset(struct drm_plane *plane)
1732 {
1733 	struct dpu_plane *pdpu;
1734 	struct dpu_plane_state *pstate;
1735 
1736 	if (!plane) {
1737 		DPU_ERROR("invalid plane\n");
1738 		return;
1739 	}
1740 
1741 	pdpu = to_dpu_plane(plane);
1742 	DPU_DEBUG_PLANE(pdpu, "\n");
1743 
1744 	/* remove previous state, if present */
1745 	if (plane->state) {
1746 		dpu_plane_destroy_state(plane, plane->state);
1747 		plane->state = NULL;
1748 	}
1749 
1750 	pstate = kzalloc_obj(*pstate);
1751 	if (!pstate) {
1752 		DPU_ERROR_PLANE(pdpu, "failed to allocate state\n");
1753 		return;
1754 	}
1755 
1756 	__drm_atomic_helper_plane_reset(plane, &pstate->base);
1757 }
1758 
1759 #ifdef CONFIG_DEBUG_FS
1760 void dpu_plane_danger_signal_ctrl(struct drm_plane *plane, bool enable)
1761 {
1762 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1763 	struct dpu_plane_state *pstate = to_dpu_plane_state(plane->state);
1764 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
1765 	int i;
1766 
1767 	if (!pdpu->is_rt_pipe)
1768 		return;
1769 
1770 	pm_runtime_get_sync(&dpu_kms->pdev->dev);
1771 	for (i = 0; i < PIPES_PER_PLANE; i++) {
1772 		if (!pstate->pipe[i].sspp)
1773 			continue;
1774 		_dpu_plane_set_qos_ctrl(plane, &pstate->pipe[i], enable);
1775 	}
1776 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
1777 }
1778 #endif
1779 
1780 static bool dpu_plane_format_mod_supported(struct drm_plane *plane,
1781 		uint32_t format, uint64_t modifier)
1782 {
1783 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
1784 	bool has_no_ubwc = (dpu_kms->mdss->ubwc_enc_version == 0) &&
1785 			   (dpu_kms->mdss->ubwc_dec_version == 0);
1786 
1787 	if (modifier == DRM_FORMAT_MOD_LINEAR)
1788 		return true;
1789 
1790 	if (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED && !has_no_ubwc)
1791 		return dpu_find_format(format, qcom_compressed_supported_formats,
1792 				ARRAY_SIZE(qcom_compressed_supported_formats));
1793 
1794 	return false;
1795 }
1796 
1797 static const struct drm_plane_funcs dpu_plane_funcs = {
1798 		.update_plane = drm_atomic_helper_update_plane,
1799 		.disable_plane = drm_atomic_helper_disable_plane,
1800 		.reset = dpu_plane_reset,
1801 		.atomic_duplicate_state = dpu_plane_duplicate_state,
1802 		.atomic_destroy_state = dpu_plane_destroy_state,
1803 		.atomic_print_state = dpu_plane_atomic_print_state,
1804 		.format_mod_supported = dpu_plane_format_mod_supported,
1805 };
1806 
1807 static const struct drm_plane_helper_funcs dpu_plane_helper_funcs = {
1808 		.prepare_fb = dpu_plane_prepare_fb,
1809 		.cleanup_fb = dpu_plane_cleanup_fb,
1810 		.atomic_check = dpu_plane_atomic_check,
1811 		.atomic_update = dpu_plane_atomic_update,
1812 };
1813 
1814 static const struct drm_plane_helper_funcs dpu_plane_virtual_helper_funcs = {
1815 	.prepare_fb = dpu_plane_prepare_fb,
1816 	.cleanup_fb = dpu_plane_cleanup_fb,
1817 	.atomic_check = dpu_plane_atomic_check,
1818 	.atomic_update = dpu_plane_atomic_update,
1819 };
1820 
1821 /* initialize plane */
1822 static struct drm_plane *dpu_plane_init_common(struct drm_device *dev,
1823 					       enum drm_plane_type type,
1824 					       unsigned long possible_crtcs,
1825 					       bool inline_rotation,
1826 					       const uint32_t *format_list,
1827 					       uint32_t num_formats,
1828 					       enum dpu_sspp pipe)
1829 {
1830 	struct drm_plane *plane = NULL;
1831 	struct dpu_plane *pdpu;
1832 	struct msm_drm_private *priv = dev->dev_private;
1833 	struct dpu_kms *kms = to_dpu_kms(priv->kms);
1834 	uint32_t supported_rotations;
1835 	int ret;
1836 
1837 	pdpu = drmm_universal_plane_alloc(dev, struct dpu_plane, base,
1838 				0xff, &dpu_plane_funcs,
1839 				format_list, num_formats,
1840 				supported_format_modifiers, type, NULL);
1841 	if (IS_ERR(pdpu))
1842 		return ERR_CAST(pdpu);
1843 
1844 	/* cache local stuff for later */
1845 	plane = &pdpu->base;
1846 	pdpu->pipe = pipe;
1847 
1848 	pdpu->catalog = kms->catalog;
1849 
1850 	ret = drm_plane_create_zpos_property(plane, 0, 0, DPU_ZPOS_MAX);
1851 	if (ret)
1852 		DPU_ERROR("failed to install zpos property, rc = %d\n", ret);
1853 
1854 	drm_plane_create_alpha_property(plane);
1855 	drm_plane_create_blend_mode_property(plane,
1856 			BIT(DRM_MODE_BLEND_PIXEL_NONE) |
1857 			BIT(DRM_MODE_BLEND_PREMULTI) |
1858 			BIT(DRM_MODE_BLEND_COVERAGE));
1859 
1860 	supported_rotations = DRM_MODE_REFLECT_MASK | DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
1861 
1862 	if (inline_rotation)
1863 		supported_rotations |= DRM_MODE_ROTATE_MASK;
1864 
1865 	drm_plane_create_rotation_property(plane,
1866 		    DRM_MODE_ROTATE_0, supported_rotations);
1867 
1868 	drm_plane_enable_fb_damage_clips(plane);
1869 
1870 	DPU_DEBUG("%s created for pipe:%u id:%u\n", plane->name,
1871 					pipe, plane->base.id);
1872 	return plane;
1873 }
1874 
1875 /**
1876  * dpu_plane_init - create new dpu plane for the given pipe
1877  * @dev:   Pointer to DRM device
1878  * @pipe:  dpu hardware pipe identifier
1879  * @type:  Plane type - PRIMARY/OVERLAY/CURSOR
1880  * @possible_crtcs: bitmask of crtc that can be attached to the given pipe
1881  *
1882  * Initialize the plane.
1883  */
1884 struct drm_plane *dpu_plane_init(struct drm_device *dev,
1885 				 uint32_t pipe, enum drm_plane_type type,
1886 				 unsigned long possible_crtcs)
1887 {
1888 	struct drm_plane *plane = NULL;
1889 	struct msm_drm_private *priv = dev->dev_private;
1890 	struct dpu_kms *kms = to_dpu_kms(priv->kms);
1891 	struct dpu_hw_sspp *pipe_hw;
1892 
1893 	/* initialize underlying h/w driver */
1894 	pipe_hw = dpu_rm_get_sspp(&kms->rm, pipe);
1895 	if (!pipe_hw || !pipe_hw->cap || !pipe_hw->cap->sblk) {
1896 		DPU_ERROR("[%u]SSPP is invalid\n", pipe);
1897 		return ERR_PTR(-EINVAL);
1898 	}
1899 
1900 
1901 	plane = dpu_plane_init_common(dev, type, possible_crtcs,
1902 				      pipe_hw->cap->features & BIT(DPU_SSPP_INLINE_ROTATION),
1903 				      pipe_hw->cap->sblk->format_list,
1904 				      pipe_hw->cap->sblk->num_formats,
1905 				      pipe);
1906 	if (IS_ERR(plane))
1907 		return plane;
1908 
1909 	drm_plane_helper_add(plane, &dpu_plane_helper_funcs);
1910 
1911 	DPU_DEBUG("%s created for pipe:%u id:%u\n", plane->name,
1912 					pipe, plane->base.id);
1913 
1914 	return plane;
1915 }
1916 
1917 /**
1918  * dpu_plane_init_virtual - create new virtualized DPU plane
1919  * @dev:   Pointer to DRM device
1920  * @type:  Plane type - PRIMARY/OVERLAY/CURSOR
1921  * @possible_crtcs: bitmask of crtc that can be attached to the given pipe
1922  *
1923  * Initialize the virtual plane with no backing SSPP / pipe.
1924  */
1925 struct drm_plane *dpu_plane_init_virtual(struct drm_device *dev,
1926 					 enum drm_plane_type type,
1927 					 unsigned long possible_crtcs)
1928 {
1929 	struct drm_plane *plane = NULL;
1930 	struct msm_drm_private *priv = dev->dev_private;
1931 	struct dpu_kms *kms = to_dpu_kms(priv->kms);
1932 	bool has_inline_rotation = false;
1933 	const u32 *format_list = NULL;
1934 	u32 num_formats = 0;
1935 	int i;
1936 
1937 	/* Determine the largest configuration that we can implement */
1938 	for (i = 0; i < kms->catalog->sspp_count; i++) {
1939 		const struct dpu_sspp_cfg *cfg = &kms->catalog->sspp[i];
1940 
1941 		if (test_bit(DPU_SSPP_INLINE_ROTATION, &cfg->features))
1942 			has_inline_rotation = true;
1943 
1944 		if (!format_list ||
1945 		    cfg->sblk->csc_blk.len) {
1946 			format_list = cfg->sblk->format_list;
1947 			num_formats = cfg->sblk->num_formats;
1948 		}
1949 	}
1950 
1951 	plane = dpu_plane_init_common(dev, type, possible_crtcs,
1952 				      has_inline_rotation,
1953 				      format_list,
1954 				      num_formats,
1955 				      SSPP_NONE);
1956 	if (IS_ERR(plane))
1957 		return plane;
1958 
1959 	drm_plane_helper_add(plane, &dpu_plane_virtual_helper_funcs);
1960 
1961 	DPU_DEBUG("%s created virtual id:%u\n", plane->name, plane->base.id);
1962 
1963 	return plane;
1964 }
1965