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