xref: /linux/drivers/gpu/drm/msm/disp/dpu1/dpu_plane.c (revision 4e0ae876f77bc01a7e77724dea57b4b82bd53244)
1 /*
2  * Copyright (C) 2014-2018 The Linux Foundation. All rights reserved.
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #define pr_fmt(fmt)	"[drm:%s:%d] " fmt, __func__, __LINE__
20 
21 #include <linux/debugfs.h>
22 #include <linux/dma-buf.h>
23 
24 #include <drm/drm_atomic_uapi.h>
25 
26 #include "msm_drv.h"
27 #include "dpu_kms.h"
28 #include "dpu_formats.h"
29 #include "dpu_hw_sspp.h"
30 #include "dpu_hw_catalog_format.h"
31 #include "dpu_trace.h"
32 #include "dpu_crtc.h"
33 #include "dpu_vbif.h"
34 #include "dpu_plane.h"
35 
36 #define DPU_DEBUG_PLANE(pl, fmt, ...) DPU_DEBUG("plane%d " fmt,\
37 		(pl) ? (pl)->base.base.id : -1, ##__VA_ARGS__)
38 
39 #define DPU_ERROR_PLANE(pl, fmt, ...) DPU_ERROR("plane%d " fmt,\
40 		(pl) ? (pl)->base.base.id : -1, ##__VA_ARGS__)
41 
42 #define DECIMATED_DIMENSION(dim, deci) (((dim) + ((1 << (deci)) - 1)) >> (deci))
43 #define PHASE_STEP_SHIFT	21
44 #define PHASE_STEP_UNIT_SCALE   ((int) (1 << PHASE_STEP_SHIFT))
45 #define PHASE_RESIDUAL		15
46 
47 #define SHARP_STRENGTH_DEFAULT	32
48 #define SHARP_EDGE_THR_DEFAULT	112
49 #define SHARP_SMOOTH_THR_DEFAULT	8
50 #define SHARP_NOISE_THR_DEFAULT	2
51 
52 #define DPU_NAME_SIZE  12
53 
54 #define DPU_PLANE_COLOR_FILL_FLAG	BIT(31)
55 #define DPU_ZPOS_MAX 255
56 
57 /* multirect rect index */
58 enum {
59 	R0,
60 	R1,
61 	R_MAX
62 };
63 
64 #define DPU_QSEED3_DEFAULT_PRELOAD_H 0x4
65 #define DPU_QSEED3_DEFAULT_PRELOAD_V 0x3
66 
67 #define DEFAULT_REFRESH_RATE	60
68 
69 /**
70  * enum dpu_plane_qos - Different qos configurations for each pipe
71  *
72  * @DPU_PLANE_QOS_VBLANK_CTRL: Setup VBLANK qos for the pipe.
73  * @DPU_PLANE_QOS_VBLANK_AMORTIZE: Enables Amortization within pipe.
74  *	this configuration is mutually exclusive from VBLANK_CTRL.
75  * @DPU_PLANE_QOS_PANIC_CTRL: Setup panic for the pipe.
76  */
77 enum dpu_plane_qos {
78 	DPU_PLANE_QOS_VBLANK_CTRL = BIT(0),
79 	DPU_PLANE_QOS_VBLANK_AMORTIZE = BIT(1),
80 	DPU_PLANE_QOS_PANIC_CTRL = BIT(2),
81 };
82 
83 /*
84  * struct dpu_plane - local dpu plane structure
85  * @aspace: address space pointer
86  * @csc_ptr: Points to dpu_csc_cfg structure to use for current
87  * @mplane_list: List of multirect planes of the same pipe
88  * @catalog: Points to dpu catalog structure
89  * @revalidate: force revalidation of all the plane properties
90  */
91 struct dpu_plane {
92 	struct drm_plane base;
93 
94 	struct mutex lock;
95 
96 	enum dpu_sspp pipe;
97 	uint32_t features;      /* capabilities from catalog */
98 
99 	struct dpu_hw_pipe *pipe_hw;
100 	struct dpu_hw_pipe_cfg pipe_cfg;
101 	struct dpu_hw_pipe_qos_cfg pipe_qos_cfg;
102 	uint32_t color_fill;
103 	bool is_error;
104 	bool is_rt_pipe;
105 	bool is_virtual;
106 	struct list_head mplane_list;
107 	struct dpu_mdss_cfg *catalog;
108 
109 	struct dpu_csc_cfg *csc_ptr;
110 
111 	const struct dpu_sspp_sub_blks *pipe_sblk;
112 	char pipe_name[DPU_NAME_SIZE];
113 
114 	/* debugfs related stuff */
115 	struct dentry *debugfs_root;
116 	struct dpu_debugfs_regset32 debugfs_src;
117 	struct dpu_debugfs_regset32 debugfs_scaler;
118 	struct dpu_debugfs_regset32 debugfs_csc;
119 	bool debugfs_default_scale;
120 };
121 
122 static const uint64_t supported_format_modifiers[] = {
123 	DRM_FORMAT_MOD_QCOM_COMPRESSED,
124 	DRM_FORMAT_MOD_LINEAR,
125 	DRM_FORMAT_MOD_INVALID
126 };
127 
128 #define to_dpu_plane(x) container_of(x, struct dpu_plane, base)
129 
130 static struct dpu_kms *_dpu_plane_get_kms(struct drm_plane *plane)
131 {
132 	struct msm_drm_private *priv = plane->dev->dev_private;
133 
134 	return to_dpu_kms(priv->kms);
135 }
136 
137 /**
138  * _dpu_plane_calc_fill_level - calculate fill level of the given source format
139  * @plane:		Pointer to drm plane
140  * @fmt:		Pointer to source buffer format
141  * @src_wdith:		width of source buffer
142  * Return: fill level corresponding to the source buffer/format or 0 if error
143  */
144 static int _dpu_plane_calc_fill_level(struct drm_plane *plane,
145 		const struct dpu_format *fmt, u32 src_width)
146 {
147 	struct dpu_plane *pdpu, *tmp;
148 	struct dpu_plane_state *pstate;
149 	u32 fixed_buff_size;
150 	u32 total_fl;
151 
152 	if (!fmt || !plane->state || !src_width || !fmt->bpp) {
153 		DPU_ERROR("invalid arguments\n");
154 		return 0;
155 	}
156 
157 	pdpu = to_dpu_plane(plane);
158 	pstate = to_dpu_plane_state(plane->state);
159 	fixed_buff_size = pdpu->pipe_sblk->common->pixel_ram_size;
160 
161 	list_for_each_entry(tmp, &pdpu->mplane_list, mplane_list) {
162 		if (!tmp->base.state->visible)
163 			continue;
164 		DPU_DEBUG("plane%d/%d src_width:%d/%d\n",
165 				pdpu->base.base.id, tmp->base.base.id,
166 				src_width,
167 				drm_rect_width(&tmp->pipe_cfg.src_rect));
168 		src_width = max_t(u32, src_width,
169 				  drm_rect_width(&tmp->pipe_cfg.src_rect));
170 	}
171 
172 	if (fmt->fetch_planes == DPU_PLANE_PSEUDO_PLANAR) {
173 		if (fmt->chroma_sample == DPU_CHROMA_420) {
174 			/* NV12 */
175 			total_fl = (fixed_buff_size / 2) /
176 				((src_width + 32) * fmt->bpp);
177 		} else {
178 			/* non NV12 */
179 			total_fl = (fixed_buff_size / 2) * 2 /
180 				((src_width + 32) * fmt->bpp);
181 		}
182 	} else {
183 		if (pstate->multirect_mode == DPU_SSPP_MULTIRECT_PARALLEL) {
184 			total_fl = (fixed_buff_size / 2) * 2 /
185 				((src_width + 32) * fmt->bpp);
186 		} else {
187 			total_fl = (fixed_buff_size) * 2 /
188 				((src_width + 32) * fmt->bpp);
189 		}
190 	}
191 
192 	DPU_DEBUG("plane%u: pnum:%d fmt: %4.4s w:%u fl:%u\n",
193 			plane->base.id, pdpu->pipe - SSPP_VIG0,
194 			(char *)&fmt->base.pixel_format,
195 			src_width, total_fl);
196 
197 	return total_fl;
198 }
199 
200 /**
201  * _dpu_plane_get_qos_lut - get LUT mapping based on fill level
202  * @tbl:		Pointer to LUT table
203  * @total_fl:		fill level
204  * Return: LUT setting corresponding to the fill level
205  */
206 static u64 _dpu_plane_get_qos_lut(const struct dpu_qos_lut_tbl *tbl,
207 		u32 total_fl)
208 {
209 	int i;
210 
211 	if (!tbl || !tbl->nentry || !tbl->entries)
212 		return 0;
213 
214 	for (i = 0; i < tbl->nentry; i++)
215 		if (total_fl <= tbl->entries[i].fl)
216 			return tbl->entries[i].lut;
217 
218 	/* if last fl is zero, use as default */
219 	if (!tbl->entries[i-1].fl)
220 		return tbl->entries[i-1].lut;
221 
222 	return 0;
223 }
224 
225 /**
226  * _dpu_plane_set_qos_lut - set QoS LUT of the given plane
227  * @plane:		Pointer to drm plane
228  * @fb:			Pointer to framebuffer associated with the given plane
229  */
230 static void _dpu_plane_set_qos_lut(struct drm_plane *plane,
231 		struct drm_framebuffer *fb)
232 {
233 	struct dpu_plane *pdpu = to_dpu_plane(plane);
234 	const struct dpu_format *fmt = NULL;
235 	u64 qos_lut;
236 	u32 total_fl = 0, lut_usage;
237 
238 	if (!pdpu->is_rt_pipe) {
239 		lut_usage = DPU_QOS_LUT_USAGE_NRT;
240 	} else {
241 		fmt = dpu_get_dpu_format_ext(
242 				fb->format->format,
243 				fb->modifier);
244 		total_fl = _dpu_plane_calc_fill_level(plane, fmt,
245 				drm_rect_width(&pdpu->pipe_cfg.src_rect));
246 
247 		if (fmt && DPU_FORMAT_IS_LINEAR(fmt))
248 			lut_usage = DPU_QOS_LUT_USAGE_LINEAR;
249 		else
250 			lut_usage = DPU_QOS_LUT_USAGE_MACROTILE;
251 	}
252 
253 	qos_lut = _dpu_plane_get_qos_lut(
254 			&pdpu->catalog->perf.qos_lut_tbl[lut_usage], total_fl);
255 
256 	pdpu->pipe_qos_cfg.creq_lut = qos_lut;
257 
258 	trace_dpu_perf_set_qos_luts(pdpu->pipe - SSPP_VIG0,
259 			(fmt) ? fmt->base.pixel_format : 0,
260 			pdpu->is_rt_pipe, total_fl, qos_lut, lut_usage);
261 
262 	DPU_DEBUG("plane%u: pnum:%d fmt: %4.4s rt:%d fl:%u lut:0x%llx\n",
263 			plane->base.id,
264 			pdpu->pipe - SSPP_VIG0,
265 			fmt ? (char *)&fmt->base.pixel_format : NULL,
266 			pdpu->is_rt_pipe, total_fl, qos_lut);
267 
268 	pdpu->pipe_hw->ops.setup_creq_lut(pdpu->pipe_hw, &pdpu->pipe_qos_cfg);
269 }
270 
271 /**
272  * _dpu_plane_set_panic_lut - set danger/safe LUT of the given plane
273  * @plane:		Pointer to drm plane
274  * @fb:			Pointer to framebuffer associated with the given plane
275  */
276 static void _dpu_plane_set_danger_lut(struct drm_plane *plane,
277 		struct drm_framebuffer *fb)
278 {
279 	struct dpu_plane *pdpu = to_dpu_plane(plane);
280 	const struct dpu_format *fmt = NULL;
281 	u32 danger_lut, safe_lut;
282 
283 	if (!pdpu->is_rt_pipe) {
284 		danger_lut = pdpu->catalog->perf.danger_lut_tbl
285 				[DPU_QOS_LUT_USAGE_NRT];
286 		safe_lut = pdpu->catalog->perf.safe_lut_tbl
287 				[DPU_QOS_LUT_USAGE_NRT];
288 	} else {
289 		fmt = dpu_get_dpu_format_ext(
290 				fb->format->format,
291 				fb->modifier);
292 
293 		if (fmt && DPU_FORMAT_IS_LINEAR(fmt)) {
294 			danger_lut = pdpu->catalog->perf.danger_lut_tbl
295 					[DPU_QOS_LUT_USAGE_LINEAR];
296 			safe_lut = pdpu->catalog->perf.safe_lut_tbl
297 					[DPU_QOS_LUT_USAGE_LINEAR];
298 		} else {
299 			danger_lut = pdpu->catalog->perf.danger_lut_tbl
300 					[DPU_QOS_LUT_USAGE_MACROTILE];
301 			safe_lut = pdpu->catalog->perf.safe_lut_tbl
302 					[DPU_QOS_LUT_USAGE_MACROTILE];
303 		}
304 	}
305 
306 	pdpu->pipe_qos_cfg.danger_lut = danger_lut;
307 	pdpu->pipe_qos_cfg.safe_lut = safe_lut;
308 
309 	trace_dpu_perf_set_danger_luts(pdpu->pipe - SSPP_VIG0,
310 			(fmt) ? fmt->base.pixel_format : 0,
311 			(fmt) ? fmt->fetch_mode : 0,
312 			pdpu->pipe_qos_cfg.danger_lut,
313 			pdpu->pipe_qos_cfg.safe_lut);
314 
315 	DPU_DEBUG("plane%u: pnum:%d fmt: %4.4s mode:%d luts[0x%x, 0x%x]\n",
316 		plane->base.id,
317 		pdpu->pipe - SSPP_VIG0,
318 		fmt ? (char *)&fmt->base.pixel_format : NULL,
319 		fmt ? fmt->fetch_mode : -1,
320 		pdpu->pipe_qos_cfg.danger_lut,
321 		pdpu->pipe_qos_cfg.safe_lut);
322 
323 	pdpu->pipe_hw->ops.setup_danger_safe_lut(pdpu->pipe_hw,
324 			&pdpu->pipe_qos_cfg);
325 }
326 
327 /**
328  * _dpu_plane_set_qos_ctrl - set QoS control of the given plane
329  * @plane:		Pointer to drm plane
330  * @enable:		true to enable QoS control
331  * @flags:		QoS control mode (enum dpu_plane_qos)
332  */
333 static void _dpu_plane_set_qos_ctrl(struct drm_plane *plane,
334 	bool enable, u32 flags)
335 {
336 	struct dpu_plane *pdpu = to_dpu_plane(plane);
337 
338 	if (flags & DPU_PLANE_QOS_VBLANK_CTRL) {
339 		pdpu->pipe_qos_cfg.creq_vblank = pdpu->pipe_sblk->creq_vblank;
340 		pdpu->pipe_qos_cfg.danger_vblank =
341 				pdpu->pipe_sblk->danger_vblank;
342 		pdpu->pipe_qos_cfg.vblank_en = enable;
343 	}
344 
345 	if (flags & DPU_PLANE_QOS_VBLANK_AMORTIZE) {
346 		/* this feature overrules previous VBLANK_CTRL */
347 		pdpu->pipe_qos_cfg.vblank_en = false;
348 		pdpu->pipe_qos_cfg.creq_vblank = 0; /* clear vblank bits */
349 	}
350 
351 	if (flags & DPU_PLANE_QOS_PANIC_CTRL)
352 		pdpu->pipe_qos_cfg.danger_safe_en = enable;
353 
354 	if (!pdpu->is_rt_pipe) {
355 		pdpu->pipe_qos_cfg.vblank_en = false;
356 		pdpu->pipe_qos_cfg.danger_safe_en = false;
357 	}
358 
359 	DPU_DEBUG("plane%u: pnum:%d ds:%d vb:%d pri[0x%x, 0x%x] is_rt:%d\n",
360 		plane->base.id,
361 		pdpu->pipe - SSPP_VIG0,
362 		pdpu->pipe_qos_cfg.danger_safe_en,
363 		pdpu->pipe_qos_cfg.vblank_en,
364 		pdpu->pipe_qos_cfg.creq_vblank,
365 		pdpu->pipe_qos_cfg.danger_vblank,
366 		pdpu->is_rt_pipe);
367 
368 	pdpu->pipe_hw->ops.setup_qos_ctrl(pdpu->pipe_hw,
369 			&pdpu->pipe_qos_cfg);
370 }
371 
372 /**
373  * _dpu_plane_set_ot_limit - set OT limit for the given plane
374  * @plane:		Pointer to drm plane
375  * @crtc:		Pointer to drm crtc
376  */
377 static void _dpu_plane_set_ot_limit(struct drm_plane *plane,
378 		struct drm_crtc *crtc)
379 {
380 	struct dpu_plane *pdpu = to_dpu_plane(plane);
381 	struct dpu_vbif_set_ot_params ot_params;
382 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
383 
384 	memset(&ot_params, 0, sizeof(ot_params));
385 	ot_params.xin_id = pdpu->pipe_hw->cap->xin_id;
386 	ot_params.num = pdpu->pipe_hw->idx - SSPP_NONE;
387 	ot_params.width = drm_rect_width(&pdpu->pipe_cfg.src_rect);
388 	ot_params.height = drm_rect_height(&pdpu->pipe_cfg.src_rect);
389 	ot_params.is_wfd = !pdpu->is_rt_pipe;
390 	ot_params.frame_rate = crtc->mode.vrefresh;
391 	ot_params.vbif_idx = VBIF_RT;
392 	ot_params.clk_ctrl = pdpu->pipe_hw->cap->clk_ctrl;
393 	ot_params.rd = true;
394 
395 	dpu_vbif_set_ot_limit(dpu_kms, &ot_params);
396 }
397 
398 /**
399  * _dpu_plane_set_vbif_qos - set vbif QoS for the given plane
400  * @plane:		Pointer to drm plane
401  */
402 static void _dpu_plane_set_qos_remap(struct drm_plane *plane)
403 {
404 	struct dpu_plane *pdpu = to_dpu_plane(plane);
405 	struct dpu_vbif_set_qos_params qos_params;
406 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
407 
408 	memset(&qos_params, 0, sizeof(qos_params));
409 	qos_params.vbif_idx = VBIF_RT;
410 	qos_params.clk_ctrl = pdpu->pipe_hw->cap->clk_ctrl;
411 	qos_params.xin_id = pdpu->pipe_hw->cap->xin_id;
412 	qos_params.num = pdpu->pipe_hw->idx - SSPP_VIG0;
413 	qos_params.is_rt = pdpu->is_rt_pipe;
414 
415 	DPU_DEBUG("plane%d pipe:%d vbif:%d xin:%d rt:%d, clk_ctrl:%d\n",
416 			plane->base.id, qos_params.num,
417 			qos_params.vbif_idx,
418 			qos_params.xin_id, qos_params.is_rt,
419 			qos_params.clk_ctrl);
420 
421 	dpu_vbif_set_qos_remap(dpu_kms, &qos_params);
422 }
423 
424 static void _dpu_plane_set_scanout(struct drm_plane *plane,
425 		struct dpu_plane_state *pstate,
426 		struct dpu_hw_pipe_cfg *pipe_cfg,
427 		struct drm_framebuffer *fb)
428 {
429 	struct dpu_plane *pdpu = to_dpu_plane(plane);
430 	struct dpu_kms *kms = _dpu_plane_get_kms(&pdpu->base);
431 	struct msm_gem_address_space *aspace = kms->base.aspace;
432 	int ret;
433 
434 	ret = dpu_format_populate_layout(aspace, fb, &pipe_cfg->layout);
435 	if (ret == -EAGAIN)
436 		DPU_DEBUG_PLANE(pdpu, "not updating same src addrs\n");
437 	else if (ret)
438 		DPU_ERROR_PLANE(pdpu, "failed to get format layout, %d\n", ret);
439 	else if (pdpu->pipe_hw->ops.setup_sourceaddress) {
440 		trace_dpu_plane_set_scanout(pdpu->pipe_hw->idx,
441 					    &pipe_cfg->layout,
442 					    pstate->multirect_index);
443 		pdpu->pipe_hw->ops.setup_sourceaddress(pdpu->pipe_hw, pipe_cfg,
444 						pstate->multirect_index);
445 	}
446 }
447 
448 static void _dpu_plane_setup_scaler3(struct dpu_plane *pdpu,
449 		struct dpu_plane_state *pstate,
450 		uint32_t src_w, uint32_t src_h, uint32_t dst_w, uint32_t dst_h,
451 		struct dpu_hw_scaler3_cfg *scale_cfg,
452 		const struct dpu_format *fmt,
453 		uint32_t chroma_subsmpl_h, uint32_t chroma_subsmpl_v)
454 {
455 	uint32_t i;
456 
457 	memset(scale_cfg, 0, sizeof(*scale_cfg));
458 	memset(&pstate->pixel_ext, 0, sizeof(struct dpu_hw_pixel_ext));
459 
460 	scale_cfg->phase_step_x[DPU_SSPP_COMP_0] =
461 		mult_frac((1 << PHASE_STEP_SHIFT), src_w, dst_w);
462 	scale_cfg->phase_step_y[DPU_SSPP_COMP_0] =
463 		mult_frac((1 << PHASE_STEP_SHIFT), src_h, dst_h);
464 
465 
466 	scale_cfg->phase_step_y[DPU_SSPP_COMP_1_2] =
467 		scale_cfg->phase_step_y[DPU_SSPP_COMP_0] / chroma_subsmpl_v;
468 	scale_cfg->phase_step_x[DPU_SSPP_COMP_1_2] =
469 		scale_cfg->phase_step_x[DPU_SSPP_COMP_0] / chroma_subsmpl_h;
470 
471 	scale_cfg->phase_step_x[DPU_SSPP_COMP_2] =
472 		scale_cfg->phase_step_x[DPU_SSPP_COMP_1_2];
473 	scale_cfg->phase_step_y[DPU_SSPP_COMP_2] =
474 		scale_cfg->phase_step_y[DPU_SSPP_COMP_1_2];
475 
476 	scale_cfg->phase_step_x[DPU_SSPP_COMP_3] =
477 		scale_cfg->phase_step_x[DPU_SSPP_COMP_0];
478 	scale_cfg->phase_step_y[DPU_SSPP_COMP_3] =
479 		scale_cfg->phase_step_y[DPU_SSPP_COMP_0];
480 
481 	for (i = 0; i < DPU_MAX_PLANES; i++) {
482 		scale_cfg->src_width[i] = src_w;
483 		scale_cfg->src_height[i] = src_h;
484 		if (i == DPU_SSPP_COMP_1_2 || i == DPU_SSPP_COMP_2) {
485 			scale_cfg->src_width[i] /= chroma_subsmpl_h;
486 			scale_cfg->src_height[i] /= chroma_subsmpl_v;
487 		}
488 		scale_cfg->preload_x[i] = DPU_QSEED3_DEFAULT_PRELOAD_H;
489 		scale_cfg->preload_y[i] = DPU_QSEED3_DEFAULT_PRELOAD_V;
490 		pstate->pixel_ext.num_ext_pxls_top[i] =
491 			scale_cfg->src_height[i];
492 		pstate->pixel_ext.num_ext_pxls_left[i] =
493 			scale_cfg->src_width[i];
494 	}
495 	if (!(DPU_FORMAT_IS_YUV(fmt)) && (src_h == dst_h)
496 		&& (src_w == dst_w))
497 		return;
498 
499 	scale_cfg->dst_width = dst_w;
500 	scale_cfg->dst_height = dst_h;
501 	scale_cfg->y_rgb_filter_cfg = DPU_SCALE_BIL;
502 	scale_cfg->uv_filter_cfg = DPU_SCALE_BIL;
503 	scale_cfg->alpha_filter_cfg = DPU_SCALE_ALPHA_BIL;
504 	scale_cfg->lut_flag = 0;
505 	scale_cfg->blend_cfg = 1;
506 	scale_cfg->enable = 1;
507 }
508 
509 static void _dpu_plane_setup_csc(struct dpu_plane *pdpu)
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 	static const struct dpu_csc_cfg dpu_csc10_YUV2RGB_601L = {
526 		{
527 			/* S15.16 format */
528 			0x00012A00, 0x00000000, 0x00019880,
529 			0x00012A00, 0xFFFF9B80, 0xFFFF3000,
530 			0x00012A00, 0x00020480, 0x00000000,
531 			},
532 		/* signed bias */
533 		{ 0xffc0, 0xfe00, 0xfe00,},
534 		{ 0x0, 0x0, 0x0,},
535 		/* unsigned clamp */
536 		{ 0x40, 0x3ac, 0x40, 0x3c0, 0x40, 0x3c0,},
537 		{ 0x00, 0x3ff, 0x00, 0x3ff, 0x00, 0x3ff,},
538 	};
539 
540 	if (!pdpu) {
541 		DPU_ERROR("invalid plane\n");
542 		return;
543 	}
544 
545 	if (BIT(DPU_SSPP_CSC_10BIT) & pdpu->features)
546 		pdpu->csc_ptr = (struct dpu_csc_cfg *)&dpu_csc10_YUV2RGB_601L;
547 	else
548 		pdpu->csc_ptr = (struct dpu_csc_cfg *)&dpu_csc_YUV2RGB_601L;
549 
550 	DPU_DEBUG_PLANE(pdpu, "using 0x%X 0x%X 0x%X...\n",
551 			pdpu->csc_ptr->csc_mv[0],
552 			pdpu->csc_ptr->csc_mv[1],
553 			pdpu->csc_ptr->csc_mv[2]);
554 }
555 
556 static void _dpu_plane_setup_scaler(struct dpu_plane *pdpu,
557 		struct dpu_plane_state *pstate,
558 		const struct dpu_format *fmt, bool color_fill)
559 {
560 	uint32_t chroma_subsmpl_h, chroma_subsmpl_v;
561 
562 	/* don't chroma subsample if decimating */
563 	chroma_subsmpl_h =
564 		drm_format_horz_chroma_subsampling(fmt->base.pixel_format);
565 	chroma_subsmpl_v =
566 		drm_format_vert_chroma_subsampling(fmt->base.pixel_format);
567 
568 	/* update scaler. calculate default config for QSEED3 */
569 	_dpu_plane_setup_scaler3(pdpu, pstate,
570 			drm_rect_width(&pdpu->pipe_cfg.src_rect),
571 			drm_rect_height(&pdpu->pipe_cfg.src_rect),
572 			drm_rect_width(&pdpu->pipe_cfg.dst_rect),
573 			drm_rect_height(&pdpu->pipe_cfg.dst_rect),
574 			&pstate->scaler3_cfg, fmt,
575 			chroma_subsmpl_h, chroma_subsmpl_v);
576 }
577 
578 /**
579  * _dpu_plane_color_fill - enables color fill on plane
580  * @pdpu:   Pointer to DPU plane object
581  * @color:  RGB fill color value, [23..16] Blue, [15..8] Green, [7..0] Red
582  * @alpha:  8-bit fill alpha value, 255 selects 100% alpha
583  * Returns: 0 on success
584  */
585 static int _dpu_plane_color_fill(struct dpu_plane *pdpu,
586 		uint32_t color, uint32_t alpha)
587 {
588 	const struct dpu_format *fmt;
589 	const struct drm_plane *plane = &pdpu->base;
590 	struct dpu_plane_state *pstate = to_dpu_plane_state(plane->state);
591 
592 	DPU_DEBUG_PLANE(pdpu, "\n");
593 
594 	/*
595 	 * select fill format to match user property expectation,
596 	 * h/w only supports RGB variants
597 	 */
598 	fmt = dpu_get_dpu_format(DRM_FORMAT_ABGR8888);
599 
600 	/* update sspp */
601 	if (fmt && pdpu->pipe_hw->ops.setup_solidfill) {
602 		pdpu->pipe_hw->ops.setup_solidfill(pdpu->pipe_hw,
603 				(color & 0xFFFFFF) | ((alpha & 0xFF) << 24),
604 				pstate->multirect_index);
605 
606 		/* override scaler/decimation if solid fill */
607 		pdpu->pipe_cfg.src_rect.x1 = 0;
608 		pdpu->pipe_cfg.src_rect.y1 = 0;
609 		pdpu->pipe_cfg.src_rect.x2 =
610 			drm_rect_width(&pdpu->pipe_cfg.dst_rect);
611 		pdpu->pipe_cfg.src_rect.y2 =
612 			drm_rect_height(&pdpu->pipe_cfg.dst_rect);
613 		_dpu_plane_setup_scaler(pdpu, pstate, fmt, true);
614 
615 		if (pdpu->pipe_hw->ops.setup_format)
616 			pdpu->pipe_hw->ops.setup_format(pdpu->pipe_hw,
617 					fmt, DPU_SSPP_SOLID_FILL,
618 					pstate->multirect_index);
619 
620 		if (pdpu->pipe_hw->ops.setup_rects)
621 			pdpu->pipe_hw->ops.setup_rects(pdpu->pipe_hw,
622 					&pdpu->pipe_cfg,
623 					pstate->multirect_index);
624 
625 		if (pdpu->pipe_hw->ops.setup_pe)
626 			pdpu->pipe_hw->ops.setup_pe(pdpu->pipe_hw,
627 					&pstate->pixel_ext);
628 
629 		if (pdpu->pipe_hw->ops.setup_scaler &&
630 				pstate->multirect_index != DPU_SSPP_RECT_1)
631 			pdpu->pipe_hw->ops.setup_scaler(pdpu->pipe_hw,
632 					&pdpu->pipe_cfg, &pstate->pixel_ext,
633 					&pstate->scaler3_cfg);
634 	}
635 
636 	return 0;
637 }
638 
639 void dpu_plane_clear_multirect(const struct drm_plane_state *drm_state)
640 {
641 	struct dpu_plane_state *pstate = to_dpu_plane_state(drm_state);
642 
643 	pstate->multirect_index = DPU_SSPP_RECT_SOLO;
644 	pstate->multirect_mode = DPU_SSPP_MULTIRECT_NONE;
645 }
646 
647 int dpu_plane_validate_multirect_v2(struct dpu_multirect_plane_states *plane)
648 {
649 	struct dpu_plane_state *pstate[R_MAX];
650 	const struct drm_plane_state *drm_state[R_MAX];
651 	struct drm_rect src[R_MAX], dst[R_MAX];
652 	struct dpu_plane *dpu_plane[R_MAX];
653 	const struct dpu_format *fmt[R_MAX];
654 	int i, buffer_lines;
655 	unsigned int max_tile_height = 1;
656 	bool parallel_fetch_qualified = true;
657 	bool has_tiled_rect = false;
658 
659 	for (i = 0; i < R_MAX; i++) {
660 		const struct msm_format *msm_fmt;
661 
662 		drm_state[i] = i ? plane->r1 : plane->r0;
663 		msm_fmt = msm_framebuffer_format(drm_state[i]->fb);
664 		fmt[i] = to_dpu_format(msm_fmt);
665 
666 		if (DPU_FORMAT_IS_UBWC(fmt[i])) {
667 			has_tiled_rect = true;
668 			if (fmt[i]->tile_height > max_tile_height)
669 				max_tile_height = fmt[i]->tile_height;
670 		}
671 	}
672 
673 	for (i = 0; i < R_MAX; i++) {
674 		int width_threshold;
675 
676 		pstate[i] = to_dpu_plane_state(drm_state[i]);
677 		dpu_plane[i] = to_dpu_plane(drm_state[i]->plane);
678 
679 		if (pstate[i] == NULL) {
680 			DPU_ERROR("DPU plane state of plane id %d is NULL\n",
681 				drm_state[i]->plane->base.id);
682 			return -EINVAL;
683 		}
684 
685 		src[i].x1 = drm_state[i]->src_x >> 16;
686 		src[i].y1 = drm_state[i]->src_y >> 16;
687 		src[i].x2 = src[i].x1 + (drm_state[i]->src_w >> 16);
688 		src[i].y2 = src[i].y1 + (drm_state[i]->src_h >> 16);
689 
690 		dst[i] = drm_plane_state_dest(drm_state[i]);
691 
692 		if (drm_rect_calc_hscale(&src[i], &dst[i], 1, 1) != 1 ||
693 		    drm_rect_calc_vscale(&src[i], &dst[i], 1, 1) != 1) {
694 			DPU_ERROR_PLANE(dpu_plane[i],
695 				"scaling is not supported in multirect mode\n");
696 			return -EINVAL;
697 		}
698 
699 		if (DPU_FORMAT_IS_YUV(fmt[i])) {
700 			DPU_ERROR_PLANE(dpu_plane[i],
701 				"Unsupported format for multirect mode\n");
702 			return -EINVAL;
703 		}
704 
705 		/**
706 		 * SSPP PD_MEM is split half - one for each RECT.
707 		 * Tiled formats need 5 lines of buffering while fetching
708 		 * whereas linear formats need only 2 lines.
709 		 * So we cannot support more than half of the supported SSPP
710 		 * width for tiled formats.
711 		 */
712 		width_threshold = dpu_plane[i]->pipe_sblk->common->maxlinewidth;
713 		if (has_tiled_rect)
714 			width_threshold /= 2;
715 
716 		if (parallel_fetch_qualified &&
717 		    drm_rect_width(&src[i]) > width_threshold)
718 			parallel_fetch_qualified = false;
719 
720 	}
721 
722 	/* Validate RECT's and set the mode */
723 
724 	/* Prefer PARALLEL FETCH Mode over TIME_MX Mode */
725 	if (parallel_fetch_qualified) {
726 		pstate[R0]->multirect_mode = DPU_SSPP_MULTIRECT_PARALLEL;
727 		pstate[R1]->multirect_mode = DPU_SSPP_MULTIRECT_PARALLEL;
728 
729 		goto done;
730 	}
731 
732 	/* TIME_MX Mode */
733 	buffer_lines = 2 * max_tile_height;
734 
735 	if (dst[R1].y1 >= dst[R0].y2 + buffer_lines ||
736 	    dst[R0].y1 >= dst[R1].y2 + buffer_lines) {
737 		pstate[R0]->multirect_mode = DPU_SSPP_MULTIRECT_TIME_MX;
738 		pstate[R1]->multirect_mode = DPU_SSPP_MULTIRECT_TIME_MX;
739 	} else {
740 		DPU_ERROR(
741 			"No multirect mode possible for the planes (%d - %d)\n",
742 			drm_state[R0]->plane->base.id,
743 			drm_state[R1]->plane->base.id);
744 		return -EINVAL;
745 	}
746 
747 done:
748 	if (dpu_plane[R0]->is_virtual) {
749 		pstate[R0]->multirect_index = DPU_SSPP_RECT_1;
750 		pstate[R1]->multirect_index = DPU_SSPP_RECT_0;
751 	} else {
752 		pstate[R0]->multirect_index = DPU_SSPP_RECT_0;
753 		pstate[R1]->multirect_index = DPU_SSPP_RECT_1;
754 	};
755 
756 	DPU_DEBUG_PLANE(dpu_plane[R0], "R0: %d - %d\n",
757 		pstate[R0]->multirect_mode, pstate[R0]->multirect_index);
758 	DPU_DEBUG_PLANE(dpu_plane[R1], "R1: %d - %d\n",
759 		pstate[R1]->multirect_mode, pstate[R1]->multirect_index);
760 	return 0;
761 }
762 
763 /**
764  * dpu_plane_get_ctl_flush - get control flush for the given plane
765  * @plane: Pointer to drm plane structure
766  * @ctl: Pointer to hardware control driver
767  * @flush_sspp: Pointer to sspp flush control word
768  */
769 void dpu_plane_get_ctl_flush(struct drm_plane *plane, struct dpu_hw_ctl *ctl,
770 		u32 *flush_sspp)
771 {
772 	*flush_sspp = ctl->ops.get_bitmask_sspp(ctl, dpu_plane_pipe(plane));
773 }
774 
775 static int dpu_plane_prepare_fb(struct drm_plane *plane,
776 		struct drm_plane_state *new_state)
777 {
778 	struct drm_framebuffer *fb = new_state->fb;
779 	struct dpu_plane *pdpu = to_dpu_plane(plane);
780 	struct dpu_plane_state *pstate = to_dpu_plane_state(new_state);
781 	struct dpu_hw_fmt_layout layout;
782 	struct drm_gem_object *obj;
783 	struct msm_gem_object *msm_obj;
784 	struct dma_fence *fence;
785 	struct dpu_kms *kms = _dpu_plane_get_kms(&pdpu->base);
786 	int ret;
787 
788 	if (!new_state->fb)
789 		return 0;
790 
791 	DPU_DEBUG_PLANE(pdpu, "FB[%u]\n", fb->base.id);
792 
793 	/* cache aspace */
794 	pstate->aspace = kms->base.aspace;
795 
796 	/*
797 	 * TODO: Need to sort out the msm_framebuffer_prepare() call below so
798 	 *       we can use msm_atomic_prepare_fb() instead of doing the
799 	 *       implicit fence and fb prepare by hand here.
800 	 */
801 	obj = msm_framebuffer_bo(new_state->fb, 0);
802 	msm_obj = to_msm_bo(obj);
803 	fence = reservation_object_get_excl_rcu(msm_obj->resv);
804 	if (fence)
805 		drm_atomic_set_fence_for_plane(new_state, fence);
806 
807 	if (pstate->aspace) {
808 		ret = msm_framebuffer_prepare(new_state->fb,
809 				pstate->aspace);
810 		if (ret) {
811 			DPU_ERROR("failed to prepare framebuffer\n");
812 			return ret;
813 		}
814 	}
815 
816 	/* validate framebuffer layout before commit */
817 	ret = dpu_format_populate_layout(pstate->aspace,
818 			new_state->fb, &layout);
819 	if (ret) {
820 		DPU_ERROR_PLANE(pdpu, "failed to get format layout, %d\n", ret);
821 		return ret;
822 	}
823 
824 	return 0;
825 }
826 
827 static void dpu_plane_cleanup_fb(struct drm_plane *plane,
828 		struct drm_plane_state *old_state)
829 {
830 	struct dpu_plane *pdpu = to_dpu_plane(plane);
831 	struct dpu_plane_state *old_pstate;
832 
833 	if (!old_state || !old_state->fb)
834 		return;
835 
836 	old_pstate = to_dpu_plane_state(old_state);
837 
838 	DPU_DEBUG_PLANE(pdpu, "FB[%u]\n", old_state->fb->base.id);
839 
840 	msm_framebuffer_cleanup(old_state->fb, old_pstate->aspace);
841 }
842 
843 static bool dpu_plane_validate_src(struct drm_rect *src,
844 				   struct drm_rect *fb_rect,
845 				   uint32_t min_src_size)
846 {
847 	/* Ensure fb size is supported */
848 	if (drm_rect_width(fb_rect) > MAX_IMG_WIDTH ||
849 	    drm_rect_height(fb_rect) > MAX_IMG_HEIGHT)
850 		return false;
851 
852 	/* Ensure src rect is above the minimum size */
853 	if (drm_rect_width(src) < min_src_size ||
854 	    drm_rect_height(src) < min_src_size)
855 		return false;
856 
857 	/* Ensure src is fully encapsulated in fb */
858 	return drm_rect_intersect(fb_rect, src) &&
859 		drm_rect_equals(fb_rect, src);
860 }
861 
862 static int dpu_plane_atomic_check(struct drm_plane *plane,
863 				  struct drm_plane_state *state)
864 {
865 	int ret = 0, min_scale;
866 	struct dpu_plane *pdpu = to_dpu_plane(plane);
867 	const struct drm_crtc_state *crtc_state = NULL;
868 	const struct dpu_format *fmt;
869 	struct drm_rect src, dst, fb_rect = { 0 };
870 	uint32_t min_src_size, max_linewidth;
871 
872 	if (state->crtc)
873 		crtc_state = drm_atomic_get_new_crtc_state(state->state,
874 							   state->crtc);
875 
876 	min_scale = FRAC_16_16(1, pdpu->pipe_sblk->maxdwnscale);
877 	ret = drm_atomic_helper_check_plane_state(state, crtc_state, min_scale,
878 					  pdpu->pipe_sblk->maxupscale << 16,
879 					  true, true);
880 	if (ret) {
881 		DPU_ERROR_PLANE(pdpu, "Check plane state failed (%d)\n", ret);
882 		return ret;
883 	}
884 	if (!state->visible)
885 		return 0;
886 
887 	src.x1 = state->src_x >> 16;
888 	src.y1 = state->src_y >> 16;
889 	src.x2 = src.x1 + (state->src_w >> 16);
890 	src.y2 = src.y1 + (state->src_h >> 16);
891 
892 	dst = drm_plane_state_dest(state);
893 
894 	fb_rect.x2 = state->fb->width;
895 	fb_rect.y2 = state->fb->height;
896 
897 	max_linewidth = pdpu->pipe_sblk->common->maxlinewidth;
898 
899 	fmt = to_dpu_format(msm_framebuffer_format(state->fb));
900 
901 	min_src_size = DPU_FORMAT_IS_YUV(fmt) ? 2 : 1;
902 
903 	if (DPU_FORMAT_IS_YUV(fmt) &&
904 		(!(pdpu->features & DPU_SSPP_SCALER) ||
905 		 !(pdpu->features & (BIT(DPU_SSPP_CSC)
906 		 | BIT(DPU_SSPP_CSC_10BIT))))) {
907 		DPU_ERROR_PLANE(pdpu,
908 				"plane doesn't have scaler/csc for yuv\n");
909 		return -EINVAL;
910 
911 	/* check src bounds */
912 	} else if (!dpu_plane_validate_src(&src, &fb_rect, min_src_size)) {
913 		DPU_ERROR_PLANE(pdpu, "invalid source " DRM_RECT_FMT "\n",
914 				DRM_RECT_ARG(&src));
915 		return -E2BIG;
916 
917 	/* valid yuv image */
918 	} else if (DPU_FORMAT_IS_YUV(fmt) &&
919 		   (src.x1 & 0x1 || src.y1 & 0x1 ||
920 		    drm_rect_width(&src) & 0x1 ||
921 		    drm_rect_height(&src) & 0x1)) {
922 		DPU_ERROR_PLANE(pdpu, "invalid yuv source " DRM_RECT_FMT "\n",
923 				DRM_RECT_ARG(&src));
924 		return -EINVAL;
925 
926 	/* min dst support */
927 	} else if (drm_rect_width(&dst) < 0x1 || drm_rect_height(&dst) < 0x1) {
928 		DPU_ERROR_PLANE(pdpu, "invalid dest rect " DRM_RECT_FMT "\n",
929 				DRM_RECT_ARG(&dst));
930 		return -EINVAL;
931 
932 	/* check decimated source width */
933 	} else if (drm_rect_width(&src) > max_linewidth) {
934 		DPU_ERROR_PLANE(pdpu, "invalid src " DRM_RECT_FMT " line:%u\n",
935 				DRM_RECT_ARG(&src), max_linewidth);
936 		return -E2BIG;
937 	}
938 
939 	return 0;
940 }
941 
942 void dpu_plane_flush(struct drm_plane *plane)
943 {
944 	struct dpu_plane *pdpu;
945 	struct dpu_plane_state *pstate;
946 
947 	if (!plane || !plane->state) {
948 		DPU_ERROR("invalid plane\n");
949 		return;
950 	}
951 
952 	pdpu = to_dpu_plane(plane);
953 	pstate = to_dpu_plane_state(plane->state);
954 
955 	/*
956 	 * These updates have to be done immediately before the plane flush
957 	 * timing, and may not be moved to the atomic_update/mode_set functions.
958 	 */
959 	if (pdpu->is_error)
960 		/* force white frame with 100% alpha pipe output on error */
961 		_dpu_plane_color_fill(pdpu, 0xFFFFFF, 0xFF);
962 	else if (pdpu->color_fill & DPU_PLANE_COLOR_FILL_FLAG)
963 		/* force 100% alpha */
964 		_dpu_plane_color_fill(pdpu, pdpu->color_fill, 0xFF);
965 	else if (pdpu->pipe_hw && pdpu->csc_ptr && pdpu->pipe_hw->ops.setup_csc)
966 		pdpu->pipe_hw->ops.setup_csc(pdpu->pipe_hw, pdpu->csc_ptr);
967 
968 	/* flag h/w flush complete */
969 	if (plane->state)
970 		pstate->pending = false;
971 }
972 
973 /**
974  * dpu_plane_set_error: enable/disable error condition
975  * @plane: pointer to drm_plane structure
976  */
977 void dpu_plane_set_error(struct drm_plane *plane, bool error)
978 {
979 	struct dpu_plane *pdpu;
980 
981 	if (!plane)
982 		return;
983 
984 	pdpu = to_dpu_plane(plane);
985 	pdpu->is_error = error;
986 }
987 
988 static void dpu_plane_sspp_atomic_update(struct drm_plane *plane)
989 {
990 	uint32_t src_flags;
991 	struct dpu_plane *pdpu = to_dpu_plane(plane);
992 	struct drm_plane_state *state = plane->state;
993 	struct dpu_plane_state *pstate = to_dpu_plane_state(state);
994 	struct drm_crtc *crtc = state->crtc;
995 	struct drm_framebuffer *fb = state->fb;
996 	const struct dpu_format *fmt =
997 		to_dpu_format(msm_framebuffer_format(fb));
998 
999 	memset(&(pdpu->pipe_cfg), 0, sizeof(struct dpu_hw_pipe_cfg));
1000 
1001 	_dpu_plane_set_scanout(plane, pstate, &pdpu->pipe_cfg, fb);
1002 
1003 	pstate->pending = true;
1004 
1005 	pdpu->is_rt_pipe = (dpu_crtc_get_client_type(crtc) != NRT_CLIENT);
1006 	_dpu_plane_set_qos_ctrl(plane, false, DPU_PLANE_QOS_PANIC_CTRL);
1007 
1008 	DPU_DEBUG_PLANE(pdpu, "FB[%u] " DRM_RECT_FP_FMT "->crtc%u " DRM_RECT_FMT
1009 			", %4.4s ubwc %d\n", fb->base.id, DRM_RECT_FP_ARG(&state->src),
1010 			crtc->base.id, DRM_RECT_ARG(&state->dst),
1011 			(char *)&fmt->base.pixel_format, DPU_FORMAT_IS_UBWC(fmt));
1012 
1013 	pdpu->pipe_cfg.src_rect = state->src;
1014 
1015 	/* state->src is 16.16, src_rect is not */
1016 	pdpu->pipe_cfg.src_rect.x1 >>= 16;
1017 	pdpu->pipe_cfg.src_rect.x2 >>= 16;
1018 	pdpu->pipe_cfg.src_rect.y1 >>= 16;
1019 	pdpu->pipe_cfg.src_rect.y2 >>= 16;
1020 
1021 	pdpu->pipe_cfg.dst_rect = state->dst;
1022 
1023 	_dpu_plane_setup_scaler(pdpu, pstate, fmt, false);
1024 
1025 	/* override for color fill */
1026 	if (pdpu->color_fill & DPU_PLANE_COLOR_FILL_FLAG) {
1027 		/* skip remaining processing on color fill */
1028 		return;
1029 	}
1030 
1031 	if (pdpu->pipe_hw->ops.setup_rects) {
1032 		pdpu->pipe_hw->ops.setup_rects(pdpu->pipe_hw,
1033 				&pdpu->pipe_cfg,
1034 				pstate->multirect_index);
1035 	}
1036 
1037 	if (pdpu->pipe_hw->ops.setup_pe &&
1038 			(pstate->multirect_index != DPU_SSPP_RECT_1))
1039 		pdpu->pipe_hw->ops.setup_pe(pdpu->pipe_hw,
1040 				&pstate->pixel_ext);
1041 
1042 	/**
1043 	 * when programmed in multirect mode, scalar block will be
1044 	 * bypassed. Still we need to update alpha and bitwidth
1045 	 * ONLY for RECT0
1046 	 */
1047 	if (pdpu->pipe_hw->ops.setup_scaler &&
1048 			pstate->multirect_index != DPU_SSPP_RECT_1)
1049 		pdpu->pipe_hw->ops.setup_scaler(pdpu->pipe_hw,
1050 				&pdpu->pipe_cfg, &pstate->pixel_ext,
1051 				&pstate->scaler3_cfg);
1052 
1053 	if (pdpu->pipe_hw->ops.setup_multirect)
1054 		pdpu->pipe_hw->ops.setup_multirect(
1055 				pdpu->pipe_hw,
1056 				pstate->multirect_index,
1057 				pstate->multirect_mode);
1058 
1059 	if (pdpu->pipe_hw->ops.setup_format) {
1060 		src_flags = 0x0;
1061 
1062 		/* update format */
1063 		pdpu->pipe_hw->ops.setup_format(pdpu->pipe_hw, fmt, src_flags,
1064 				pstate->multirect_index);
1065 
1066 		if (pdpu->pipe_hw->ops.setup_cdp) {
1067 			struct dpu_hw_pipe_cdp_cfg *cdp_cfg = &pstate->cdp_cfg;
1068 
1069 			memset(cdp_cfg, 0, sizeof(struct dpu_hw_pipe_cdp_cfg));
1070 
1071 			cdp_cfg->enable = pdpu->catalog->perf.cdp_cfg
1072 					[DPU_PERF_CDP_USAGE_RT].rd_enable;
1073 			cdp_cfg->ubwc_meta_enable =
1074 					DPU_FORMAT_IS_UBWC(fmt);
1075 			cdp_cfg->tile_amortize_enable =
1076 					DPU_FORMAT_IS_UBWC(fmt) ||
1077 					DPU_FORMAT_IS_TILE(fmt);
1078 			cdp_cfg->preload_ahead = DPU_SSPP_CDP_PRELOAD_AHEAD_64;
1079 
1080 			pdpu->pipe_hw->ops.setup_cdp(pdpu->pipe_hw, cdp_cfg);
1081 		}
1082 
1083 		/* update csc */
1084 		if (DPU_FORMAT_IS_YUV(fmt))
1085 			_dpu_plane_setup_csc(pdpu);
1086 		else
1087 			pdpu->csc_ptr = 0;
1088 	}
1089 
1090 	_dpu_plane_set_qos_lut(plane, fb);
1091 	_dpu_plane_set_danger_lut(plane, fb);
1092 
1093 	if (plane->type != DRM_PLANE_TYPE_CURSOR) {
1094 		_dpu_plane_set_qos_ctrl(plane, true, DPU_PLANE_QOS_PANIC_CTRL);
1095 		_dpu_plane_set_ot_limit(plane, crtc);
1096 	}
1097 
1098 	_dpu_plane_set_qos_remap(plane);
1099 }
1100 
1101 static void _dpu_plane_atomic_disable(struct drm_plane *plane)
1102 {
1103 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1104 	struct drm_plane_state *state = plane->state;
1105 	struct dpu_plane_state *pstate = to_dpu_plane_state(state);
1106 
1107 	trace_dpu_plane_disable(DRMID(plane), is_dpu_plane_virtual(plane),
1108 				pstate->multirect_mode);
1109 
1110 	pstate->pending = true;
1111 
1112 	if (is_dpu_plane_virtual(plane) &&
1113 			pdpu->pipe_hw && pdpu->pipe_hw->ops.setup_multirect)
1114 		pdpu->pipe_hw->ops.setup_multirect(pdpu->pipe_hw,
1115 				DPU_SSPP_RECT_SOLO, DPU_SSPP_MULTIRECT_NONE);
1116 }
1117 
1118 static void dpu_plane_atomic_update(struct drm_plane *plane,
1119 				struct drm_plane_state *old_state)
1120 {
1121 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1122 	struct drm_plane_state *state = plane->state;
1123 
1124 	pdpu->is_error = false;
1125 
1126 	DPU_DEBUG_PLANE(pdpu, "\n");
1127 
1128 	if (!state->visible) {
1129 		_dpu_plane_atomic_disable(plane);
1130 	} else {
1131 		dpu_plane_sspp_atomic_update(plane);
1132 	}
1133 }
1134 
1135 void dpu_plane_restore(struct drm_plane *plane)
1136 {
1137 	struct dpu_plane *pdpu;
1138 
1139 	if (!plane || !plane->state) {
1140 		DPU_ERROR("invalid plane\n");
1141 		return;
1142 	}
1143 
1144 	pdpu = to_dpu_plane(plane);
1145 
1146 	DPU_DEBUG_PLANE(pdpu, "\n");
1147 
1148 	/* last plane state is same as current state */
1149 	dpu_plane_atomic_update(plane, plane->state);
1150 }
1151 
1152 static void dpu_plane_destroy(struct drm_plane *plane)
1153 {
1154 	struct dpu_plane *pdpu = plane ? to_dpu_plane(plane) : NULL;
1155 
1156 	DPU_DEBUG_PLANE(pdpu, "\n");
1157 
1158 	if (pdpu) {
1159 		_dpu_plane_set_qos_ctrl(plane, false, DPU_PLANE_QOS_PANIC_CTRL);
1160 
1161 		mutex_destroy(&pdpu->lock);
1162 
1163 		/* this will destroy the states as well */
1164 		drm_plane_cleanup(plane);
1165 
1166 		dpu_hw_sspp_destroy(pdpu->pipe_hw);
1167 
1168 		kfree(pdpu);
1169 	}
1170 }
1171 
1172 static void dpu_plane_destroy_state(struct drm_plane *plane,
1173 		struct drm_plane_state *state)
1174 {
1175 	__drm_atomic_helper_plane_destroy_state(state);
1176 	kfree(to_dpu_plane_state(state));
1177 }
1178 
1179 static struct drm_plane_state *
1180 dpu_plane_duplicate_state(struct drm_plane *plane)
1181 {
1182 	struct dpu_plane *pdpu;
1183 	struct dpu_plane_state *pstate;
1184 	struct dpu_plane_state *old_state;
1185 
1186 	if (!plane) {
1187 		DPU_ERROR("invalid plane\n");
1188 		return NULL;
1189 	} else if (!plane->state) {
1190 		DPU_ERROR("invalid plane state\n");
1191 		return NULL;
1192 	}
1193 
1194 	old_state = to_dpu_plane_state(plane->state);
1195 	pdpu = to_dpu_plane(plane);
1196 	pstate = kmemdup(old_state, sizeof(*old_state), GFP_KERNEL);
1197 	if (!pstate) {
1198 		DPU_ERROR_PLANE(pdpu, "failed to allocate state\n");
1199 		return NULL;
1200 	}
1201 
1202 	DPU_DEBUG_PLANE(pdpu, "\n");
1203 
1204 	pstate->pending = false;
1205 
1206 	__drm_atomic_helper_plane_duplicate_state(plane, &pstate->base);
1207 
1208 	return &pstate->base;
1209 }
1210 
1211 static void dpu_plane_reset(struct drm_plane *plane)
1212 {
1213 	struct dpu_plane *pdpu;
1214 	struct dpu_plane_state *pstate;
1215 
1216 	if (!plane) {
1217 		DPU_ERROR("invalid plane\n");
1218 		return;
1219 	}
1220 
1221 	pdpu = to_dpu_plane(plane);
1222 	DPU_DEBUG_PLANE(pdpu, "\n");
1223 
1224 	/* remove previous state, if present */
1225 	if (plane->state) {
1226 		dpu_plane_destroy_state(plane, plane->state);
1227 		plane->state = 0;
1228 	}
1229 
1230 	pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
1231 	if (!pstate) {
1232 		DPU_ERROR_PLANE(pdpu, "failed to allocate state\n");
1233 		return;
1234 	}
1235 
1236 	pstate->base.plane = plane;
1237 
1238 	plane->state = &pstate->base;
1239 }
1240 
1241 #ifdef CONFIG_DEBUG_FS
1242 static void dpu_plane_danger_signal_ctrl(struct drm_plane *plane, bool enable)
1243 {
1244 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1245 	struct dpu_kms *dpu_kms = _dpu_plane_get_kms(plane);
1246 
1247 	if (!pdpu->is_rt_pipe)
1248 		return;
1249 
1250 	pm_runtime_get_sync(&dpu_kms->pdev->dev);
1251 	_dpu_plane_set_qos_ctrl(plane, enable, DPU_PLANE_QOS_PANIC_CTRL);
1252 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
1253 }
1254 
1255 static ssize_t _dpu_plane_danger_read(struct file *file,
1256 			char __user *buff, size_t count, loff_t *ppos)
1257 {
1258 	struct dpu_kms *kms = file->private_data;
1259 	int len;
1260 	char buf[40];
1261 
1262 	len = scnprintf(buf, sizeof(buf), "%d\n", !kms->has_danger_ctrl);
1263 
1264 	return simple_read_from_buffer(buff, count, ppos, buf, len);
1265 }
1266 
1267 static void _dpu_plane_set_danger_state(struct dpu_kms *kms, bool enable)
1268 {
1269 	struct drm_plane *plane;
1270 
1271 	drm_for_each_plane(plane, kms->dev) {
1272 		if (plane->fb && plane->state) {
1273 			dpu_plane_danger_signal_ctrl(plane, enable);
1274 			DPU_DEBUG("plane:%d img:%dx%d ",
1275 				plane->base.id, plane->fb->width,
1276 				plane->fb->height);
1277 			DPU_DEBUG("src[%d,%d,%d,%d] dst[%d,%d,%d,%d]\n",
1278 				plane->state->src_x >> 16,
1279 				plane->state->src_y >> 16,
1280 				plane->state->src_w >> 16,
1281 				plane->state->src_h >> 16,
1282 				plane->state->crtc_x, plane->state->crtc_y,
1283 				plane->state->crtc_w, plane->state->crtc_h);
1284 		} else {
1285 			DPU_DEBUG("Inactive plane:%d\n", plane->base.id);
1286 		}
1287 	}
1288 }
1289 
1290 static ssize_t _dpu_plane_danger_write(struct file *file,
1291 		    const char __user *user_buf, size_t count, loff_t *ppos)
1292 {
1293 	struct dpu_kms *kms = file->private_data;
1294 	int disable_panic;
1295 	int ret;
1296 
1297 	ret = kstrtouint_from_user(user_buf, count, 0, &disable_panic);
1298 	if (ret)
1299 		return ret;
1300 
1301 	if (disable_panic) {
1302 		/* Disable panic signal for all active pipes */
1303 		DPU_DEBUG("Disabling danger:\n");
1304 		_dpu_plane_set_danger_state(kms, false);
1305 		kms->has_danger_ctrl = false;
1306 	} else {
1307 		/* Enable panic signal for all active pipes */
1308 		DPU_DEBUG("Enabling danger:\n");
1309 		kms->has_danger_ctrl = true;
1310 		_dpu_plane_set_danger_state(kms, true);
1311 	}
1312 
1313 	return count;
1314 }
1315 
1316 static const struct file_operations dpu_plane_danger_enable = {
1317 	.open = simple_open,
1318 	.read = _dpu_plane_danger_read,
1319 	.write = _dpu_plane_danger_write,
1320 };
1321 
1322 static int _dpu_plane_init_debugfs(struct drm_plane *plane)
1323 {
1324 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1325 	struct dpu_kms *kms = _dpu_plane_get_kms(plane);
1326 	const struct dpu_sspp_cfg *cfg = pdpu->pipe_hw->cap;
1327 	const struct dpu_sspp_sub_blks *sblk = cfg->sblk;
1328 
1329 	/* create overall sub-directory for the pipe */
1330 	pdpu->debugfs_root =
1331 		debugfs_create_dir(pdpu->pipe_name,
1332 				plane->dev->primary->debugfs_root);
1333 
1334 	if (!pdpu->debugfs_root)
1335 		return -ENOMEM;
1336 
1337 	/* don't error check these */
1338 	debugfs_create_x32("features", 0600,
1339 			pdpu->debugfs_root, &pdpu->features);
1340 
1341 	/* add register dump support */
1342 	dpu_debugfs_setup_regset32(&pdpu->debugfs_src,
1343 			sblk->src_blk.base + cfg->base,
1344 			sblk->src_blk.len,
1345 			kms);
1346 	dpu_debugfs_create_regset32("src_blk", 0400,
1347 			pdpu->debugfs_root, &pdpu->debugfs_src);
1348 
1349 	if (cfg->features & BIT(DPU_SSPP_SCALER_QSEED3) ||
1350 			cfg->features & BIT(DPU_SSPP_SCALER_QSEED2)) {
1351 		dpu_debugfs_setup_regset32(&pdpu->debugfs_scaler,
1352 				sblk->scaler_blk.base + cfg->base,
1353 				sblk->scaler_blk.len,
1354 				kms);
1355 		dpu_debugfs_create_regset32("scaler_blk", 0400,
1356 				pdpu->debugfs_root,
1357 				&pdpu->debugfs_scaler);
1358 		debugfs_create_bool("default_scaling",
1359 				0600,
1360 				pdpu->debugfs_root,
1361 				&pdpu->debugfs_default_scale);
1362 	}
1363 
1364 	if (cfg->features & BIT(DPU_SSPP_CSC) ||
1365 			cfg->features & BIT(DPU_SSPP_CSC_10BIT)) {
1366 		dpu_debugfs_setup_regset32(&pdpu->debugfs_csc,
1367 				sblk->csc_blk.base + cfg->base,
1368 				sblk->csc_blk.len,
1369 				kms);
1370 		dpu_debugfs_create_regset32("csc_blk", 0400,
1371 				pdpu->debugfs_root, &pdpu->debugfs_csc);
1372 	}
1373 
1374 	debugfs_create_u32("xin_id",
1375 			0400,
1376 			pdpu->debugfs_root,
1377 			(u32 *) &cfg->xin_id);
1378 	debugfs_create_u32("clk_ctrl",
1379 			0400,
1380 			pdpu->debugfs_root,
1381 			(u32 *) &cfg->clk_ctrl);
1382 	debugfs_create_x32("creq_vblank",
1383 			0600,
1384 			pdpu->debugfs_root,
1385 			(u32 *) &sblk->creq_vblank);
1386 	debugfs_create_x32("danger_vblank",
1387 			0600,
1388 			pdpu->debugfs_root,
1389 			(u32 *) &sblk->danger_vblank);
1390 
1391 	debugfs_create_file("disable_danger",
1392 			0600,
1393 			pdpu->debugfs_root,
1394 			kms, &dpu_plane_danger_enable);
1395 
1396 	return 0;
1397 }
1398 #else
1399 static int _dpu_plane_init_debugfs(struct drm_plane *plane)
1400 {
1401 	return 0;
1402 }
1403 #endif
1404 
1405 static int dpu_plane_late_register(struct drm_plane *plane)
1406 {
1407 	return _dpu_plane_init_debugfs(plane);
1408 }
1409 
1410 static void dpu_plane_early_unregister(struct drm_plane *plane)
1411 {
1412 	struct dpu_plane *pdpu = to_dpu_plane(plane);
1413 
1414 	debugfs_remove_recursive(pdpu->debugfs_root);
1415 }
1416 
1417 static bool dpu_plane_format_mod_supported(struct drm_plane *plane,
1418 		uint32_t format, uint64_t modifier)
1419 {
1420 	if (modifier == DRM_FORMAT_MOD_LINEAR)
1421 		return true;
1422 
1423 	if (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) {
1424 		int i;
1425 		for (i = 0; i < ARRAY_SIZE(qcom_compressed_supported_formats); i++) {
1426 			if (format == qcom_compressed_supported_formats[i])
1427 				return true;
1428 		}
1429 	}
1430 
1431 	return false;
1432 }
1433 
1434 static const struct drm_plane_funcs dpu_plane_funcs = {
1435 		.update_plane = drm_atomic_helper_update_plane,
1436 		.disable_plane = drm_atomic_helper_disable_plane,
1437 		.destroy = dpu_plane_destroy,
1438 		.reset = dpu_plane_reset,
1439 		.atomic_duplicate_state = dpu_plane_duplicate_state,
1440 		.atomic_destroy_state = dpu_plane_destroy_state,
1441 		.late_register = dpu_plane_late_register,
1442 		.early_unregister = dpu_plane_early_unregister,
1443 		.format_mod_supported = dpu_plane_format_mod_supported,
1444 };
1445 
1446 static const struct drm_plane_helper_funcs dpu_plane_helper_funcs = {
1447 		.prepare_fb = dpu_plane_prepare_fb,
1448 		.cleanup_fb = dpu_plane_cleanup_fb,
1449 		.atomic_check = dpu_plane_atomic_check,
1450 		.atomic_update = dpu_plane_atomic_update,
1451 };
1452 
1453 enum dpu_sspp dpu_plane_pipe(struct drm_plane *plane)
1454 {
1455 	return plane ? to_dpu_plane(plane)->pipe : SSPP_NONE;
1456 }
1457 
1458 bool is_dpu_plane_virtual(struct drm_plane *plane)
1459 {
1460 	return plane ? to_dpu_plane(plane)->is_virtual : false;
1461 }
1462 
1463 /* initialize plane */
1464 struct drm_plane *dpu_plane_init(struct drm_device *dev,
1465 		uint32_t pipe, enum drm_plane_type type,
1466 		unsigned long possible_crtcs, u32 master_plane_id)
1467 {
1468 	struct drm_plane *plane = NULL, *master_plane = NULL;
1469 	const uint32_t *format_list;
1470 	struct dpu_plane *pdpu;
1471 	struct msm_drm_private *priv = dev->dev_private;
1472 	struct dpu_kms *kms = to_dpu_kms(priv->kms);
1473 	int zpos_max = DPU_ZPOS_MAX;
1474 	uint32_t num_formats;
1475 	int ret = -EINVAL;
1476 
1477 	/* create and zero local structure */
1478 	pdpu = kzalloc(sizeof(*pdpu), GFP_KERNEL);
1479 	if (!pdpu) {
1480 		DPU_ERROR("[%u]failed to allocate local plane struct\n", pipe);
1481 		ret = -ENOMEM;
1482 		return ERR_PTR(ret);
1483 	}
1484 
1485 	/* cache local stuff for later */
1486 	plane = &pdpu->base;
1487 	pdpu->pipe = pipe;
1488 	pdpu->is_virtual = (master_plane_id != 0);
1489 	INIT_LIST_HEAD(&pdpu->mplane_list);
1490 	master_plane = drm_plane_find(dev, NULL, master_plane_id);
1491 	if (master_plane) {
1492 		struct dpu_plane *mpdpu = to_dpu_plane(master_plane);
1493 
1494 		list_add_tail(&pdpu->mplane_list, &mpdpu->mplane_list);
1495 	}
1496 
1497 	/* initialize underlying h/w driver */
1498 	pdpu->pipe_hw = dpu_hw_sspp_init(pipe, kms->mmio, kms->catalog,
1499 							master_plane_id != 0);
1500 	if (IS_ERR(pdpu->pipe_hw)) {
1501 		DPU_ERROR("[%u]SSPP init failed\n", pipe);
1502 		ret = PTR_ERR(pdpu->pipe_hw);
1503 		goto clean_plane;
1504 	} else if (!pdpu->pipe_hw->cap || !pdpu->pipe_hw->cap->sblk) {
1505 		DPU_ERROR("[%u]SSPP init returned invalid cfg\n", pipe);
1506 		goto clean_sspp;
1507 	}
1508 
1509 	/* cache features mask for later */
1510 	pdpu->features = pdpu->pipe_hw->cap->features;
1511 	pdpu->pipe_sblk = pdpu->pipe_hw->cap->sblk;
1512 	if (!pdpu->pipe_sblk) {
1513 		DPU_ERROR("[%u]invalid sblk\n", pipe);
1514 		goto clean_sspp;
1515 	}
1516 
1517 	if (pdpu->is_virtual) {
1518 		format_list = pdpu->pipe_sblk->virt_format_list;
1519 		num_formats = pdpu->pipe_sblk->virt_num_formats;
1520 	}
1521 	else {
1522 		format_list = pdpu->pipe_sblk->format_list;
1523 		num_formats = pdpu->pipe_sblk->num_formats;
1524 	}
1525 
1526 	ret = drm_universal_plane_init(dev, plane, 0xff, &dpu_plane_funcs,
1527 				format_list, num_formats,
1528 				supported_format_modifiers, type, NULL);
1529 	if (ret)
1530 		goto clean_sspp;
1531 
1532 	pdpu->catalog = kms->catalog;
1533 
1534 	if (kms->catalog->mixer_count &&
1535 		kms->catalog->mixer[0].sblk->maxblendstages) {
1536 		zpos_max = kms->catalog->mixer[0].sblk->maxblendstages - 1;
1537 		if (zpos_max > DPU_STAGE_MAX - DPU_STAGE_0 - 1)
1538 			zpos_max = DPU_STAGE_MAX - DPU_STAGE_0 - 1;
1539 	}
1540 
1541 	ret = drm_plane_create_zpos_property(plane, 0, 0, zpos_max);
1542 	if (ret)
1543 		DPU_ERROR("failed to install zpos property, rc = %d\n", ret);
1544 
1545 	/* success! finalize initialization */
1546 	drm_plane_helper_add(plane, &dpu_plane_helper_funcs);
1547 
1548 	/* save user friendly pipe name for later */
1549 	snprintf(pdpu->pipe_name, DPU_NAME_SIZE, "plane%u", plane->base.id);
1550 
1551 	mutex_init(&pdpu->lock);
1552 
1553 	DPU_DEBUG("%s created for pipe:%u id:%u virtual:%u\n", pdpu->pipe_name,
1554 					pipe, plane->base.id, master_plane_id);
1555 	return plane;
1556 
1557 clean_sspp:
1558 	if (pdpu && pdpu->pipe_hw)
1559 		dpu_hw_sspp_destroy(pdpu->pipe_hw);
1560 clean_plane:
1561 	kfree(pdpu);
1562 	return ERR_PTR(ret);
1563 }
1564