xref: /openbmc/linux/drivers/gpu/drm/amd/display/dc/core/dc_resource.c (revision 8ebc80a25f9d9bf7a8e368b266d5b740c485c362)
1 /*
2  * Copyright 2012-15 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include "dm_services.h"
27 
28 #include "resource.h"
29 #include "include/irq_service_interface.h"
30 #include "link_encoder.h"
31 #include "stream_encoder.h"
32 #include "opp.h"
33 #include "timing_generator.h"
34 #include "transform.h"
35 #include "dccg.h"
36 #include "dchubbub.h"
37 #include "dpp.h"
38 #include "core_types.h"
39 #include "set_mode_types.h"
40 #include "virtual/virtual_stream_encoder.h"
41 #include "dpcd_defs.h"
42 #include "link_enc_cfg.h"
43 #include "link.h"
44 #include "virtual/virtual_link_hwss.h"
45 #include "link/hwss/link_hwss_dio.h"
46 #include "link/hwss/link_hwss_dpia.h"
47 #include "link/hwss/link_hwss_hpo_dp.h"
48 #include "link/hwss/link_hwss_dio_fixed_vs_pe_retimer.h"
49 #include "link/hwss/link_hwss_hpo_fixed_vs_pe_retimer_dp.h"
50 
51 #if defined(CONFIG_DRM_AMD_DC_SI)
52 #include "dce60/dce60_resource.h"
53 #endif
54 #include "dce80/dce80_resource.h"
55 #include "dce100/dce100_resource.h"
56 #include "dce110/dce110_resource.h"
57 #include "dce112/dce112_resource.h"
58 #include "dce120/dce120_resource.h"
59 #include "dcn10/dcn10_resource.h"
60 #include "dcn20/dcn20_resource.h"
61 #include "dcn21/dcn21_resource.h"
62 #include "dcn201/dcn201_resource.h"
63 #include "dcn30/dcn30_resource.h"
64 #include "dcn301/dcn301_resource.h"
65 #include "dcn302/dcn302_resource.h"
66 #include "dcn303/dcn303_resource.h"
67 #include "dcn31/dcn31_resource.h"
68 #include "dcn314/dcn314_resource.h"
69 #include "dcn315/dcn315_resource.h"
70 #include "dcn316/dcn316_resource.h"
71 #include "../dcn32/dcn32_resource.h"
72 #include "../dcn321/dcn321_resource.h"
73 
74 #define VISUAL_CONFIRM_BASE_DEFAULT 3
75 #define VISUAL_CONFIRM_BASE_MIN 1
76 #define VISUAL_CONFIRM_BASE_MAX 10
77 /* we choose 240 because it is a common denominator of common v addressable
78  * such as 2160, 1440, 1200, 960. So we take 1/240 portion of v addressable as
79  * the visual confirm dpp offset height. So visual confirm height can stay
80  * relatively the same independent from timing used.
81  */
82 #define VISUAL_CONFIRM_DPP_OFFSET_DENO 240
83 
84 #define DC_LOGGER_INIT(logger)
85 
86 #define UNABLE_TO_SPLIT -1
87 
resource_parse_asic_id(struct hw_asic_id asic_id)88 enum dce_version resource_parse_asic_id(struct hw_asic_id asic_id)
89 {
90 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
91 
92 	switch (asic_id.chip_family) {
93 
94 #if defined(CONFIG_DRM_AMD_DC_SI)
95 	case FAMILY_SI:
96 		if (ASIC_REV_IS_TAHITI_P(asic_id.hw_internal_rev) ||
97 		    ASIC_REV_IS_PITCAIRN_PM(asic_id.hw_internal_rev) ||
98 		    ASIC_REV_IS_CAPEVERDE_M(asic_id.hw_internal_rev))
99 			dc_version = DCE_VERSION_6_0;
100 		else if (ASIC_REV_IS_OLAND_M(asic_id.hw_internal_rev))
101 			dc_version = DCE_VERSION_6_4;
102 		else
103 			dc_version = DCE_VERSION_6_1;
104 		break;
105 #endif
106 	case FAMILY_CI:
107 		dc_version = DCE_VERSION_8_0;
108 		break;
109 	case FAMILY_KV:
110 		if (ASIC_REV_IS_KALINDI(asic_id.hw_internal_rev) ||
111 		    ASIC_REV_IS_BHAVANI(asic_id.hw_internal_rev) ||
112 		    ASIC_REV_IS_GODAVARI(asic_id.hw_internal_rev))
113 			dc_version = DCE_VERSION_8_3;
114 		else
115 			dc_version = DCE_VERSION_8_1;
116 		break;
117 	case FAMILY_CZ:
118 		dc_version = DCE_VERSION_11_0;
119 		break;
120 
121 	case FAMILY_VI:
122 		if (ASIC_REV_IS_TONGA_P(asic_id.hw_internal_rev) ||
123 				ASIC_REV_IS_FIJI_P(asic_id.hw_internal_rev)) {
124 			dc_version = DCE_VERSION_10_0;
125 			break;
126 		}
127 		if (ASIC_REV_IS_POLARIS10_P(asic_id.hw_internal_rev) ||
128 				ASIC_REV_IS_POLARIS11_M(asic_id.hw_internal_rev) ||
129 				ASIC_REV_IS_POLARIS12_V(asic_id.hw_internal_rev)) {
130 			dc_version = DCE_VERSION_11_2;
131 		}
132 		if (ASIC_REV_IS_VEGAM(asic_id.hw_internal_rev))
133 			dc_version = DCE_VERSION_11_22;
134 		break;
135 	case FAMILY_AI:
136 		if (ASICREV_IS_VEGA20_P(asic_id.hw_internal_rev))
137 			dc_version = DCE_VERSION_12_1;
138 		else
139 			dc_version = DCE_VERSION_12_0;
140 		break;
141 	case FAMILY_RV:
142 		dc_version = DCN_VERSION_1_0;
143 		if (ASICREV_IS_RAVEN2(asic_id.hw_internal_rev))
144 			dc_version = DCN_VERSION_1_01;
145 		if (ASICREV_IS_RENOIR(asic_id.hw_internal_rev))
146 			dc_version = DCN_VERSION_2_1;
147 		if (ASICREV_IS_GREEN_SARDINE(asic_id.hw_internal_rev))
148 			dc_version = DCN_VERSION_2_1;
149 		break;
150 
151 	case FAMILY_NV:
152 		dc_version = DCN_VERSION_2_0;
153 		if (asic_id.chip_id == DEVICE_ID_NV_13FE || asic_id.chip_id == DEVICE_ID_NV_143F) {
154 			dc_version = DCN_VERSION_2_01;
155 			break;
156 		}
157 		if (ASICREV_IS_SIENNA_CICHLID_P(asic_id.hw_internal_rev))
158 			dc_version = DCN_VERSION_3_0;
159 		if (ASICREV_IS_DIMGREY_CAVEFISH_P(asic_id.hw_internal_rev))
160 			dc_version = DCN_VERSION_3_02;
161 		if (ASICREV_IS_BEIGE_GOBY_P(asic_id.hw_internal_rev))
162 			dc_version = DCN_VERSION_3_03;
163 		break;
164 
165 	case FAMILY_VGH:
166 		dc_version = DCN_VERSION_3_01;
167 		break;
168 
169 	case FAMILY_YELLOW_CARP:
170 		if (ASICREV_IS_YELLOW_CARP(asic_id.hw_internal_rev))
171 			dc_version = DCN_VERSION_3_1;
172 		break;
173 	case AMDGPU_FAMILY_GC_10_3_6:
174 		if (ASICREV_IS_GC_10_3_6(asic_id.hw_internal_rev))
175 			dc_version = DCN_VERSION_3_15;
176 		break;
177 	case AMDGPU_FAMILY_GC_10_3_7:
178 		if (ASICREV_IS_GC_10_3_7(asic_id.hw_internal_rev))
179 			dc_version = DCN_VERSION_3_16;
180 		break;
181 	case AMDGPU_FAMILY_GC_11_0_0:
182 		dc_version = DCN_VERSION_3_2;
183 		if (ASICREV_IS_GC_11_0_2(asic_id.hw_internal_rev))
184 			dc_version = DCN_VERSION_3_21;
185 		break;
186 	case AMDGPU_FAMILY_GC_11_0_1:
187 		dc_version = DCN_VERSION_3_14;
188 		break;
189 	default:
190 		dc_version = DCE_VERSION_UNKNOWN;
191 		break;
192 	}
193 	return dc_version;
194 }
195 
dc_create_resource_pool(struct dc * dc,const struct dc_init_data * init_data,enum dce_version dc_version)196 struct resource_pool *dc_create_resource_pool(struct dc  *dc,
197 					      const struct dc_init_data *init_data,
198 					      enum dce_version dc_version)
199 {
200 	struct resource_pool *res_pool = NULL;
201 
202 	switch (dc_version) {
203 #if defined(CONFIG_DRM_AMD_DC_SI)
204 	case DCE_VERSION_6_0:
205 		res_pool = dce60_create_resource_pool(
206 			init_data->num_virtual_links, dc);
207 		break;
208 	case DCE_VERSION_6_1:
209 		res_pool = dce61_create_resource_pool(
210 			init_data->num_virtual_links, dc);
211 		break;
212 	case DCE_VERSION_6_4:
213 		res_pool = dce64_create_resource_pool(
214 			init_data->num_virtual_links, dc);
215 		break;
216 #endif
217 	case DCE_VERSION_8_0:
218 		res_pool = dce80_create_resource_pool(
219 				init_data->num_virtual_links, dc);
220 		break;
221 	case DCE_VERSION_8_1:
222 		res_pool = dce81_create_resource_pool(
223 				init_data->num_virtual_links, dc);
224 		break;
225 	case DCE_VERSION_8_3:
226 		res_pool = dce83_create_resource_pool(
227 				init_data->num_virtual_links, dc);
228 		break;
229 	case DCE_VERSION_10_0:
230 		res_pool = dce100_create_resource_pool(
231 				init_data->num_virtual_links, dc);
232 		break;
233 	case DCE_VERSION_11_0:
234 		res_pool = dce110_create_resource_pool(
235 				init_data->num_virtual_links, dc,
236 				init_data->asic_id);
237 		break;
238 	case DCE_VERSION_11_2:
239 	case DCE_VERSION_11_22:
240 		res_pool = dce112_create_resource_pool(
241 				init_data->num_virtual_links, dc);
242 		break;
243 	case DCE_VERSION_12_0:
244 	case DCE_VERSION_12_1:
245 		res_pool = dce120_create_resource_pool(
246 				init_data->num_virtual_links, dc);
247 		break;
248 
249 #if defined(CONFIG_DRM_AMD_DC_FP)
250 	case DCN_VERSION_1_0:
251 	case DCN_VERSION_1_01:
252 		res_pool = dcn10_create_resource_pool(init_data, dc);
253 		break;
254 	case DCN_VERSION_2_0:
255 		res_pool = dcn20_create_resource_pool(init_data, dc);
256 		break;
257 	case DCN_VERSION_2_1:
258 		res_pool = dcn21_create_resource_pool(init_data, dc);
259 		break;
260 	case DCN_VERSION_2_01:
261 		res_pool = dcn201_create_resource_pool(init_data, dc);
262 		break;
263 	case DCN_VERSION_3_0:
264 		res_pool = dcn30_create_resource_pool(init_data, dc);
265 		break;
266 	case DCN_VERSION_3_01:
267 		res_pool = dcn301_create_resource_pool(init_data, dc);
268 		break;
269 	case DCN_VERSION_3_02:
270 		res_pool = dcn302_create_resource_pool(init_data, dc);
271 		break;
272 	case DCN_VERSION_3_03:
273 		res_pool = dcn303_create_resource_pool(init_data, dc);
274 		break;
275 	case DCN_VERSION_3_1:
276 		res_pool = dcn31_create_resource_pool(init_data, dc);
277 		break;
278 	case DCN_VERSION_3_14:
279 		res_pool = dcn314_create_resource_pool(init_data, dc);
280 		break;
281 	case DCN_VERSION_3_15:
282 		res_pool = dcn315_create_resource_pool(init_data, dc);
283 		break;
284 	case DCN_VERSION_3_16:
285 		res_pool = dcn316_create_resource_pool(init_data, dc);
286 		break;
287 	case DCN_VERSION_3_2:
288 		res_pool = dcn32_create_resource_pool(init_data, dc);
289 		break;
290 	case DCN_VERSION_3_21:
291 		res_pool = dcn321_create_resource_pool(init_data, dc);
292 		break;
293 #endif /* CONFIG_DRM_AMD_DC_FP */
294 	default:
295 		break;
296 	}
297 
298 	if (res_pool != NULL) {
299 		if (dc->ctx->dc_bios->fw_info_valid) {
300 			res_pool->ref_clocks.xtalin_clock_inKhz =
301 				dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency;
302 			/* initialize with firmware data first, no all
303 			 * ASIC have DCCG SW component. FPGA or
304 			 * simulation need initialization of
305 			 * dccg_ref_clock_inKhz, dchub_ref_clock_inKhz
306 			 * with xtalin_clock_inKhz
307 			 */
308 			res_pool->ref_clocks.dccg_ref_clock_inKhz =
309 				res_pool->ref_clocks.xtalin_clock_inKhz;
310 			res_pool->ref_clocks.dchub_ref_clock_inKhz =
311 				res_pool->ref_clocks.xtalin_clock_inKhz;
312 		} else
313 			ASSERT_CRITICAL(false);
314 	}
315 
316 	return res_pool;
317 }
318 
dc_destroy_resource_pool(struct dc * dc)319 void dc_destroy_resource_pool(struct dc  *dc)
320 {
321 	if (dc) {
322 		if (dc->res_pool)
323 			dc->res_pool->funcs->destroy(&dc->res_pool);
324 
325 		kfree(dc->hwseq);
326 	}
327 }
328 
update_num_audio(const struct resource_straps * straps,unsigned int * num_audio,struct audio_support * aud_support)329 static void update_num_audio(
330 	const struct resource_straps *straps,
331 	unsigned int *num_audio,
332 	struct audio_support *aud_support)
333 {
334 	aud_support->dp_audio = true;
335 	aud_support->hdmi_audio_native = false;
336 	aud_support->hdmi_audio_on_dongle = false;
337 
338 	if (straps->hdmi_disable == 0) {
339 		if (straps->dc_pinstraps_audio & 0x2) {
340 			aud_support->hdmi_audio_on_dongle = true;
341 			aud_support->hdmi_audio_native = true;
342 		}
343 	}
344 
345 	switch (straps->audio_stream_number) {
346 	case 0: /* multi streams supported */
347 		break;
348 	case 1: /* multi streams not supported */
349 		*num_audio = 1;
350 		break;
351 	default:
352 		DC_ERR("DC: unexpected audio fuse!\n");
353 	}
354 }
355 
resource_construct(unsigned int num_virtual_links,struct dc * dc,struct resource_pool * pool,const struct resource_create_funcs * create_funcs)356 bool resource_construct(
357 	unsigned int num_virtual_links,
358 	struct dc  *dc,
359 	struct resource_pool *pool,
360 	const struct resource_create_funcs *create_funcs)
361 {
362 	struct dc_context *ctx = dc->ctx;
363 	const struct resource_caps *caps = pool->res_cap;
364 	int i;
365 	unsigned int num_audio = caps->num_audio;
366 	struct resource_straps straps = {0};
367 
368 	if (create_funcs->read_dce_straps)
369 		create_funcs->read_dce_straps(dc->ctx, &straps);
370 
371 	pool->audio_count = 0;
372 	if (create_funcs->create_audio) {
373 		/* find the total number of streams available via the
374 		 * AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT
375 		 * registers (one for each pin) starting from pin 1
376 		 * up to the max number of audio pins.
377 		 * We stop on the first pin where
378 		 * PORT_CONNECTIVITY == 1 (as instructed by HW team).
379 		 */
380 		update_num_audio(&straps, &num_audio, &pool->audio_support);
381 		for (i = 0; i < caps->num_audio; i++) {
382 			struct audio *aud = create_funcs->create_audio(ctx, i);
383 
384 			if (aud == NULL) {
385 				DC_ERR("DC: failed to create audio!\n");
386 				return false;
387 			}
388 			if (!aud->funcs->endpoint_valid(aud)) {
389 				aud->funcs->destroy(&aud);
390 				break;
391 			}
392 			pool->audios[i] = aud;
393 			pool->audio_count++;
394 		}
395 	}
396 
397 	pool->stream_enc_count = 0;
398 	if (create_funcs->create_stream_encoder) {
399 		for (i = 0; i < caps->num_stream_encoder; i++) {
400 			pool->stream_enc[i] = create_funcs->create_stream_encoder(i, ctx);
401 			if (pool->stream_enc[i] == NULL)
402 				DC_ERR("DC: failed to create stream_encoder!\n");
403 			pool->stream_enc_count++;
404 		}
405 	}
406 
407 	pool->hpo_dp_stream_enc_count = 0;
408 	if (create_funcs->create_hpo_dp_stream_encoder) {
409 		for (i = 0; i < caps->num_hpo_dp_stream_encoder; i++) {
410 			pool->hpo_dp_stream_enc[i] = create_funcs->create_hpo_dp_stream_encoder(i+ENGINE_ID_HPO_DP_0, ctx);
411 			if (pool->hpo_dp_stream_enc[i] == NULL)
412 				DC_ERR("DC: failed to create HPO DP stream encoder!\n");
413 			pool->hpo_dp_stream_enc_count++;
414 
415 		}
416 	}
417 
418 	pool->hpo_dp_link_enc_count = 0;
419 	if (create_funcs->create_hpo_dp_link_encoder) {
420 		for (i = 0; i < caps->num_hpo_dp_link_encoder; i++) {
421 			pool->hpo_dp_link_enc[i] = create_funcs->create_hpo_dp_link_encoder(i, ctx);
422 			if (pool->hpo_dp_link_enc[i] == NULL)
423 				DC_ERR("DC: failed to create HPO DP link encoder!\n");
424 			pool->hpo_dp_link_enc_count++;
425 		}
426 	}
427 
428 	for (i = 0; i < caps->num_mpc_3dlut; i++) {
429 		pool->mpc_lut[i] = dc_create_3dlut_func();
430 		if (pool->mpc_lut[i] == NULL)
431 			DC_ERR("DC: failed to create MPC 3dlut!\n");
432 		pool->mpc_shaper[i] = dc_create_transfer_func();
433 		if (pool->mpc_shaper[i] == NULL)
434 			DC_ERR("DC: failed to create MPC shaper!\n");
435 	}
436 
437 	dc->caps.dynamic_audio = false;
438 	if (pool->audio_count < pool->stream_enc_count) {
439 		dc->caps.dynamic_audio = true;
440 	}
441 	for (i = 0; i < num_virtual_links; i++) {
442 		pool->stream_enc[pool->stream_enc_count] =
443 			virtual_stream_encoder_create(
444 					ctx, ctx->dc_bios);
445 		if (pool->stream_enc[pool->stream_enc_count] == NULL) {
446 			DC_ERR("DC: failed to create stream_encoder!\n");
447 			return false;
448 		}
449 		pool->stream_enc_count++;
450 	}
451 
452 	dc->hwseq = create_funcs->create_hwseq(ctx);
453 
454 	return true;
455 }
find_matching_clock_source(const struct resource_pool * pool,struct clock_source * clock_source)456 static int find_matching_clock_source(
457 		const struct resource_pool *pool,
458 		struct clock_source *clock_source)
459 {
460 
461 	int i;
462 
463 	for (i = 0; i < pool->clk_src_count; i++) {
464 		if (pool->clock_sources[i] == clock_source)
465 			return i;
466 	}
467 	return -1;
468 }
469 
resource_unreference_clock_source(struct resource_context * res_ctx,const struct resource_pool * pool,struct clock_source * clock_source)470 void resource_unreference_clock_source(
471 		struct resource_context *res_ctx,
472 		const struct resource_pool *pool,
473 		struct clock_source *clock_source)
474 {
475 	int i = find_matching_clock_source(pool, clock_source);
476 
477 	if (i > -1)
478 		res_ctx->clock_source_ref_count[i]--;
479 
480 	if (pool->dp_clock_source == clock_source)
481 		res_ctx->dp_clock_source_ref_count--;
482 }
483 
resource_reference_clock_source(struct resource_context * res_ctx,const struct resource_pool * pool,struct clock_source * clock_source)484 void resource_reference_clock_source(
485 		struct resource_context *res_ctx,
486 		const struct resource_pool *pool,
487 		struct clock_source *clock_source)
488 {
489 	int i = find_matching_clock_source(pool, clock_source);
490 
491 	if (i > -1)
492 		res_ctx->clock_source_ref_count[i]++;
493 
494 	if (pool->dp_clock_source == clock_source)
495 		res_ctx->dp_clock_source_ref_count++;
496 }
497 
resource_get_clock_source_reference(struct resource_context * res_ctx,const struct resource_pool * pool,struct clock_source * clock_source)498 int resource_get_clock_source_reference(
499 		struct resource_context *res_ctx,
500 		const struct resource_pool *pool,
501 		struct clock_source *clock_source)
502 {
503 	int i = find_matching_clock_source(pool, clock_source);
504 
505 	if (i > -1)
506 		return res_ctx->clock_source_ref_count[i];
507 
508 	if (pool->dp_clock_source == clock_source)
509 		return res_ctx->dp_clock_source_ref_count;
510 
511 	return -1;
512 }
513 
resource_are_vblanks_synchronizable(struct dc_stream_state * stream1,struct dc_stream_state * stream2)514 bool resource_are_vblanks_synchronizable(
515 	struct dc_stream_state *stream1,
516 	struct dc_stream_state *stream2)
517 {
518 	uint32_t base60_refresh_rates[] = {10, 20, 5};
519 	uint8_t i;
520 	uint8_t rr_count = ARRAY_SIZE(base60_refresh_rates);
521 	uint64_t frame_time_diff;
522 
523 	if (stream1->ctx->dc->config.vblank_alignment_dto_params &&
524 		stream1->ctx->dc->config.vblank_alignment_max_frame_time_diff > 0 &&
525 		dc_is_dp_signal(stream1->signal) &&
526 		dc_is_dp_signal(stream2->signal) &&
527 		false == stream1->has_non_synchronizable_pclk &&
528 		false == stream2->has_non_synchronizable_pclk &&
529 		stream1->timing.flags.VBLANK_SYNCHRONIZABLE &&
530 		stream2->timing.flags.VBLANK_SYNCHRONIZABLE) {
531 		/* disable refresh rates higher than 60Hz for now */
532 		if (stream1->timing.pix_clk_100hz*100/stream1->timing.h_total/
533 				stream1->timing.v_total > 60)
534 			return false;
535 		if (stream2->timing.pix_clk_100hz*100/stream2->timing.h_total/
536 				stream2->timing.v_total > 60)
537 			return false;
538 		frame_time_diff = (uint64_t)10000 *
539 			stream1->timing.h_total *
540 			stream1->timing.v_total *
541 			stream2->timing.pix_clk_100hz;
542 		frame_time_diff = div_u64(frame_time_diff, stream1->timing.pix_clk_100hz);
543 		frame_time_diff = div_u64(frame_time_diff, stream2->timing.h_total);
544 		frame_time_diff = div_u64(frame_time_diff, stream2->timing.v_total);
545 		for (i = 0; i < rr_count; i++) {
546 			int64_t diff = (int64_t)div_u64(frame_time_diff * base60_refresh_rates[i], 10) - 10000;
547 
548 			if (diff < 0)
549 				diff = -diff;
550 			if (diff < stream1->ctx->dc->config.vblank_alignment_max_frame_time_diff)
551 				return true;
552 		}
553 	}
554 	return false;
555 }
556 
resource_are_streams_timing_synchronizable(struct dc_stream_state * stream1,struct dc_stream_state * stream2)557 bool resource_are_streams_timing_synchronizable(
558 	struct dc_stream_state *stream1,
559 	struct dc_stream_state *stream2)
560 {
561 	if (stream1->timing.h_total != stream2->timing.h_total)
562 		return false;
563 
564 	if (stream1->timing.v_total != stream2->timing.v_total)
565 		return false;
566 
567 	if (stream1->timing.h_addressable
568 				!= stream2->timing.h_addressable)
569 		return false;
570 
571 	if (stream1->timing.v_addressable
572 				!= stream2->timing.v_addressable)
573 		return false;
574 
575 	if (stream1->timing.v_front_porch
576 				!= stream2->timing.v_front_porch)
577 		return false;
578 
579 	if (stream1->timing.pix_clk_100hz
580 				!= stream2->timing.pix_clk_100hz)
581 		return false;
582 
583 	if (stream1->clamping.c_depth != stream2->clamping.c_depth)
584 		return false;
585 
586 	if (stream1->phy_pix_clk != stream2->phy_pix_clk
587 			&& (!dc_is_dp_signal(stream1->signal)
588 			|| !dc_is_dp_signal(stream2->signal)))
589 		return false;
590 
591 	if (stream1->view_format != stream2->view_format)
592 		return false;
593 
594 	if (stream1->ignore_msa_timing_param || stream2->ignore_msa_timing_param)
595 		return false;
596 
597 	return true;
598 }
is_dp_and_hdmi_sharable(struct dc_stream_state * stream1,struct dc_stream_state * stream2)599 static bool is_dp_and_hdmi_sharable(
600 		struct dc_stream_state *stream1,
601 		struct dc_stream_state *stream2)
602 {
603 	if (stream1->ctx->dc->caps.disable_dp_clk_share)
604 		return false;
605 
606 	if (stream1->clamping.c_depth != COLOR_DEPTH_888 ||
607 		stream2->clamping.c_depth != COLOR_DEPTH_888)
608 		return false;
609 
610 	return true;
611 
612 }
613 
is_sharable_clk_src(const struct pipe_ctx * pipe_with_clk_src,const struct pipe_ctx * pipe)614 static bool is_sharable_clk_src(
615 	const struct pipe_ctx *pipe_with_clk_src,
616 	const struct pipe_ctx *pipe)
617 {
618 	if (pipe_with_clk_src->clock_source == NULL)
619 		return false;
620 
621 	if (pipe_with_clk_src->stream->signal == SIGNAL_TYPE_VIRTUAL)
622 		return false;
623 
624 	if (dc_is_dp_signal(pipe_with_clk_src->stream->signal) ||
625 		(dc_is_dp_signal(pipe->stream->signal) &&
626 		!is_dp_and_hdmi_sharable(pipe_with_clk_src->stream,
627 				     pipe->stream)))
628 		return false;
629 
630 	if (dc_is_hdmi_signal(pipe_with_clk_src->stream->signal)
631 			&& dc_is_dual_link_signal(pipe->stream->signal))
632 		return false;
633 
634 	if (dc_is_hdmi_signal(pipe->stream->signal)
635 			&& dc_is_dual_link_signal(pipe_with_clk_src->stream->signal))
636 		return false;
637 
638 	if (!resource_are_streams_timing_synchronizable(
639 			pipe_with_clk_src->stream, pipe->stream))
640 		return false;
641 
642 	return true;
643 }
644 
resource_find_used_clk_src_for_sharing(struct resource_context * res_ctx,struct pipe_ctx * pipe_ctx)645 struct clock_source *resource_find_used_clk_src_for_sharing(
646 					struct resource_context *res_ctx,
647 					struct pipe_ctx *pipe_ctx)
648 {
649 	int i;
650 
651 	for (i = 0; i < MAX_PIPES; i++) {
652 		if (is_sharable_clk_src(&res_ctx->pipe_ctx[i], pipe_ctx))
653 			return res_ctx->pipe_ctx[i].clock_source;
654 	}
655 
656 	return NULL;
657 }
658 
convert_pixel_format_to_dalsurface(enum surface_pixel_format surface_pixel_format)659 static enum pixel_format convert_pixel_format_to_dalsurface(
660 		enum surface_pixel_format surface_pixel_format)
661 {
662 	enum pixel_format dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
663 
664 	switch (surface_pixel_format) {
665 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
666 		dal_pixel_format = PIXEL_FORMAT_INDEX8;
667 		break;
668 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
669 		dal_pixel_format = PIXEL_FORMAT_RGB565;
670 		break;
671 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
672 		dal_pixel_format = PIXEL_FORMAT_RGB565;
673 		break;
674 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
675 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
676 		break;
677 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
678 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
679 		break;
680 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
681 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
682 		break;
683 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
684 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
685 		break;
686 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
687 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010_XRBIAS;
688 		break;
689 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
690 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
691 		dal_pixel_format = PIXEL_FORMAT_FP16;
692 		break;
693 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
694 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
695 		dal_pixel_format = PIXEL_FORMAT_420BPP8;
696 		break;
697 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
698 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
699 		dal_pixel_format = PIXEL_FORMAT_420BPP10;
700 		break;
701 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
702 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
703 	default:
704 		dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
705 		break;
706 	}
707 	return dal_pixel_format;
708 }
709 
get_vp_scan_direction(enum dc_rotation_angle rotation,bool horizontal_mirror,bool * orthogonal_rotation,bool * flip_vert_scan_dir,bool * flip_horz_scan_dir)710 static inline void get_vp_scan_direction(
711 	enum dc_rotation_angle rotation,
712 	bool horizontal_mirror,
713 	bool *orthogonal_rotation,
714 	bool *flip_vert_scan_dir,
715 	bool *flip_horz_scan_dir)
716 {
717 	*orthogonal_rotation = false;
718 	*flip_vert_scan_dir = false;
719 	*flip_horz_scan_dir = false;
720 	if (rotation == ROTATION_ANGLE_180) {
721 		*flip_vert_scan_dir = true;
722 		*flip_horz_scan_dir = true;
723 	} else if (rotation == ROTATION_ANGLE_90) {
724 		*orthogonal_rotation = true;
725 		*flip_horz_scan_dir = true;
726 	} else if (rotation == ROTATION_ANGLE_270) {
727 		*orthogonal_rotation = true;
728 		*flip_vert_scan_dir = true;
729 	}
730 
731 	if (horizontal_mirror)
732 		*flip_horz_scan_dir = !*flip_horz_scan_dir;
733 }
734 
resource_get_num_mpc_splits(const struct pipe_ctx * pipe)735 int resource_get_num_mpc_splits(const struct pipe_ctx *pipe)
736 {
737 	int mpc_split_count = 0;
738 	const struct pipe_ctx *other_pipe = pipe->bottom_pipe;
739 
740 	while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
741 		mpc_split_count++;
742 		other_pipe = other_pipe->bottom_pipe;
743 	}
744 	other_pipe = pipe->top_pipe;
745 	while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
746 		mpc_split_count++;
747 		other_pipe = other_pipe->top_pipe;
748 	}
749 
750 	return mpc_split_count;
751 }
752 
resource_get_num_odm_splits(const struct pipe_ctx * pipe)753 int resource_get_num_odm_splits(const struct pipe_ctx *pipe)
754 {
755 	int odm_split_count = 0;
756 
757 	pipe = resource_get_otg_master(pipe);
758 
759 	while (pipe->next_odm_pipe) {
760 		odm_split_count++;
761 		pipe = pipe->next_odm_pipe;
762 	}
763 	return odm_split_count;
764 }
765 
get_odm_split_index(struct pipe_ctx * pipe_ctx)766 static int get_odm_split_index(struct pipe_ctx *pipe_ctx)
767 {
768 	int index = 0;
769 
770 	pipe_ctx = resource_get_opp_head(pipe_ctx);
771 	if (!pipe_ctx)
772 		return 0;
773 
774 	while (pipe_ctx->prev_odm_pipe) {
775 		index++;
776 		pipe_ctx = pipe_ctx->prev_odm_pipe;
777 	}
778 
779 	return index;
780 }
781 
get_mpc_split_index(struct pipe_ctx * pipe_ctx)782 static int get_mpc_split_index(struct pipe_ctx *pipe_ctx)
783 {
784 	struct pipe_ctx *split_pipe = pipe_ctx->top_pipe;
785 	int index = 0;
786 
787 	while (split_pipe && split_pipe->plane_state == pipe_ctx->plane_state) {
788 		index++;
789 		split_pipe = split_pipe->top_pipe;
790 	}
791 
792 	return index;
793 }
794 
795 /*
796  * This is a preliminary vp size calculation to allow us to check taps support.
797  * The result is completely overridden afterwards.
798  */
calculate_viewport_size(struct pipe_ctx * pipe_ctx)799 static void calculate_viewport_size(struct pipe_ctx *pipe_ctx)
800 {
801 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
802 
803 	data->viewport.width = dc_fixpt_ceil(dc_fixpt_mul_int(data->ratios.horz, data->recout.width));
804 	data->viewport.height = dc_fixpt_ceil(dc_fixpt_mul_int(data->ratios.vert, data->recout.height));
805 	data->viewport_c.width = dc_fixpt_ceil(dc_fixpt_mul_int(data->ratios.horz_c, data->recout.width));
806 	data->viewport_c.height = dc_fixpt_ceil(dc_fixpt_mul_int(data->ratios.vert_c, data->recout.height));
807 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
808 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270) {
809 		swap(data->viewport.width, data->viewport.height);
810 		swap(data->viewport_c.width, data->viewport_c.height);
811 	}
812 }
813 
intersect_rec(const struct rect * r0,const struct rect * r1)814 static struct rect intersect_rec(const struct rect *r0, const struct rect *r1)
815 {
816 	struct rect rec;
817 	int r0_x_end = r0->x + r0->width;
818 	int r1_x_end = r1->x + r1->width;
819 	int r0_y_end = r0->y + r0->height;
820 	int r1_y_end = r1->y + r1->height;
821 
822 	rec.x = r0->x > r1->x ? r0->x : r1->x;
823 	rec.width = r0_x_end > r1_x_end ? r1_x_end - rec.x : r0_x_end - rec.x;
824 	rec.y = r0->y > r1->y ? r0->y : r1->y;
825 	rec.height = r0_y_end > r1_y_end ? r1_y_end - rec.y : r0_y_end - rec.y;
826 
827 	/* in case that there is no intersection */
828 	if (rec.width < 0 || rec.height < 0)
829 		memset(&rec, 0, sizeof(rec));
830 
831 	return rec;
832 }
833 
shift_rec(const struct rect * rec_in,int x,int y)834 static struct rect shift_rec(const struct rect *rec_in, int x, int y)
835 {
836 	struct rect rec_out = *rec_in;
837 
838 	rec_out.x += x;
839 	rec_out.y += y;
840 
841 	return rec_out;
842 }
843 
calculate_odm_slice_in_timing_active(struct pipe_ctx * pipe_ctx)844 static struct rect calculate_odm_slice_in_timing_active(struct pipe_ctx *pipe_ctx)
845 {
846 	const struct dc_stream_state *stream = pipe_ctx->stream;
847 	int odm_slice_count = resource_get_num_odm_splits(pipe_ctx) + 1;
848 	int odm_slice_idx = get_odm_split_index(pipe_ctx);
849 	bool is_last_odm_slice = (odm_slice_idx + 1) == odm_slice_count;
850 	int h_active = stream->timing.h_addressable +
851 			stream->timing.h_border_left +
852 			stream->timing.h_border_right;
853 	int odm_slice_width = h_active / odm_slice_count;
854 	struct rect odm_rec;
855 
856 	odm_rec.x = odm_slice_width * odm_slice_idx;
857 	odm_rec.width = is_last_odm_slice ?
858 			/* last slice width is the reminder of h_active */
859 			h_active - odm_slice_width * (odm_slice_count - 1) :
860 			/* odm slice width is the floor of h_active / count */
861 			odm_slice_width;
862 	odm_rec.y = 0;
863 	odm_rec.height = stream->timing.v_addressable +
864 			stream->timing.v_border_bottom +
865 			stream->timing.v_border_top;
866 
867 	return odm_rec;
868 }
869 
calculate_plane_rec_in_timing_active(struct pipe_ctx * pipe_ctx,const struct rect * rec_in)870 static struct rect calculate_plane_rec_in_timing_active(
871 		struct pipe_ctx *pipe_ctx,
872 		const struct rect *rec_in)
873 {
874 	/*
875 	 * The following diagram shows an example where we map a 1920x1200
876 	 * desktop to a 2560x1440 timing with a plane rect in the middle
877 	 * of the screen. To map a plane rect from Stream Source to Timing
878 	 * Active space, we first multiply stream scaling ratios (i.e 2304/1920
879 	 * horizontal and 1440/1200 vertical) to the plane's x and y, then
880 	 * we add stream destination offsets (i.e 128 horizontal, 0 vertical).
881 	 * This will give us a plane rect's position in Timing Active. However
882 	 * we have to remove the fractional. The rule is that we find left/right
883 	 * and top/bottom positions and round the value to the adjacent integer.
884 	 *
885 	 * Stream Source Space
886 	 * ------------
887 	 *        __________________________________________________
888 	 *       |Stream Source (1920 x 1200) ^                     |
889 	 *       |                            y                     |
890 	 *       |         <------- w --------|>                    |
891 	 *       |          __________________V                     |
892 	 *       |<-- x -->|Plane//////////////| ^                  |
893 	 *       |         |(pre scale)////////| |                  |
894 	 *       |         |///////////////////| |                  |
895 	 *       |         |///////////////////| h                  |
896 	 *       |         |///////////////////| |                  |
897 	 *       |         |///////////////////| |                  |
898 	 *       |         |///////////////////| V                  |
899 	 *       |                                                  |
900 	 *       |                                                  |
901 	 *       |__________________________________________________|
902 	 *
903 	 *
904 	 * Timing Active Space
905 	 * ---------------------------------
906 	 *
907 	 *       Timing Active (2560 x 1440)
908 	 *        __________________________________________________
909 	 *       |*****|  Stteam Destination (2304 x 1440)    |*****|
910 	 *       |*****|                                      |*****|
911 	 *       |<128>|                                      |*****|
912 	 *       |*****|     __________________               |*****|
913 	 *       |*****|    |Plane/////////////|              |*****|
914 	 *       |*****|    |(post scale)//////|              |*****|
915 	 *       |*****|    |//////////////////|              |*****|
916 	 *       |*****|    |//////////////////|              |*****|
917 	 *       |*****|    |//////////////////|              |*****|
918 	 *       |*****|    |//////////////////|              |*****|
919 	 *       |*****|                                      |*****|
920 	 *       |*****|                                      |*****|
921 	 *       |*****|                                      |*****|
922 	 *       |*****|______________________________________|*****|
923 	 *
924 	 * So the resulting formulas are shown below:
925 	 *
926 	 * recout_x = 128 + round(plane_x * 2304 / 1920)
927 	 * recout_w = 128 + round((plane_x + plane_w) * 2304 / 1920) - recout_x
928 	 * recout_y = 0 + round(plane_y * 1440 / 1280)
929 	 * recout_h = 0 + round((plane_y + plane_h) * 1440 / 1200) - recout_y
930 	 *
931 	 * NOTE: fixed point division is not error free. To reduce errors
932 	 * introduced by fixed point division, we divide only after
933 	 * multiplication is complete.
934 	 */
935 	const struct dc_stream_state *stream = pipe_ctx->stream;
936 	struct rect rec_out = {0};
937 	struct fixed31_32 temp;
938 
939 	temp = dc_fixpt_from_fraction(rec_in->x * stream->dst.width,
940 			stream->src.width);
941 	rec_out.x = stream->dst.x + dc_fixpt_round(temp);
942 
943 	temp = dc_fixpt_from_fraction(
944 			(rec_in->x + rec_in->width) * stream->dst.width,
945 			stream->src.width);
946 	rec_out.width = stream->dst.x + dc_fixpt_round(temp) - rec_out.x;
947 
948 	temp = dc_fixpt_from_fraction(rec_in->y * stream->dst.height,
949 			stream->src.height);
950 	rec_out.y = stream->dst.y + dc_fixpt_round(temp);
951 
952 	temp = dc_fixpt_from_fraction(
953 			(rec_in->y + rec_in->height) * stream->dst.height,
954 			stream->src.height);
955 	rec_out.height = stream->dst.y + dc_fixpt_round(temp) - rec_out.y;
956 
957 	return rec_out;
958 }
959 
calculate_mpc_slice_in_timing_active(struct pipe_ctx * pipe_ctx,struct rect * plane_clip_rec)960 static struct rect calculate_mpc_slice_in_timing_active(
961 		struct pipe_ctx *pipe_ctx,
962 		struct rect *plane_clip_rec)
963 {
964 	const struct dc_stream_state *stream = pipe_ctx->stream;
965 	int mpc_slice_count = resource_get_num_mpc_splits(pipe_ctx) + 1;
966 	int mpc_slice_idx = get_mpc_split_index(pipe_ctx);
967 	int epimo = mpc_slice_count - plane_clip_rec->width % mpc_slice_count - 1;
968 	struct rect mpc_rec;
969 
970 	mpc_rec.width = plane_clip_rec->width / mpc_slice_count;
971 	mpc_rec.x = plane_clip_rec->x + mpc_rec.width * mpc_slice_idx;
972 	mpc_rec.height = plane_clip_rec->height;
973 	mpc_rec.y = plane_clip_rec->y;
974 	ASSERT(mpc_slice_count == 1 ||
975 			stream->view_format != VIEW_3D_FORMAT_SIDE_BY_SIDE ||
976 			mpc_rec.width % 2 == 0);
977 
978 	/* extra pixels in the division remainder need to go to pipes after
979 	 * the extra pixel index minus one(epimo) defined here as:
980 	 */
981 	if (mpc_slice_idx > epimo) {
982 		mpc_rec.x += mpc_slice_idx - epimo - 1;
983 		mpc_rec.width += 1;
984 	}
985 
986 	if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM) {
987 		ASSERT(mpc_rec.height % 2 == 0);
988 		mpc_rec.height /= 2;
989 	}
990 	return mpc_rec;
991 }
992 
adjust_recout_for_visual_confirm(struct rect * recout,struct pipe_ctx * pipe_ctx)993 static void adjust_recout_for_visual_confirm(struct rect *recout,
994 		struct pipe_ctx *pipe_ctx)
995 {
996 	struct dc *dc = pipe_ctx->stream->ctx->dc;
997 	int dpp_offset, base_offset;
998 
999 	if (dc->debug.visual_confirm == VISUAL_CONFIRM_DISABLE || !pipe_ctx->plane_res.dpp)
1000 		return;
1001 
1002 	dpp_offset = pipe_ctx->stream->timing.v_addressable / VISUAL_CONFIRM_DPP_OFFSET_DENO;
1003 	dpp_offset *= pipe_ctx->plane_res.dpp->inst;
1004 
1005 	if ((dc->debug.visual_confirm_rect_height >= VISUAL_CONFIRM_BASE_MIN) &&
1006 			dc->debug.visual_confirm_rect_height <= VISUAL_CONFIRM_BASE_MAX)
1007 		base_offset = dc->debug.visual_confirm_rect_height;
1008 	else
1009 		base_offset = VISUAL_CONFIRM_BASE_DEFAULT;
1010 
1011 	recout->height -= base_offset;
1012 	recout->height -= dpp_offset;
1013 }
1014 
1015 /*
1016  * The function maps a plane clip from Stream Source Space to ODM Slice Space
1017  * and calculates the rec of the overlapping area of MPC slice of the plane
1018  * clip, ODM slice associated with the pipe context and stream destination rec.
1019  */
calculate_recout(struct pipe_ctx * pipe_ctx)1020 static void calculate_recout(struct pipe_ctx *pipe_ctx)
1021 {
1022 	/*
1023 	 * A plane clip represents the desired plane size and position in Stream
1024 	 * Source Space. Stream Source is the destination where all planes are
1025 	 * blended (i.e. positioned, scaled and overlaid). It is a canvas where
1026 	 * all planes associated with the current stream are drawn together.
1027 	 * After Stream Source is completed, we will further scale and
1028 	 * reposition the entire canvas of the stream source to Stream
1029 	 * Destination in Timing Active Space. This could be due to display
1030 	 * overscan adjustment where we will need to rescale and reposition all
1031 	 * the planes so they can fit into a TV with overscan or downscale
1032 	 * upscale features such as GPU scaling or VSR.
1033 	 *
1034 	 * This two step blending is a virtual procedure in software. In
1035 	 * hardware there is no such thing as Stream Source. all planes are
1036 	 * blended once in Timing Active Space. Software virtualizes a Stream
1037 	 * Source space to decouple the math complicity so scaling param
1038 	 * calculation focuses on one step at a time.
1039 	 *
1040 	 * In the following two diagrams, user applied 10% overscan adjustment
1041 	 * so the Stream Source needs to be scaled down a little before mapping
1042 	 * to Timing Active Space. As a result the Plane Clip is also scaled
1043 	 * down by the same ratio, Plane Clip position (i.e. x and y) with
1044 	 * respect to Stream Source is also scaled down. To map it in Timing
1045 	 * Active Space additional x and y offsets from Stream Destination are
1046 	 * added to Plane Clip as well.
1047 	 *
1048 	 * Stream Source Space
1049 	 * ------------
1050 	 *        __________________________________________________
1051 	 *       |Stream Source (3840 x 2160) ^                     |
1052 	 *       |                            y                     |
1053 	 *       |                            |                     |
1054 	 *       |          __________________V                     |
1055 	 *       |<-- x -->|Plane Clip/////////|                    |
1056 	 *       |         |(pre scale)////////|                    |
1057 	 *       |         |///////////////////|                    |
1058 	 *       |         |///////////////////|                    |
1059 	 *       |         |///////////////////|                    |
1060 	 *       |         |///////////////////|                    |
1061 	 *       |         |///////////////////|                    |
1062 	 *       |                                                  |
1063 	 *       |                                                  |
1064 	 *       |__________________________________________________|
1065 	 *
1066 	 *
1067 	 * Timing Active Space (3840 x 2160)
1068 	 * ---------------------------------
1069 	 *
1070 	 *       Timing Active
1071 	 *        __________________________________________________
1072 	 *       | y_____________________________________________   |
1073 	 *       |x |Stream Destination (3456 x 1944)            |  |
1074 	 *       |  |                                            |  |
1075 	 *       |  |        __________________                  |  |
1076 	 *       |  |       |Plane Clip////////|                 |  |
1077 	 *       |  |       |(post scale)//////|                 |  |
1078 	 *       |  |       |//////////////////|                 |  |
1079 	 *       |  |       |//////////////////|                 |  |
1080 	 *       |  |       |//////////////////|                 |  |
1081 	 *       |  |       |//////////////////|                 |  |
1082 	 *       |  |                                            |  |
1083 	 *       |  |                                            |  |
1084 	 *       |  |____________________________________________|  |
1085 	 *       |__________________________________________________|
1086 	 *
1087 	 *
1088 	 * In Timing Active Space a plane clip could be further sliced into
1089 	 * pieces called MPC slices. Each Pipe Context is responsible for
1090 	 * processing only one MPC slice so the plane processing workload can be
1091 	 * distributed to multiple DPP Pipes. MPC slices could be blended
1092 	 * together to a single ODM slice. Each ODM slice is responsible for
1093 	 * processing a portion of Timing Active divided horizontally so the
1094 	 * output pixel processing workload can be distributed to multiple OPP
1095 	 * pipes. All ODM slices are mapped together in ODM block so all MPC
1096 	 * slices belong to different ODM slices could be pieced together to
1097 	 * form a single image in Timing Active. MPC slices must belong to
1098 	 * single ODM slice. If an MPC slice goes across ODM slice boundary, it
1099 	 * needs to be divided into two MPC slices one for each ODM slice.
1100 	 *
1101 	 * In the following diagram the output pixel processing workload is
1102 	 * divided horizontally into two ODM slices one for each OPP blend tree.
1103 	 * OPP0 blend tree is responsible for processing left half of Timing
1104 	 * Active, while OPP2 blend tree is responsible for processing right
1105 	 * half.
1106 	 *
1107 	 * The plane has two MPC slices. However since the right MPC slice goes
1108 	 * across ODM boundary, two DPP pipes are needed one for each OPP blend
1109 	 * tree. (i.e. DPP1 for OPP0 blend tree and DPP2 for OPP2 blend tree).
1110 	 *
1111 	 * Assuming that we have a Pipe Context associated with OPP0 and DPP1
1112 	 * working on processing the plane in the diagram. We want to know the
1113 	 * width and height of the shaded rectangle and its relative position
1114 	 * with respect to the ODM slice0. This is called the recout of the pipe
1115 	 * context.
1116 	 *
1117 	 * Planes can be at arbitrary size and position and there could be an
1118 	 * arbitrary number of MPC and ODM slices. The algorithm needs to take
1119 	 * all scenarios into account.
1120 	 *
1121 	 * Timing Active Space (3840 x 2160)
1122 	 * ---------------------------------
1123 	 *
1124 	 *       Timing Active
1125 	 *        __________________________________________________
1126 	 *       |OPP0(ODM slice0)^        |OPP2(ODM slice1)        |
1127 	 *       |                y        |                        |
1128 	 *       |                |  <- w ->                        |
1129 	 *       |           _____V________|____                    |
1130 	 *       |          |DPP0 ^  |DPP1 |DPP2|                   |
1131 	 *       |<------ x |-----|->|/////|    |                   |
1132 	 *       |          |     |  |/////|    |                   |
1133 	 *       |          |     h  |/////|    |                   |
1134 	 *       |          |     |  |/////|    |                   |
1135 	 *       |          |_____V__|/////|____|                   |
1136 	 *       |                         |                        |
1137 	 *       |                         |                        |
1138 	 *       |                         |                        |
1139 	 *       |_________________________|________________________|
1140 	 *
1141 	 *
1142 	 */
1143 	struct rect plane_clip;
1144 	struct rect mpc_slice_of_plane_clip;
1145 	struct rect odm_slice;
1146 	struct rect overlapping_area;
1147 
1148 	plane_clip = calculate_plane_rec_in_timing_active(pipe_ctx,
1149 			&pipe_ctx->plane_state->clip_rect);
1150 	/* guard plane clip from drawing beyond stream dst here */
1151 	plane_clip = intersect_rec(&plane_clip,
1152 				&pipe_ctx->stream->dst);
1153 	mpc_slice_of_plane_clip = calculate_mpc_slice_in_timing_active(
1154 			pipe_ctx, &plane_clip);
1155 	odm_slice = calculate_odm_slice_in_timing_active(pipe_ctx);
1156 	overlapping_area = intersect_rec(&mpc_slice_of_plane_clip, &odm_slice);
1157 	if (overlapping_area.height > 0 &&
1158 			overlapping_area.width > 0) {
1159 		/* shift the overlapping area so it is with respect to current
1160 		 * ODM slice's position
1161 		 */
1162 		pipe_ctx->plane_res.scl_data.recout = shift_rec(
1163 				&overlapping_area,
1164 				-odm_slice.x, -odm_slice.y);
1165 		adjust_recout_for_visual_confirm(
1166 				&pipe_ctx->plane_res.scl_data.recout,
1167 				pipe_ctx);
1168 	} else {
1169 		/* if there is no overlap, zero recout */
1170 		memset(&pipe_ctx->plane_res.scl_data.recout, 0,
1171 				sizeof(struct rect));
1172 	}
1173 
1174 }
1175 
calculate_scaling_ratios(struct pipe_ctx * pipe_ctx)1176 static void calculate_scaling_ratios(struct pipe_ctx *pipe_ctx)
1177 {
1178 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1179 	const struct dc_stream_state *stream = pipe_ctx->stream;
1180 	struct rect surf_src = plane_state->src_rect;
1181 	const int in_w = stream->src.width;
1182 	const int in_h = stream->src.height;
1183 	const int out_w = stream->dst.width;
1184 	const int out_h = stream->dst.height;
1185 
1186 	/*Swap surf_src height and width since scaling ratios are in recout rotation*/
1187 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
1188 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
1189 		swap(surf_src.height, surf_src.width);
1190 
1191 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_from_fraction(
1192 					surf_src.width,
1193 					plane_state->dst_rect.width);
1194 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_from_fraction(
1195 					surf_src.height,
1196 					plane_state->dst_rect.height);
1197 
1198 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
1199 		pipe_ctx->plane_res.scl_data.ratios.horz.value *= 2;
1200 	else if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
1201 		pipe_ctx->plane_res.scl_data.ratios.vert.value *= 2;
1202 
1203 	pipe_ctx->plane_res.scl_data.ratios.vert.value = div64_s64(
1204 		pipe_ctx->plane_res.scl_data.ratios.vert.value * in_h, out_h);
1205 	pipe_ctx->plane_res.scl_data.ratios.horz.value = div64_s64(
1206 		pipe_ctx->plane_res.scl_data.ratios.horz.value * in_w, out_w);
1207 
1208 	pipe_ctx->plane_res.scl_data.ratios.horz_c = pipe_ctx->plane_res.scl_data.ratios.horz;
1209 	pipe_ctx->plane_res.scl_data.ratios.vert_c = pipe_ctx->plane_res.scl_data.ratios.vert;
1210 
1211 	if (pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP8
1212 			|| pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP10) {
1213 		pipe_ctx->plane_res.scl_data.ratios.horz_c.value /= 2;
1214 		pipe_ctx->plane_res.scl_data.ratios.vert_c.value /= 2;
1215 	}
1216 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_truncate(
1217 			pipe_ctx->plane_res.scl_data.ratios.horz, 19);
1218 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_truncate(
1219 			pipe_ctx->plane_res.scl_data.ratios.vert, 19);
1220 	pipe_ctx->plane_res.scl_data.ratios.horz_c = dc_fixpt_truncate(
1221 			pipe_ctx->plane_res.scl_data.ratios.horz_c, 19);
1222 	pipe_ctx->plane_res.scl_data.ratios.vert_c = dc_fixpt_truncate(
1223 			pipe_ctx->plane_res.scl_data.ratios.vert_c, 19);
1224 }
1225 
1226 
1227 /*
1228  * We completely calculate vp offset, size and inits here based entirely on scaling
1229  * ratios and recout for pixel perfect pipe combine.
1230  */
calculate_init_and_vp(bool flip_scan_dir,int recout_offset_within_recout_full,int recout_size,int src_size,int taps,struct fixed31_32 ratio,struct fixed31_32 * init,int * vp_offset,int * vp_size)1231 static void calculate_init_and_vp(
1232 		bool flip_scan_dir,
1233 		int recout_offset_within_recout_full,
1234 		int recout_size,
1235 		int src_size,
1236 		int taps,
1237 		struct fixed31_32 ratio,
1238 		struct fixed31_32 *init,
1239 		int *vp_offset,
1240 		int *vp_size)
1241 {
1242 	struct fixed31_32 temp;
1243 	int int_part;
1244 
1245 	/*
1246 	 * First of the taps starts sampling pixel number <init_int_part> corresponding to recout
1247 	 * pixel 1. Next recout pixel samples int part of <init + scaling ratio> and so on.
1248 	 * All following calculations are based on this logic.
1249 	 *
1250 	 * Init calculated according to formula:
1251 	 * 	init = (scaling_ratio + number_of_taps + 1) / 2
1252 	 * 	init_bot = init + scaling_ratio
1253 	 * 	to get pixel perfect combine add the fraction from calculating vp offset
1254 	 */
1255 	temp = dc_fixpt_mul_int(ratio, recout_offset_within_recout_full);
1256 	*vp_offset = dc_fixpt_floor(temp);
1257 	temp.value &= 0xffffffff;
1258 	*init = dc_fixpt_truncate(dc_fixpt_add(dc_fixpt_div_int(
1259 			dc_fixpt_add_int(ratio, taps + 1), 2), temp), 19);
1260 	/*
1261 	 * If viewport has non 0 offset and there are more taps than covered by init then
1262 	 * we should decrease the offset and increase init so we are never sampling
1263 	 * outside of viewport.
1264 	 */
1265 	int_part = dc_fixpt_floor(*init);
1266 	if (int_part < taps) {
1267 		int_part = taps - int_part;
1268 		if (int_part > *vp_offset)
1269 			int_part = *vp_offset;
1270 		*vp_offset -= int_part;
1271 		*init = dc_fixpt_add_int(*init, int_part);
1272 	}
1273 	/*
1274 	 * If taps are sampling outside of viewport at end of recout and there are more pixels
1275 	 * available in the surface we should increase the viewport size, regardless set vp to
1276 	 * only what is used.
1277 	 */
1278 	temp = dc_fixpt_add(*init, dc_fixpt_mul_int(ratio, recout_size - 1));
1279 	*vp_size = dc_fixpt_floor(temp);
1280 	if (*vp_size + *vp_offset > src_size)
1281 		*vp_size = src_size - *vp_offset;
1282 
1283 	/* We did all the math assuming we are scanning same direction as display does,
1284 	 * however mirror/rotation changes how vp scans vs how it is offset. If scan direction
1285 	 * is flipped we simply need to calculate offset from the other side of plane.
1286 	 * Note that outside of viewport all scaling hardware works in recout space.
1287 	 */
1288 	if (flip_scan_dir)
1289 		*vp_offset = src_size - *vp_offset - *vp_size;
1290 }
1291 
calculate_inits_and_viewports(struct pipe_ctx * pipe_ctx)1292 static void calculate_inits_and_viewports(struct pipe_ctx *pipe_ctx)
1293 {
1294 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1295 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
1296 	struct rect src = plane_state->src_rect;
1297 	struct rect recout_dst_in_active_timing;
1298 	struct rect recout_clip_in_active_timing;
1299 	struct rect recout_clip_in_recout_dst;
1300 	struct rect overlap_in_active_timing;
1301 	struct rect odm_slice = calculate_odm_slice_in_timing_active(pipe_ctx);
1302 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
1303 				|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
1304 	bool orthogonal_rotation, flip_vert_scan_dir, flip_horz_scan_dir;
1305 
1306 	recout_clip_in_active_timing = shift_rec(
1307 			&data->recout, odm_slice.x, odm_slice.y);
1308 	recout_dst_in_active_timing = calculate_plane_rec_in_timing_active(
1309 			pipe_ctx, &plane_state->dst_rect);
1310 	overlap_in_active_timing = intersect_rec(&recout_clip_in_active_timing,
1311 			&recout_dst_in_active_timing);
1312 	if (overlap_in_active_timing.width > 0 &&
1313 			overlap_in_active_timing.height > 0)
1314 		recout_clip_in_recout_dst = shift_rec(&overlap_in_active_timing,
1315 				-recout_dst_in_active_timing.x,
1316 				-recout_dst_in_active_timing.y);
1317 	else
1318 		memset(&recout_clip_in_recout_dst, 0, sizeof(struct rect));
1319 
1320 	/*
1321 	 * Work in recout rotation since that requires less transformations
1322 	 */
1323 	get_vp_scan_direction(
1324 			plane_state->rotation,
1325 			plane_state->horizontal_mirror,
1326 			&orthogonal_rotation,
1327 			&flip_vert_scan_dir,
1328 			&flip_horz_scan_dir);
1329 
1330 	if (orthogonal_rotation) {
1331 		swap(src.width, src.height);
1332 		swap(flip_vert_scan_dir, flip_horz_scan_dir);
1333 	}
1334 
1335 	calculate_init_and_vp(
1336 			flip_horz_scan_dir,
1337 			recout_clip_in_recout_dst.x,
1338 			data->recout.width,
1339 			src.width,
1340 			data->taps.h_taps,
1341 			data->ratios.horz,
1342 			&data->inits.h,
1343 			&data->viewport.x,
1344 			&data->viewport.width);
1345 	calculate_init_and_vp(
1346 			flip_horz_scan_dir,
1347 			recout_clip_in_recout_dst.x,
1348 			data->recout.width,
1349 			src.width / vpc_div,
1350 			data->taps.h_taps_c,
1351 			data->ratios.horz_c,
1352 			&data->inits.h_c,
1353 			&data->viewport_c.x,
1354 			&data->viewport_c.width);
1355 	calculate_init_and_vp(
1356 			flip_vert_scan_dir,
1357 			recout_clip_in_recout_dst.y,
1358 			data->recout.height,
1359 			src.height,
1360 			data->taps.v_taps,
1361 			data->ratios.vert,
1362 			&data->inits.v,
1363 			&data->viewport.y,
1364 			&data->viewport.height);
1365 	calculate_init_and_vp(
1366 			flip_vert_scan_dir,
1367 			recout_clip_in_recout_dst.y,
1368 			data->recout.height,
1369 			src.height / vpc_div,
1370 			data->taps.v_taps_c,
1371 			data->ratios.vert_c,
1372 			&data->inits.v_c,
1373 			&data->viewport_c.y,
1374 			&data->viewport_c.height);
1375 	if (orthogonal_rotation) {
1376 		swap(data->viewport.x, data->viewport.y);
1377 		swap(data->viewport.width, data->viewport.height);
1378 		swap(data->viewport_c.x, data->viewport_c.y);
1379 		swap(data->viewport_c.width, data->viewport_c.height);
1380 	}
1381 	data->viewport.x += src.x;
1382 	data->viewport.y += src.y;
1383 	ASSERT(src.x % vpc_div == 0 && src.y % vpc_div == 0);
1384 	data->viewport_c.x += src.x / vpc_div;
1385 	data->viewport_c.y += src.y / vpc_div;
1386 }
1387 
resource_build_scaling_params(struct pipe_ctx * pipe_ctx)1388 bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
1389 {
1390 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1391 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
1392 	const struct rect odm_slice_rec = calculate_odm_slice_in_timing_active(pipe_ctx);
1393 	bool res = false;
1394 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1395 
1396 	/* Invalid input */
1397 	if (!plane_state ||
1398 			!plane_state->dst_rect.width ||
1399 			!plane_state->dst_rect.height ||
1400 			!plane_state->src_rect.width ||
1401 			!plane_state->src_rect.height) {
1402 		ASSERT(0);
1403 		return false;
1404 	}
1405 
1406 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
1407 			pipe_ctx->plane_state->format);
1408 
1409 	/* Timing borders are part of vactive that we are also supposed to skip in addition
1410 	 * to any stream dst offset. Since dm logic assumes dst is in addressable
1411 	 * space we need to add the left and top borders to dst offsets temporarily.
1412 	 * TODO: fix in DM, stream dst is supposed to be in vactive
1413 	 */
1414 	pipe_ctx->stream->dst.x += timing->h_border_left;
1415 	pipe_ctx->stream->dst.y += timing->v_border_top;
1416 
1417 	/* Calculate H and V active size */
1418 	pipe_ctx->plane_res.scl_data.h_active = odm_slice_rec.width;
1419 	pipe_ctx->plane_res.scl_data.v_active = odm_slice_rec.height;
1420 
1421 	/* depends on h_active */
1422 	calculate_recout(pipe_ctx);
1423 	/* depends on pixel format */
1424 	calculate_scaling_ratios(pipe_ctx);
1425 	/* depends on scaling ratios and recout, does not calculate offset yet */
1426 	calculate_viewport_size(pipe_ctx);
1427 
1428 	if (!pipe_ctx->stream->ctx->dc->config.enable_windowed_mpo_odm) {
1429 		/* Stopgap for validation of ODM + MPO on one side of screen case */
1430 		if (pipe_ctx->plane_res.scl_data.viewport.height < 1 ||
1431 				pipe_ctx->plane_res.scl_data.viewport.width < 1)
1432 			return false;
1433 	}
1434 
1435 	/*
1436 	 * LB calculations depend on vp size, h/v_active and scaling ratios
1437 	 * Setting line buffer pixel depth to 24bpp yields banding
1438 	 * on certain displays, such as the Sharp 4k. 36bpp is needed
1439 	 * to support SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616 and
1440 	 * SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616 with actual > 10 bpc
1441 	 * precision on DCN display engines, but apparently not for DCE, as
1442 	 * far as testing on DCE-11.2 and DCE-8 showed. Various DCE parts have
1443 	 * problems: Carrizo with DCE_VERSION_11_0 does not like 36 bpp lb depth,
1444 	 * neither do DCE-8 at 4k resolution, or DCE-11.2 (broken identify pixel
1445 	 * passthrough). Therefore only use 36 bpp on DCN where it is actually needed.
1446 	 */
1447 	if (plane_state->ctx->dce_version > DCE_VERSION_MAX)
1448 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_36BPP;
1449 	else
1450 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
1451 
1452 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = plane_state->per_pixel_alpha;
1453 
1454 	if (pipe_ctx->plane_res.xfm != NULL)
1455 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1456 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1457 
1458 	if (pipe_ctx->plane_res.dpp != NULL)
1459 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1460 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1461 
1462 
1463 	if (!res) {
1464 		/* Try 24 bpp linebuffer */
1465 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
1466 
1467 		if (pipe_ctx->plane_res.xfm != NULL)
1468 			res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1469 					pipe_ctx->plane_res.xfm,
1470 					&pipe_ctx->plane_res.scl_data,
1471 					&plane_state->scaling_quality);
1472 
1473 		if (pipe_ctx->plane_res.dpp != NULL)
1474 			res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1475 					pipe_ctx->plane_res.dpp,
1476 					&pipe_ctx->plane_res.scl_data,
1477 					&plane_state->scaling_quality);
1478 	}
1479 
1480 	/*
1481 	 * Depends on recout, scaling ratios, h_active and taps
1482 	 * May need to re-check lb size after this in some obscure scenario
1483 	 */
1484 	if (res)
1485 		calculate_inits_and_viewports(pipe_ctx);
1486 
1487 	/*
1488 	 * Handle side by side and top bottom 3d recout offsets after vp calculation
1489 	 * since 3d is special and needs to calculate vp as if there is no recout offset
1490 	 * This may break with rotation, good thing we aren't mixing hw rotation and 3d
1491 	 */
1492 	if (pipe_ctx->top_pipe && pipe_ctx->top_pipe->plane_state == plane_state) {
1493 		ASSERT(plane_state->rotation == ROTATION_ANGLE_0 ||
1494 			(pipe_ctx->stream->view_format != VIEW_3D_FORMAT_TOP_AND_BOTTOM &&
1495 				pipe_ctx->stream->view_format != VIEW_3D_FORMAT_SIDE_BY_SIDE));
1496 		if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
1497 			pipe_ctx->plane_res.scl_data.recout.y += pipe_ctx->plane_res.scl_data.recout.height;
1498 		else if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
1499 			pipe_ctx->plane_res.scl_data.recout.x += pipe_ctx->plane_res.scl_data.recout.width;
1500 	}
1501 
1502 	/* Clamp minimum viewport size */
1503 	if (pipe_ctx->plane_res.scl_data.viewport.height < MIN_VIEWPORT_SIZE)
1504 		pipe_ctx->plane_res.scl_data.viewport.height = MIN_VIEWPORT_SIZE;
1505 	if (pipe_ctx->plane_res.scl_data.viewport.width < MIN_VIEWPORT_SIZE)
1506 		pipe_ctx->plane_res.scl_data.viewport.width = MIN_VIEWPORT_SIZE;
1507 
1508 
1509 	DC_LOG_SCALER("%s pipe %d:\nViewport: height:%d width:%d x:%d y:%d  Recout: height:%d width:%d x:%d y:%d  HACTIVE:%d VACTIVE:%d\n"
1510 			"src_rect: height:%d width:%d x:%d y:%d  dst_rect: height:%d width:%d x:%d y:%d  clip_rect: height:%d width:%d x:%d y:%d\n",
1511 			__func__,
1512 			pipe_ctx->pipe_idx,
1513 			pipe_ctx->plane_res.scl_data.viewport.height,
1514 			pipe_ctx->plane_res.scl_data.viewport.width,
1515 			pipe_ctx->plane_res.scl_data.viewport.x,
1516 			pipe_ctx->plane_res.scl_data.viewport.y,
1517 			pipe_ctx->plane_res.scl_data.recout.height,
1518 			pipe_ctx->plane_res.scl_data.recout.width,
1519 			pipe_ctx->plane_res.scl_data.recout.x,
1520 			pipe_ctx->plane_res.scl_data.recout.y,
1521 			pipe_ctx->plane_res.scl_data.h_active,
1522 			pipe_ctx->plane_res.scl_data.v_active,
1523 			plane_state->src_rect.height,
1524 			plane_state->src_rect.width,
1525 			plane_state->src_rect.x,
1526 			plane_state->src_rect.y,
1527 			plane_state->dst_rect.height,
1528 			plane_state->dst_rect.width,
1529 			plane_state->dst_rect.x,
1530 			plane_state->dst_rect.y,
1531 			plane_state->clip_rect.height,
1532 			plane_state->clip_rect.width,
1533 			plane_state->clip_rect.x,
1534 			plane_state->clip_rect.y);
1535 
1536 	pipe_ctx->stream->dst.x -= timing->h_border_left;
1537 	pipe_ctx->stream->dst.y -= timing->v_border_top;
1538 
1539 	return res;
1540 }
1541 
1542 
resource_build_scaling_params_for_context(const struct dc * dc,struct dc_state * context)1543 enum dc_status resource_build_scaling_params_for_context(
1544 	const struct dc  *dc,
1545 	struct dc_state *context)
1546 {
1547 	int i;
1548 
1549 	for (i = 0; i < MAX_PIPES; i++) {
1550 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
1551 				context->res_ctx.pipe_ctx[i].stream != NULL)
1552 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1553 				return DC_FAIL_SCALING;
1554 	}
1555 
1556 	return DC_OK;
1557 }
1558 
resource_find_free_secondary_pipe_legacy(struct resource_context * res_ctx,const struct resource_pool * pool,const struct pipe_ctx * primary_pipe)1559 struct pipe_ctx *resource_find_free_secondary_pipe_legacy(
1560 		struct resource_context *res_ctx,
1561 		const struct resource_pool *pool,
1562 		const struct pipe_ctx *primary_pipe)
1563 {
1564 	int i;
1565 	struct pipe_ctx *secondary_pipe = NULL;
1566 
1567 	/*
1568 	 * We add a preferred pipe mapping to avoid the chance that
1569 	 * MPCCs already in use will need to be reassigned to other trees.
1570 	 * For example, if we went with the strict, assign backwards logic:
1571 	 *
1572 	 * (State 1)
1573 	 * Display A on, no surface, top pipe = 0
1574 	 * Display B on, no surface, top pipe = 1
1575 	 *
1576 	 * (State 2)
1577 	 * Display A on, no surface, top pipe = 0
1578 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 5
1579 	 *
1580 	 * (State 3)
1581 	 * Display A on, surface enable, top pipe = 0, bottom pipe = 5
1582 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1583 	 *
1584 	 * The state 2->3 transition requires remapping MPCC 5 from display B
1585 	 * to display A.
1586 	 *
1587 	 * However, with the preferred pipe logic, state 2 would look like:
1588 	 *
1589 	 * (State 2)
1590 	 * Display A on, no surface, top pipe = 0
1591 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1592 	 *
1593 	 * This would then cause 2->3 to not require remapping any MPCCs.
1594 	 */
1595 	if (primary_pipe) {
1596 		int preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
1597 		if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1598 			secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1599 			secondary_pipe->pipe_idx = preferred_pipe_idx;
1600 		}
1601 	}
1602 
1603 	/*
1604 	 * search backwards for the second pipe to keep pipe
1605 	 * assignment more consistent
1606 	 */
1607 	if (!secondary_pipe)
1608 		for (i = pool->pipe_count - 1; i >= 0; i--) {
1609 			if (res_ctx->pipe_ctx[i].stream == NULL) {
1610 				secondary_pipe = &res_ctx->pipe_ctx[i];
1611 				secondary_pipe->pipe_idx = i;
1612 				break;
1613 			}
1614 		}
1615 
1616 	return secondary_pipe;
1617 }
1618 
resource_find_free_pipe_used_in_cur_mpc_blending_tree(const struct resource_context * cur_res_ctx,struct resource_context * new_res_ctx,const struct pipe_ctx * cur_opp_head)1619 int resource_find_free_pipe_used_in_cur_mpc_blending_tree(
1620 		const struct resource_context *cur_res_ctx,
1621 		struct resource_context *new_res_ctx,
1622 		const struct pipe_ctx *cur_opp_head)
1623 {
1624 	const struct pipe_ctx *cur_sec_dpp = cur_opp_head->bottom_pipe;
1625 	struct pipe_ctx *new_pipe;
1626 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1627 
1628 	while (cur_sec_dpp) {
1629 		/* find a free pipe used in current opp blend tree,
1630 		 * this is to avoid MPO pipe switching to different opp blending
1631 		 * tree
1632 		 */
1633 		new_pipe = &new_res_ctx->pipe_ctx[cur_sec_dpp->pipe_idx];
1634 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1635 			free_pipe_idx = cur_sec_dpp->pipe_idx;
1636 			break;
1637 		}
1638 		cur_sec_dpp = cur_sec_dpp->bottom_pipe;
1639 	}
1640 
1641 	return free_pipe_idx;
1642 }
1643 
recource_find_free_pipe_not_used_in_cur_res_ctx(const struct resource_context * cur_res_ctx,struct resource_context * new_res_ctx,const struct resource_pool * pool)1644 int recource_find_free_pipe_not_used_in_cur_res_ctx(
1645 		const struct resource_context *cur_res_ctx,
1646 		struct resource_context *new_res_ctx,
1647 		const struct resource_pool *pool)
1648 {
1649 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1650 	const struct pipe_ctx *new_pipe, *cur_pipe;
1651 	int i;
1652 
1653 	for (i = 0; i < pool->pipe_count; i++) {
1654 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1655 		new_pipe = &new_res_ctx->pipe_ctx[i];
1656 
1657 		if (resource_is_pipe_type(cur_pipe, FREE_PIPE) &&
1658 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1659 			free_pipe_idx = i;
1660 			break;
1661 		}
1662 	}
1663 
1664 	return free_pipe_idx;
1665 }
1666 
resource_find_free_pipe_used_as_cur_sec_dpp_in_mpcc_combine(const struct resource_context * cur_res_ctx,struct resource_context * new_res_ctx,const struct resource_pool * pool)1667 int resource_find_free_pipe_used_as_cur_sec_dpp_in_mpcc_combine(
1668 		const struct resource_context *cur_res_ctx,
1669 		struct resource_context *new_res_ctx,
1670 		const struct resource_pool *pool)
1671 {
1672 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1673 	const struct pipe_ctx *new_pipe, *cur_pipe;
1674 	int i;
1675 
1676 	for (i = 0; i < pool->pipe_count; i++) {
1677 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1678 		new_pipe = &new_res_ctx->pipe_ctx[i];
1679 
1680 		if (resource_is_pipe_type(cur_pipe, DPP_PIPE) &&
1681 				!resource_is_pipe_type(cur_pipe, OPP_HEAD) &&
1682 				resource_is_for_mpcc_combine(cur_pipe) &&
1683 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1684 			free_pipe_idx = i;
1685 			break;
1686 		}
1687 	}
1688 
1689 	return free_pipe_idx;
1690 }
1691 
resource_find_any_free_pipe(struct resource_context * new_res_ctx,const struct resource_pool * pool)1692 int resource_find_any_free_pipe(struct resource_context *new_res_ctx,
1693 		const struct resource_pool *pool)
1694 {
1695 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1696 	const struct pipe_ctx *new_pipe;
1697 	int i;
1698 
1699 	for (i = 0; i < pool->pipe_count; i++) {
1700 		new_pipe = &new_res_ctx->pipe_ctx[i];
1701 
1702 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1703 			free_pipe_idx = i;
1704 			break;
1705 		}
1706 	}
1707 
1708 	return free_pipe_idx;
1709 }
1710 
resource_is_pipe_type(const struct pipe_ctx * pipe_ctx,enum pipe_type type)1711 bool resource_is_pipe_type(const struct pipe_ctx *pipe_ctx, enum pipe_type type)
1712 {
1713 #ifdef DBG
1714 	if (pipe_ctx->stream == NULL) {
1715 		/* a free pipe with dangling states */
1716 		ASSERT(!pipe_ctx->plane_state);
1717 		ASSERT(!pipe_ctx->prev_odm_pipe);
1718 		ASSERT(!pipe_ctx->next_odm_pipe);
1719 		ASSERT(!pipe_ctx->top_pipe);
1720 		ASSERT(!pipe_ctx->bottom_pipe);
1721 	} else if (pipe_ctx->top_pipe) {
1722 		/* a secondary DPP pipe must be signed to a plane */
1723 		ASSERT(pipe_ctx->plane_state)
1724 	}
1725 	/* Add more checks here to prevent corrupted pipe ctx. It is very hard
1726 	 * to debug this issue afterwards because we can't pinpoint the code
1727 	 * location causing inconsistent pipe context states.
1728 	 */
1729 #endif
1730 	switch (type) {
1731 	case OTG_MASTER:
1732 		return !pipe_ctx->prev_odm_pipe &&
1733 				!pipe_ctx->top_pipe &&
1734 				pipe_ctx->stream;
1735 	case OPP_HEAD:
1736 		return !pipe_ctx->top_pipe && pipe_ctx->stream;
1737 	case DPP_PIPE:
1738 		return pipe_ctx->plane_state && pipe_ctx->stream;
1739 	case FREE_PIPE:
1740 		return !pipe_ctx->plane_state && !pipe_ctx->stream;
1741 	default:
1742 		return false;
1743 	}
1744 }
1745 
resource_is_for_mpcc_combine(const struct pipe_ctx * pipe_ctx)1746 bool resource_is_for_mpcc_combine(const struct pipe_ctx *pipe_ctx)
1747 {
1748 	return resource_get_num_mpc_splits(pipe_ctx) > 0;
1749 }
1750 
resource_get_otg_master_for_stream(struct resource_context * res_ctx,struct dc_stream_state * stream)1751 struct pipe_ctx *resource_get_otg_master_for_stream(
1752 		struct resource_context *res_ctx,
1753 		struct dc_stream_state *stream)
1754 {
1755 	int i;
1756 
1757 	for (i = 0; i < MAX_PIPES; i++) {
1758 		if (res_ctx->pipe_ctx[i].stream == stream &&
1759 				resource_is_pipe_type(&res_ctx->pipe_ctx[i], OTG_MASTER))
1760 			return &res_ctx->pipe_ctx[i];
1761 	}
1762 	return NULL;
1763 }
1764 
resource_get_otg_master(const struct pipe_ctx * pipe_ctx)1765 struct pipe_ctx *resource_get_otg_master(const struct pipe_ctx *pipe_ctx)
1766 {
1767 	struct pipe_ctx *otg_master = resource_get_opp_head(pipe_ctx);
1768 
1769 	while (otg_master->prev_odm_pipe)
1770 		otg_master = otg_master->prev_odm_pipe;
1771 	return otg_master;
1772 }
1773 
resource_get_opp_head(const struct pipe_ctx * pipe_ctx)1774 struct pipe_ctx *resource_get_opp_head(const struct pipe_ctx *pipe_ctx)
1775 {
1776 	struct pipe_ctx *opp_head = (struct pipe_ctx *) pipe_ctx;
1777 
1778 	ASSERT(!resource_is_pipe_type(opp_head, FREE_PIPE));
1779 	while (opp_head->top_pipe)
1780 		opp_head = opp_head->top_pipe;
1781 	return opp_head;
1782 }
1783 
get_tail_pipe(struct pipe_ctx * head_pipe)1784 static struct pipe_ctx *get_tail_pipe(
1785 		struct pipe_ctx *head_pipe)
1786 {
1787 	struct pipe_ctx *tail_pipe = head_pipe->bottom_pipe;
1788 
1789 	while (tail_pipe) {
1790 		head_pipe = tail_pipe;
1791 		tail_pipe = tail_pipe->bottom_pipe;
1792 	}
1793 
1794 	return head_pipe;
1795 }
1796 
acquire_first_split_pipe(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)1797 static int acquire_first_split_pipe(
1798 		struct resource_context *res_ctx,
1799 		const struct resource_pool *pool,
1800 		struct dc_stream_state *stream)
1801 {
1802 	int i;
1803 
1804 	for (i = 0; i < pool->pipe_count; i++) {
1805 		struct pipe_ctx *split_pipe = &res_ctx->pipe_ctx[i];
1806 
1807 		if (split_pipe->top_pipe &&
1808 				split_pipe->top_pipe->plane_state == split_pipe->plane_state) {
1809 			split_pipe->top_pipe->bottom_pipe = split_pipe->bottom_pipe;
1810 			if (split_pipe->bottom_pipe)
1811 				split_pipe->bottom_pipe->top_pipe = split_pipe->top_pipe;
1812 
1813 			if (split_pipe->top_pipe->plane_state)
1814 				resource_build_scaling_params(split_pipe->top_pipe);
1815 
1816 			memset(split_pipe, 0, sizeof(*split_pipe));
1817 			split_pipe->stream_res.tg = pool->timing_generators[i];
1818 			split_pipe->plane_res.hubp = pool->hubps[i];
1819 			split_pipe->plane_res.ipp = pool->ipps[i];
1820 			split_pipe->plane_res.dpp = pool->dpps[i];
1821 			split_pipe->stream_res.opp = pool->opps[i];
1822 			split_pipe->plane_res.mpcc_inst = pool->dpps[i]->inst;
1823 			split_pipe->pipe_idx = i;
1824 
1825 			split_pipe->stream = stream;
1826 			return i;
1827 		}
1828 	}
1829 	return UNABLE_TO_SPLIT;
1830 }
1831 
add_plane_to_opp_head_pipes(struct pipe_ctx * otg_master_pipe,struct dc_plane_state * plane_state,struct dc_state * context)1832 static bool add_plane_to_opp_head_pipes(struct pipe_ctx *otg_master_pipe,
1833 		struct dc_plane_state *plane_state,
1834 		struct dc_state *context)
1835 {
1836 	struct pipe_ctx *opp_head_pipe = otg_master_pipe;
1837 
1838 	while (opp_head_pipe) {
1839 		if (opp_head_pipe->plane_state) {
1840 			ASSERT(0);
1841 			return false;
1842 		}
1843 		opp_head_pipe->plane_state = plane_state;
1844 		opp_head_pipe = opp_head_pipe->next_odm_pipe;
1845 	}
1846 
1847 	return true;
1848 }
1849 
insert_secondary_dpp_pipe_with_plane(struct pipe_ctx * opp_head_pipe,struct pipe_ctx * sec_pipe,struct dc_plane_state * plane_state)1850 static void insert_secondary_dpp_pipe_with_plane(struct pipe_ctx *opp_head_pipe,
1851 		struct pipe_ctx *sec_pipe, struct dc_plane_state *plane_state)
1852 {
1853 	struct pipe_ctx *tail_pipe = get_tail_pipe(opp_head_pipe);
1854 
1855 	tail_pipe->bottom_pipe = sec_pipe;
1856 	sec_pipe->top_pipe = tail_pipe;
1857 	if (tail_pipe->prev_odm_pipe) {
1858 		ASSERT(tail_pipe->prev_odm_pipe->bottom_pipe);
1859 		sec_pipe->prev_odm_pipe = tail_pipe->prev_odm_pipe->bottom_pipe;
1860 		tail_pipe->prev_odm_pipe->bottom_pipe->next_odm_pipe = sec_pipe;
1861 	}
1862 	sec_pipe->plane_state = plane_state;
1863 }
1864 
1865 /* for each opp head pipe of an otg master pipe, acquire a secondary dpp pipe
1866  * and add the plane. So the plane is added to all MPC blend trees associated
1867  * with the otg master pipe.
1868  */
acquire_secondary_dpp_pipes_and_add_plane(struct pipe_ctx * otg_master_pipe,struct dc_plane_state * plane_state,struct dc_state * new_ctx,struct dc_state * cur_ctx,struct resource_pool * pool)1869 static bool acquire_secondary_dpp_pipes_and_add_plane(
1870 		struct pipe_ctx *otg_master_pipe,
1871 		struct dc_plane_state *plane_state,
1872 		struct dc_state *new_ctx,
1873 		struct dc_state *cur_ctx,
1874 		struct resource_pool *pool)
1875 {
1876 	struct pipe_ctx *opp_head_pipe, *sec_pipe;
1877 
1878 	if (!pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe)
1879 		return false;
1880 
1881 	opp_head_pipe = otg_master_pipe;
1882 	while (opp_head_pipe) {
1883 		sec_pipe = pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe(
1884 				cur_ctx,
1885 				new_ctx,
1886 				pool,
1887 				opp_head_pipe);
1888 		if (!sec_pipe) {
1889 			/* try tearing down MPCC combine */
1890 			int pipe_idx = acquire_first_split_pipe(
1891 					&new_ctx->res_ctx, pool,
1892 					otg_master_pipe->stream);
1893 
1894 			if (pipe_idx >= 0)
1895 				sec_pipe = &new_ctx->res_ctx.pipe_ctx[pipe_idx];
1896 		}
1897 
1898 		if (!sec_pipe)
1899 			return false;
1900 
1901 		insert_secondary_dpp_pipe_with_plane(opp_head_pipe, sec_pipe,
1902 				plane_state);
1903 		opp_head_pipe = opp_head_pipe->next_odm_pipe;
1904 	}
1905 	return true;
1906 }
1907 
dc_add_plane_to_context(const struct dc * dc,struct dc_stream_state * stream,struct dc_plane_state * plane_state,struct dc_state * context)1908 bool dc_add_plane_to_context(
1909 		const struct dc *dc,
1910 		struct dc_stream_state *stream,
1911 		struct dc_plane_state *plane_state,
1912 		struct dc_state *context)
1913 {
1914 	struct resource_pool *pool = dc->res_pool;
1915 	struct pipe_ctx *otg_master_pipe;
1916 	struct dc_stream_status *stream_status = NULL;
1917 	bool added = false;
1918 
1919 	stream_status = dc_stream_get_status_from_state(context, stream);
1920 	if (stream_status == NULL) {
1921 		dm_error("Existing stream not found; failed to attach surface!\n");
1922 		goto out;
1923 	} else if (stream_status->plane_count == MAX_SURFACE_NUM) {
1924 		dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
1925 				plane_state, MAX_SURFACE_NUM);
1926 		goto out;
1927 	}
1928 
1929 	otg_master_pipe = resource_get_otg_master_for_stream(
1930 			&context->res_ctx, stream);
1931 	if (otg_master_pipe->plane_state == NULL)
1932 		added = add_plane_to_opp_head_pipes(otg_master_pipe,
1933 				plane_state, context);
1934 	else
1935 		added = acquire_secondary_dpp_pipes_and_add_plane(
1936 				otg_master_pipe, plane_state, context,
1937 				dc->current_state, pool);
1938 	if (added) {
1939 		stream_status->plane_states[stream_status->plane_count] =
1940 				plane_state;
1941 		stream_status->plane_count++;
1942 		dc_plane_state_retain(plane_state);
1943 	}
1944 
1945 out:
1946 	return added;
1947 }
1948 
dc_remove_plane_from_context(const struct dc * dc,struct dc_stream_state * stream,struct dc_plane_state * plane_state,struct dc_state * context)1949 bool dc_remove_plane_from_context(
1950 		const struct dc *dc,
1951 		struct dc_stream_state *stream,
1952 		struct dc_plane_state *plane_state,
1953 		struct dc_state *context)
1954 {
1955 	int i;
1956 	struct dc_stream_status *stream_status = NULL;
1957 	struct resource_pool *pool = dc->res_pool;
1958 
1959 	if (!plane_state)
1960 		return true;
1961 
1962 	for (i = 0; i < context->stream_count; i++)
1963 		if (context->streams[i] == stream) {
1964 			stream_status = &context->stream_status[i];
1965 			break;
1966 		}
1967 
1968 	if (stream_status == NULL) {
1969 		dm_error("Existing stream not found; failed to remove plane.\n");
1970 		return false;
1971 	}
1972 
1973 	/* release pipe for plane*/
1974 	for (i = pool->pipe_count - 1; i >= 0; i--) {
1975 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1976 
1977 		if (pipe_ctx->plane_state == plane_state) {
1978 			if (pipe_ctx->top_pipe)
1979 				pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
1980 
1981 			/* Second condition is to avoid setting NULL to top pipe
1982 			 * of tail pipe making it look like head pipe in subsequent
1983 			 * deletes
1984 			 */
1985 			if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
1986 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
1987 
1988 			/*
1989 			 * For head pipe detach surfaces from pipe for tail
1990 			 * pipe just zero it out
1991 			 */
1992 			if (!pipe_ctx->top_pipe)
1993 				pipe_ctx->plane_state = NULL;
1994 			else
1995 				memset(pipe_ctx, 0, sizeof(*pipe_ctx));
1996 		}
1997 	}
1998 
1999 
2000 	for (i = 0; i < stream_status->plane_count; i++) {
2001 		if (stream_status->plane_states[i] == plane_state) {
2002 			dc_plane_state_release(stream_status->plane_states[i]);
2003 			break;
2004 		}
2005 	}
2006 
2007 	if (i == stream_status->plane_count) {
2008 		dm_error("Existing plane_state not found; failed to detach it!\n");
2009 		return false;
2010 	}
2011 
2012 	stream_status->plane_count--;
2013 
2014 	/* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
2015 	for (; i < stream_status->plane_count; i++)
2016 		stream_status->plane_states[i] = stream_status->plane_states[i + 1];
2017 
2018 	stream_status->plane_states[stream_status->plane_count] = NULL;
2019 
2020 	return true;
2021 }
2022 
2023 /**
2024  * dc_rem_all_planes_for_stream - Remove planes attached to the target stream.
2025  *
2026  * @dc: Current dc state.
2027  * @stream: Target stream, which we want to remove the attached plans.
2028  * @context: New context.
2029  *
2030  * Return:
2031  * Return true if DC was able to remove all planes from the target
2032  * stream, otherwise, return false.
2033  */
dc_rem_all_planes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)2034 bool dc_rem_all_planes_for_stream(
2035 		const struct dc *dc,
2036 		struct dc_stream_state *stream,
2037 		struct dc_state *context)
2038 {
2039 	int i, old_plane_count;
2040 	struct dc_stream_status *stream_status = NULL;
2041 	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
2042 
2043 	for (i = 0; i < context->stream_count; i++)
2044 			if (context->streams[i] == stream) {
2045 				stream_status = &context->stream_status[i];
2046 				break;
2047 			}
2048 
2049 	if (stream_status == NULL) {
2050 		dm_error("Existing stream %p not found!\n", stream);
2051 		return false;
2052 	}
2053 
2054 	old_plane_count = stream_status->plane_count;
2055 
2056 	for (i = 0; i < old_plane_count; i++)
2057 		del_planes[i] = stream_status->plane_states[i];
2058 
2059 	for (i = 0; i < old_plane_count; i++)
2060 		if (!dc_remove_plane_from_context(dc, stream, del_planes[i], context))
2061 			return false;
2062 
2063 	return true;
2064 }
2065 
add_all_planes_for_stream(const struct dc * dc,struct dc_stream_state * stream,const struct dc_validation_set set[],int set_count,struct dc_state * context)2066 static bool add_all_planes_for_stream(
2067 		const struct dc *dc,
2068 		struct dc_stream_state *stream,
2069 		const struct dc_validation_set set[],
2070 		int set_count,
2071 		struct dc_state *context)
2072 {
2073 	int i, j;
2074 
2075 	for (i = 0; i < set_count; i++)
2076 		if (set[i].stream == stream)
2077 			break;
2078 
2079 	if (i == set_count) {
2080 		dm_error("Stream %p not found in set!\n", stream);
2081 		return false;
2082 	}
2083 
2084 	for (j = 0; j < set[i].plane_count; j++)
2085 		if (!dc_add_plane_to_context(dc, stream, set[i].plane_states[j], context))
2086 			return false;
2087 
2088 	return true;
2089 }
2090 
dc_add_all_planes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_plane_state * const * plane_states,int plane_count,struct dc_state * context)2091 bool dc_add_all_planes_for_stream(
2092 		const struct dc *dc,
2093 		struct dc_stream_state *stream,
2094 		struct dc_plane_state * const *plane_states,
2095 		int plane_count,
2096 		struct dc_state *context)
2097 {
2098 	struct dc_validation_set set;
2099 	int i;
2100 
2101 	set.stream = stream;
2102 	set.plane_count = plane_count;
2103 
2104 	for (i = 0; i < plane_count; i++)
2105 		set.plane_states[i] = plane_states[i];
2106 
2107 	return add_all_planes_for_stream(dc, stream, &set, 1, context);
2108 }
2109 
dc_is_timing_changed(struct dc_stream_state * cur_stream,struct dc_stream_state * new_stream)2110 bool dc_is_timing_changed(struct dc_stream_state *cur_stream,
2111 		       struct dc_stream_state *new_stream)
2112 {
2113 	if (cur_stream == NULL)
2114 		return true;
2115 
2116 	/* If output color space is changed, need to reprogram info frames */
2117 	if (cur_stream->output_color_space != new_stream->output_color_space)
2118 		return true;
2119 
2120 	return memcmp(
2121 		&cur_stream->timing,
2122 		&new_stream->timing,
2123 		sizeof(struct dc_crtc_timing)) != 0;
2124 }
2125 
are_stream_backends_same(struct dc_stream_state * stream_a,struct dc_stream_state * stream_b)2126 static bool are_stream_backends_same(
2127 	struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
2128 {
2129 	if (stream_a == stream_b)
2130 		return true;
2131 
2132 	if (stream_a == NULL || stream_b == NULL)
2133 		return false;
2134 
2135 	if (dc_is_timing_changed(stream_a, stream_b))
2136 		return false;
2137 
2138 	if (stream_a->signal != stream_b->signal)
2139 		return false;
2140 
2141 	if (stream_a->dpms_off != stream_b->dpms_off)
2142 		return false;
2143 
2144 	return true;
2145 }
2146 
2147 /*
2148  * dc_is_stream_unchanged() - Compare two stream states for equivalence.
2149  *
2150  * Checks if there a difference between the two states
2151  * that would require a mode change.
2152  *
2153  * Does not compare cursor position or attributes.
2154  */
dc_is_stream_unchanged(struct dc_stream_state * old_stream,struct dc_stream_state * stream)2155 bool dc_is_stream_unchanged(
2156 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
2157 {
2158 	if (!old_stream || !stream)
2159 		return false;
2160 
2161 	if (!are_stream_backends_same(old_stream, stream))
2162 		return false;
2163 
2164 	if (old_stream->ignore_msa_timing_param != stream->ignore_msa_timing_param)
2165 		return false;
2166 
2167 	/*compare audio info*/
2168 	if (memcmp(&old_stream->audio_info, &stream->audio_info, sizeof(stream->audio_info)) != 0)
2169 		return false;
2170 
2171 	return true;
2172 }
2173 
2174 /*
2175  * dc_is_stream_scaling_unchanged() - Compare scaling rectangles of two streams.
2176  */
dc_is_stream_scaling_unchanged(struct dc_stream_state * old_stream,struct dc_stream_state * stream)2177 bool dc_is_stream_scaling_unchanged(struct dc_stream_state *old_stream,
2178 				    struct dc_stream_state *stream)
2179 {
2180 	if (old_stream == stream)
2181 		return true;
2182 
2183 	if (old_stream == NULL || stream == NULL)
2184 		return false;
2185 
2186 	if (memcmp(&old_stream->src,
2187 			&stream->src,
2188 			sizeof(struct rect)) != 0)
2189 		return false;
2190 
2191 	if (memcmp(&old_stream->dst,
2192 			&stream->dst,
2193 			sizeof(struct rect)) != 0)
2194 		return false;
2195 
2196 	return true;
2197 }
2198 
update_stream_engine_usage(struct resource_context * res_ctx,const struct resource_pool * pool,struct stream_encoder * stream_enc,bool acquired)2199 static void update_stream_engine_usage(
2200 		struct resource_context *res_ctx,
2201 		const struct resource_pool *pool,
2202 		struct stream_encoder *stream_enc,
2203 		bool acquired)
2204 {
2205 	int i;
2206 
2207 	for (i = 0; i < pool->stream_enc_count; i++) {
2208 		if (pool->stream_enc[i] == stream_enc)
2209 			res_ctx->is_stream_enc_acquired[i] = acquired;
2210 	}
2211 }
2212 
update_hpo_dp_stream_engine_usage(struct resource_context * res_ctx,const struct resource_pool * pool,struct hpo_dp_stream_encoder * hpo_dp_stream_enc,bool acquired)2213 static void update_hpo_dp_stream_engine_usage(
2214 		struct resource_context *res_ctx,
2215 		const struct resource_pool *pool,
2216 		struct hpo_dp_stream_encoder *hpo_dp_stream_enc,
2217 		bool acquired)
2218 {
2219 	int i;
2220 
2221 	for (i = 0; i < pool->hpo_dp_stream_enc_count; i++) {
2222 		if (pool->hpo_dp_stream_enc[i] == hpo_dp_stream_enc)
2223 			res_ctx->is_hpo_dp_stream_enc_acquired[i] = acquired;
2224 	}
2225 }
2226 
find_acquired_hpo_dp_link_enc_for_link(const struct resource_context * res_ctx,const struct dc_link * link)2227 static inline int find_acquired_hpo_dp_link_enc_for_link(
2228 		const struct resource_context *res_ctx,
2229 		const struct dc_link *link)
2230 {
2231 	int i;
2232 
2233 	for (i = 0; i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_to_link_idx); i++)
2234 		if (res_ctx->hpo_dp_link_enc_ref_cnts[i] > 0 &&
2235 				res_ctx->hpo_dp_link_enc_to_link_idx[i] == link->link_index)
2236 			return i;
2237 
2238 	return -1;
2239 }
2240 
find_free_hpo_dp_link_enc(const struct resource_context * res_ctx,const struct resource_pool * pool)2241 static inline int find_free_hpo_dp_link_enc(const struct resource_context *res_ctx,
2242 		const struct resource_pool *pool)
2243 {
2244 	int i;
2245 
2246 	for (i = 0; i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_ref_cnts); i++)
2247 		if (res_ctx->hpo_dp_link_enc_ref_cnts[i] == 0)
2248 			break;
2249 
2250 	return (i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_ref_cnts) &&
2251 			i < pool->hpo_dp_link_enc_count) ? i : -1;
2252 }
2253 
acquire_hpo_dp_link_enc(struct resource_context * res_ctx,unsigned int link_index,int enc_index)2254 static inline void acquire_hpo_dp_link_enc(
2255 		struct resource_context *res_ctx,
2256 		unsigned int link_index,
2257 		int enc_index)
2258 {
2259 	res_ctx->hpo_dp_link_enc_to_link_idx[enc_index] = link_index;
2260 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index] = 1;
2261 }
2262 
retain_hpo_dp_link_enc(struct resource_context * res_ctx,int enc_index)2263 static inline void retain_hpo_dp_link_enc(
2264 		struct resource_context *res_ctx,
2265 		int enc_index)
2266 {
2267 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index]++;
2268 }
2269 
release_hpo_dp_link_enc(struct resource_context * res_ctx,int enc_index)2270 static inline void release_hpo_dp_link_enc(
2271 		struct resource_context *res_ctx,
2272 		int enc_index)
2273 {
2274 	ASSERT(res_ctx->hpo_dp_link_enc_ref_cnts[enc_index] > 0);
2275 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index]--;
2276 }
2277 
add_hpo_dp_link_enc_to_ctx(struct resource_context * res_ctx,const struct resource_pool * pool,struct pipe_ctx * pipe_ctx,struct dc_stream_state * stream)2278 static bool add_hpo_dp_link_enc_to_ctx(struct resource_context *res_ctx,
2279 		const struct resource_pool *pool,
2280 		struct pipe_ctx *pipe_ctx,
2281 		struct dc_stream_state *stream)
2282 {
2283 	int enc_index;
2284 
2285 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, stream->link);
2286 
2287 	if (enc_index >= 0) {
2288 		retain_hpo_dp_link_enc(res_ctx, enc_index);
2289 	} else {
2290 		enc_index = find_free_hpo_dp_link_enc(res_ctx, pool);
2291 		if (enc_index >= 0)
2292 			acquire_hpo_dp_link_enc(res_ctx, stream->link->link_index, enc_index);
2293 	}
2294 
2295 	if (enc_index >= 0)
2296 		pipe_ctx->link_res.hpo_dp_link_enc = pool->hpo_dp_link_enc[enc_index];
2297 
2298 	return pipe_ctx->link_res.hpo_dp_link_enc != NULL;
2299 }
2300 
remove_hpo_dp_link_enc_from_ctx(struct resource_context * res_ctx,struct pipe_ctx * pipe_ctx,struct dc_stream_state * stream)2301 static void remove_hpo_dp_link_enc_from_ctx(struct resource_context *res_ctx,
2302 		struct pipe_ctx *pipe_ctx,
2303 		struct dc_stream_state *stream)
2304 {
2305 	int enc_index;
2306 
2307 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, stream->link);
2308 
2309 	if (enc_index >= 0) {
2310 		release_hpo_dp_link_enc(res_ctx, enc_index);
2311 		pipe_ctx->link_res.hpo_dp_link_enc = NULL;
2312 	}
2313 }
2314 
2315 /* TODO: release audio object */
update_audio_usage(struct resource_context * res_ctx,const struct resource_pool * pool,struct audio * audio,bool acquired)2316 void update_audio_usage(
2317 		struct resource_context *res_ctx,
2318 		const struct resource_pool *pool,
2319 		struct audio *audio,
2320 		bool acquired)
2321 {
2322 	int i;
2323 	for (i = 0; i < pool->audio_count; i++) {
2324 		if (pool->audios[i] == audio)
2325 			res_ctx->is_audio_acquired[i] = acquired;
2326 	}
2327 }
2328 
acquire_first_free_pipe(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)2329 static int acquire_first_free_pipe(
2330 		struct resource_context *res_ctx,
2331 		const struct resource_pool *pool,
2332 		struct dc_stream_state *stream)
2333 {
2334 	int i;
2335 
2336 	for (i = 0; i < pool->pipe_count; i++) {
2337 		if (!res_ctx->pipe_ctx[i].stream) {
2338 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
2339 
2340 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
2341 			pipe_ctx->plane_res.mi = pool->mis[i];
2342 			pipe_ctx->plane_res.hubp = pool->hubps[i];
2343 			pipe_ctx->plane_res.ipp = pool->ipps[i];
2344 			pipe_ctx->plane_res.xfm = pool->transforms[i];
2345 			pipe_ctx->plane_res.dpp = pool->dpps[i];
2346 			pipe_ctx->stream_res.opp = pool->opps[i];
2347 			if (pool->dpps[i])
2348 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[i]->inst;
2349 			pipe_ctx->pipe_idx = i;
2350 
2351 			if (i >= pool->timing_generator_count) {
2352 				int tg_inst = pool->timing_generator_count - 1;
2353 
2354 				pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
2355 				pipe_ctx->stream_res.opp = pool->opps[tg_inst];
2356 			}
2357 
2358 			pipe_ctx->stream = stream;
2359 			return i;
2360 		}
2361 	}
2362 	return -1;
2363 }
2364 
find_first_free_match_hpo_dp_stream_enc_for_link(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)2365 static struct hpo_dp_stream_encoder *find_first_free_match_hpo_dp_stream_enc_for_link(
2366 		struct resource_context *res_ctx,
2367 		const struct resource_pool *pool,
2368 		struct dc_stream_state *stream)
2369 {
2370 	int i;
2371 
2372 	for (i = 0; i < pool->hpo_dp_stream_enc_count; i++) {
2373 		if (!res_ctx->is_hpo_dp_stream_enc_acquired[i] &&
2374 				pool->hpo_dp_stream_enc[i]) {
2375 
2376 			return pool->hpo_dp_stream_enc[i];
2377 		}
2378 	}
2379 
2380 	return NULL;
2381 }
2382 
find_first_free_audio(struct resource_context * res_ctx,const struct resource_pool * pool,enum engine_id id,enum dce_version dc_version)2383 static struct audio *find_first_free_audio(
2384 		struct resource_context *res_ctx,
2385 		const struct resource_pool *pool,
2386 		enum engine_id id,
2387 		enum dce_version dc_version)
2388 {
2389 	int i, available_audio_count;
2390 
2391 	if (id == ENGINE_ID_UNKNOWN)
2392 		return NULL;
2393 
2394 	available_audio_count = pool->audio_count;
2395 
2396 	for (i = 0; i < available_audio_count; i++) {
2397 		if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
2398 			/*we have enough audio endpoint, find the matching inst*/
2399 			if (id != i)
2400 				continue;
2401 			return pool->audios[i];
2402 		}
2403 	}
2404 
2405 	/* use engine id to find free audio */
2406 	if ((id < available_audio_count) && (res_ctx->is_audio_acquired[id] == false)) {
2407 		return pool->audios[id];
2408 	}
2409 	/*not found the matching one, first come first serve*/
2410 	for (i = 0; i < available_audio_count; i++) {
2411 		if (res_ctx->is_audio_acquired[i] == false) {
2412 			return pool->audios[i];
2413 		}
2414 	}
2415 	return NULL;
2416 }
2417 
2418 /*
2419  * dc_add_stream_to_ctx() - Add a new dc_stream_state to a dc_state.
2420  */
dc_add_stream_to_ctx(struct dc * dc,struct dc_state * new_ctx,struct dc_stream_state * stream)2421 enum dc_status dc_add_stream_to_ctx(
2422 		struct dc *dc,
2423 		struct dc_state *new_ctx,
2424 		struct dc_stream_state *stream)
2425 {
2426 	enum dc_status res;
2427 	DC_LOGGER_INIT(dc->ctx->logger);
2428 
2429 	if (new_ctx->stream_count >= dc->res_pool->timing_generator_count) {
2430 		DC_LOG_WARNING("Max streams reached, can't add stream %p !\n", stream);
2431 		return DC_ERROR_UNEXPECTED;
2432 	}
2433 
2434 	new_ctx->streams[new_ctx->stream_count] = stream;
2435 	dc_stream_retain(stream);
2436 	new_ctx->stream_count++;
2437 
2438 	res = dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
2439 	if (res != DC_OK)
2440 		DC_LOG_WARNING("Adding stream %p to context failed with err %d!\n", stream, res);
2441 
2442 	return res;
2443 }
2444 
2445 /*
2446  * dc_remove_stream_from_ctx() - Remove a stream from a dc_state.
2447  */
dc_remove_stream_from_ctx(struct dc * dc,struct dc_state * new_ctx,struct dc_stream_state * stream)2448 enum dc_status dc_remove_stream_from_ctx(
2449 			struct dc *dc,
2450 			struct dc_state *new_ctx,
2451 			struct dc_stream_state *stream)
2452 {
2453 	int i;
2454 	struct dc_context *dc_ctx = dc->ctx;
2455 	struct pipe_ctx *del_pipe = resource_get_otg_master_for_stream(&new_ctx->res_ctx, stream);
2456 	struct pipe_ctx *odm_pipe;
2457 
2458 	if (!del_pipe) {
2459 		DC_ERROR("Pipe not found for stream %p !\n", stream);
2460 		return DC_ERROR_UNEXPECTED;
2461 	}
2462 
2463 	odm_pipe = del_pipe->next_odm_pipe;
2464 
2465 	/* Release primary pipe */
2466 	ASSERT(del_pipe->stream_res.stream_enc);
2467 	update_stream_engine_usage(
2468 			&new_ctx->res_ctx,
2469 				dc->res_pool,
2470 			del_pipe->stream_res.stream_enc,
2471 			false);
2472 
2473 	if (dc->link_srv->dp_is_128b_132b_signal(del_pipe)) {
2474 		update_hpo_dp_stream_engine_usage(
2475 			&new_ctx->res_ctx, dc->res_pool,
2476 			del_pipe->stream_res.hpo_dp_stream_enc,
2477 			false);
2478 		remove_hpo_dp_link_enc_from_ctx(&new_ctx->res_ctx, del_pipe, del_pipe->stream);
2479 	}
2480 
2481 	if (del_pipe->stream_res.audio)
2482 		update_audio_usage(
2483 			&new_ctx->res_ctx,
2484 			dc->res_pool,
2485 			del_pipe->stream_res.audio,
2486 			false);
2487 
2488 	resource_unreference_clock_source(&new_ctx->res_ctx,
2489 					  dc->res_pool,
2490 					  del_pipe->clock_source);
2491 
2492 	if (dc->res_pool->funcs->remove_stream_from_ctx)
2493 		dc->res_pool->funcs->remove_stream_from_ctx(dc, new_ctx, stream);
2494 
2495 	while (odm_pipe) {
2496 		struct pipe_ctx *next_odm_pipe = odm_pipe->next_odm_pipe;
2497 
2498 		memset(odm_pipe, 0, sizeof(*odm_pipe));
2499 		odm_pipe = next_odm_pipe;
2500 	}
2501 	memset(del_pipe, 0, sizeof(*del_pipe));
2502 
2503 	for (i = 0; i < new_ctx->stream_count; i++)
2504 		if (new_ctx->streams[i] == stream)
2505 			break;
2506 
2507 	if (new_ctx->streams[i] != stream) {
2508 		DC_ERROR("Context doesn't have stream %p !\n", stream);
2509 		return DC_ERROR_UNEXPECTED;
2510 	}
2511 
2512 	dc_stream_release(new_ctx->streams[i]);
2513 	new_ctx->stream_count--;
2514 
2515 	/* Trim back arrays */
2516 	for (; i < new_ctx->stream_count; i++) {
2517 		new_ctx->streams[i] = new_ctx->streams[i + 1];
2518 		new_ctx->stream_status[i] = new_ctx->stream_status[i + 1];
2519 	}
2520 
2521 	new_ctx->streams[new_ctx->stream_count] = NULL;
2522 	memset(
2523 			&new_ctx->stream_status[new_ctx->stream_count],
2524 			0,
2525 			sizeof(new_ctx->stream_status[0]));
2526 
2527 	return DC_OK;
2528 }
2529 
find_pll_sharable_stream(struct dc_stream_state * stream_needs_pll,struct dc_state * context)2530 static struct dc_stream_state *find_pll_sharable_stream(
2531 		struct dc_stream_state *stream_needs_pll,
2532 		struct dc_state *context)
2533 {
2534 	int i;
2535 
2536 	for (i = 0; i < context->stream_count; i++) {
2537 		struct dc_stream_state *stream_has_pll = context->streams[i];
2538 
2539 		/* We are looking for non dp, non virtual stream */
2540 		if (resource_are_streams_timing_synchronizable(
2541 			stream_needs_pll, stream_has_pll)
2542 			&& !dc_is_dp_signal(stream_has_pll->signal)
2543 			&& stream_has_pll->link->connector_signal
2544 			!= SIGNAL_TYPE_VIRTUAL)
2545 			return stream_has_pll;
2546 
2547 	}
2548 
2549 	return NULL;
2550 }
2551 
get_norm_pix_clk(const struct dc_crtc_timing * timing)2552 static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
2553 {
2554 	uint32_t pix_clk = timing->pix_clk_100hz;
2555 	uint32_t normalized_pix_clk = pix_clk;
2556 
2557 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2558 		pix_clk /= 2;
2559 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
2560 		switch (timing->display_color_depth) {
2561 		case COLOR_DEPTH_666:
2562 		case COLOR_DEPTH_888:
2563 			normalized_pix_clk = pix_clk;
2564 			break;
2565 		case COLOR_DEPTH_101010:
2566 			normalized_pix_clk = (pix_clk * 30) / 24;
2567 			break;
2568 		case COLOR_DEPTH_121212:
2569 			normalized_pix_clk = (pix_clk * 36) / 24;
2570 		break;
2571 		case COLOR_DEPTH_161616:
2572 			normalized_pix_clk = (pix_clk * 48) / 24;
2573 		break;
2574 		default:
2575 			ASSERT(0);
2576 		break;
2577 		}
2578 	}
2579 	return normalized_pix_clk;
2580 }
2581 
calculate_phy_pix_clks(struct dc_stream_state * stream)2582 static void calculate_phy_pix_clks(struct dc_stream_state *stream)
2583 {
2584 	/* update actual pixel clock on all streams */
2585 	if (dc_is_hdmi_signal(stream->signal))
2586 		stream->phy_pix_clk = get_norm_pix_clk(
2587 			&stream->timing) / 10;
2588 	else
2589 		stream->phy_pix_clk =
2590 			stream->timing.pix_clk_100hz / 10;
2591 
2592 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
2593 		stream->phy_pix_clk *= 2;
2594 }
2595 
acquire_resource_from_hw_enabled_state(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)2596 static int acquire_resource_from_hw_enabled_state(
2597 		struct resource_context *res_ctx,
2598 		const struct resource_pool *pool,
2599 		struct dc_stream_state *stream)
2600 {
2601 	struct dc_link *link = stream->link;
2602 	unsigned int i, inst, tg_inst = 0;
2603 	uint32_t numPipes = 1;
2604 	uint32_t id_src[4] = {0};
2605 
2606 	/* Check for enabled DIG to identify enabled display */
2607 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
2608 		return -1;
2609 
2610 	inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
2611 
2612 	if (inst == ENGINE_ID_UNKNOWN)
2613 		return -1;
2614 
2615 	for (i = 0; i < pool->stream_enc_count; i++) {
2616 		if (pool->stream_enc[i]->id == inst) {
2617 			tg_inst = pool->stream_enc[i]->funcs->dig_source_otg(
2618 				pool->stream_enc[i]);
2619 			break;
2620 		}
2621 	}
2622 
2623 	// tg_inst not found
2624 	if (i == pool->stream_enc_count)
2625 		return -1;
2626 
2627 	if (tg_inst >= pool->timing_generator_count)
2628 		return -1;
2629 
2630 	if (!res_ctx->pipe_ctx[tg_inst].stream) {
2631 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[tg_inst];
2632 
2633 		pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
2634 		id_src[0] = tg_inst;
2635 
2636 		if (pipe_ctx->stream_res.tg->funcs->get_optc_source)
2637 			pipe_ctx->stream_res.tg->funcs->get_optc_source(pipe_ctx->stream_res.tg,
2638 						&numPipes, &id_src[0], &id_src[1]);
2639 
2640 		if (id_src[0] == 0xf && id_src[1] == 0xf) {
2641 			id_src[0] = tg_inst;
2642 			numPipes = 1;
2643 		}
2644 
2645 		for (i = 0; i < numPipes; i++) {
2646 			//Check if src id invalid
2647 			if (id_src[i] == 0xf)
2648 				return -1;
2649 
2650 			pipe_ctx = &res_ctx->pipe_ctx[id_src[i]];
2651 
2652 			pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
2653 			pipe_ctx->plane_res.mi = pool->mis[id_src[i]];
2654 			pipe_ctx->plane_res.hubp = pool->hubps[id_src[i]];
2655 			pipe_ctx->plane_res.ipp = pool->ipps[id_src[i]];
2656 			pipe_ctx->plane_res.xfm = pool->transforms[id_src[i]];
2657 			pipe_ctx->plane_res.dpp = pool->dpps[id_src[i]];
2658 			pipe_ctx->stream_res.opp = pool->opps[id_src[i]];
2659 
2660 			if (pool->dpps[id_src[i]]) {
2661 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[id_src[i]]->inst;
2662 
2663 				if (pool->mpc->funcs->read_mpcc_state) {
2664 					struct mpcc_state s = {0};
2665 
2666 					pool->mpc->funcs->read_mpcc_state(pool->mpc, pipe_ctx->plane_res.mpcc_inst, &s);
2667 
2668 					if (s.dpp_id < MAX_MPCC)
2669 						pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].dpp_id =
2670 								s.dpp_id;
2671 
2672 					if (s.bot_mpcc_id < MAX_MPCC)
2673 						pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].mpcc_bot =
2674 								&pool->mpc->mpcc_array[s.bot_mpcc_id];
2675 
2676 					if (s.opp_id < MAX_OPP)
2677 						pipe_ctx->stream_res.opp->mpc_tree_params.opp_id = s.opp_id;
2678 				}
2679 			}
2680 			pipe_ctx->pipe_idx = id_src[i];
2681 
2682 			if (id_src[i] >= pool->timing_generator_count) {
2683 				id_src[i] = pool->timing_generator_count - 1;
2684 
2685 				pipe_ctx->stream_res.tg = pool->timing_generators[id_src[i]];
2686 				pipe_ctx->stream_res.opp = pool->opps[id_src[i]];
2687 			}
2688 
2689 			pipe_ctx->stream = stream;
2690 		}
2691 
2692 		if (numPipes == 2) {
2693 			stream->apply_boot_odm_mode = dm_odm_combine_policy_2to1;
2694 			res_ctx->pipe_ctx[id_src[0]].next_odm_pipe = &res_ctx->pipe_ctx[id_src[1]];
2695 			res_ctx->pipe_ctx[id_src[0]].prev_odm_pipe = NULL;
2696 			res_ctx->pipe_ctx[id_src[1]].next_odm_pipe = NULL;
2697 			res_ctx->pipe_ctx[id_src[1]].prev_odm_pipe = &res_ctx->pipe_ctx[id_src[0]];
2698 		} else
2699 			stream->apply_boot_odm_mode = dm_odm_combine_mode_disabled;
2700 
2701 		return id_src[0];
2702 	}
2703 
2704 	return -1;
2705 }
2706 
mark_seamless_boot_stream(const struct dc * dc,struct dc_stream_state * stream)2707 static void mark_seamless_boot_stream(
2708 		const struct dc  *dc,
2709 		struct dc_stream_state *stream)
2710 {
2711 	struct dc_bios *dcb = dc->ctx->dc_bios;
2712 
2713 	if (dc->config.allow_seamless_boot_optimization &&
2714 			!dcb->funcs->is_accelerated_mode(dcb)) {
2715 		if (dc_validate_boot_timing(dc, stream->sink, &stream->timing))
2716 			stream->apply_seamless_boot_optimization = true;
2717 	}
2718 }
2719 
resource_map_pool_resources(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)2720 enum dc_status resource_map_pool_resources(
2721 		const struct dc  *dc,
2722 		struct dc_state *context,
2723 		struct dc_stream_state *stream)
2724 {
2725 	const struct resource_pool *pool = dc->res_pool;
2726 	int i;
2727 	struct dc_context *dc_ctx = dc->ctx;
2728 	struct pipe_ctx *pipe_ctx = NULL;
2729 	int pipe_idx = -1;
2730 
2731 	calculate_phy_pix_clks(stream);
2732 
2733 	mark_seamless_boot_stream(dc, stream);
2734 
2735 	if (stream->apply_seamless_boot_optimization) {
2736 		pipe_idx = acquire_resource_from_hw_enabled_state(
2737 				&context->res_ctx,
2738 				pool,
2739 				stream);
2740 		if (pipe_idx < 0)
2741 			/* hw resource was assigned to other stream */
2742 			stream->apply_seamless_boot_optimization = false;
2743 	}
2744 
2745 	if (pipe_idx < 0)
2746 		/* acquire new resources */
2747 		pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
2748 
2749 	if (pipe_idx < 0)
2750 		pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
2751 
2752 	if (pipe_idx < 0 || context->res_ctx.pipe_ctx[pipe_idx].stream_res.tg == NULL)
2753 		return DC_NO_CONTROLLER_RESOURCE;
2754 
2755 	pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
2756 
2757 	pipe_ctx->stream_res.stream_enc =
2758 		dc->res_pool->funcs->find_first_free_match_stream_enc_for_link(
2759 			&context->res_ctx, pool, stream);
2760 
2761 	if (!pipe_ctx->stream_res.stream_enc)
2762 		return DC_NO_STREAM_ENC_RESOURCE;
2763 
2764 	update_stream_engine_usage(
2765 		&context->res_ctx, pool,
2766 		pipe_ctx->stream_res.stream_enc,
2767 		true);
2768 
2769 	/* Allocate DP HPO Stream Encoder based on signal, hw capabilities
2770 	 * and link settings
2771 	 */
2772 	if (dc_is_dp_signal(stream->signal)) {
2773 		if (!dc->link_srv->dp_decide_link_settings(stream, &pipe_ctx->link_config.dp_link_settings))
2774 			return DC_FAIL_DP_LINK_BANDWIDTH;
2775 		if (dc->link_srv->dp_get_encoding_format(
2776 				&pipe_ctx->link_config.dp_link_settings) == DP_128b_132b_ENCODING) {
2777 			pipe_ctx->stream_res.hpo_dp_stream_enc =
2778 					find_first_free_match_hpo_dp_stream_enc_for_link(
2779 							&context->res_ctx, pool, stream);
2780 
2781 			if (!pipe_ctx->stream_res.hpo_dp_stream_enc)
2782 				return DC_NO_STREAM_ENC_RESOURCE;
2783 
2784 			update_hpo_dp_stream_engine_usage(
2785 					&context->res_ctx, pool,
2786 					pipe_ctx->stream_res.hpo_dp_stream_enc,
2787 					true);
2788 			if (!add_hpo_dp_link_enc_to_ctx(&context->res_ctx, pool, pipe_ctx, stream))
2789 				return DC_NO_LINK_ENC_RESOURCE;
2790 		}
2791 	}
2792 
2793 	/* TODO: Add check if ASIC support and EDID audio */
2794 	if (!stream->converter_disable_audio &&
2795 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
2796 	    stream->audio_info.mode_count && stream->audio_info.flags.all) {
2797 		pipe_ctx->stream_res.audio = find_first_free_audio(
2798 		&context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id, dc_ctx->dce_version);
2799 
2800 		/*
2801 		 * Audio assigned in order first come first get.
2802 		 * There are asics which has number of audio
2803 		 * resources less then number of pipes
2804 		 */
2805 		if (pipe_ctx->stream_res.audio)
2806 			update_audio_usage(&context->res_ctx, pool,
2807 					   pipe_ctx->stream_res.audio, true);
2808 	}
2809 
2810 	/* Add ABM to the resource if on EDP */
2811 	if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal)) {
2812 		if (pool->abm)
2813 			pipe_ctx->stream_res.abm = pool->abm;
2814 		else
2815 			pipe_ctx->stream_res.abm = pool->multiple_abms[pipe_ctx->stream_res.tg->inst];
2816 	}
2817 
2818 	for (i = 0; i < context->stream_count; i++)
2819 		if (context->streams[i] == stream) {
2820 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
2821 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->stream_enc_inst;
2822 			context->stream_status[i].audio_inst =
2823 				pipe_ctx->stream_res.audio ? pipe_ctx->stream_res.audio->inst : -1;
2824 
2825 			return DC_OK;
2826 		}
2827 
2828 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
2829 	return DC_ERROR_UNEXPECTED;
2830 }
2831 
2832 /**
2833  * dc_resource_state_copy_construct_current() - Creates a new dc_state from existing state
2834  *
2835  * @dc: copy out of dc->current_state
2836  * @dst_ctx: copy into this
2837  *
2838  * This function makes a shallow copy of the current DC state and increments
2839  * refcounts on existing streams and planes.
2840  */
dc_resource_state_copy_construct_current(const struct dc * dc,struct dc_state * dst_ctx)2841 void dc_resource_state_copy_construct_current(
2842 		const struct dc *dc,
2843 		struct dc_state *dst_ctx)
2844 {
2845 	dc_resource_state_copy_construct(dc->current_state, dst_ctx);
2846 }
2847 
2848 
dc_resource_state_construct(const struct dc * dc,struct dc_state * dst_ctx)2849 void dc_resource_state_construct(
2850 		const struct dc *dc,
2851 		struct dc_state *dst_ctx)
2852 {
2853 	dst_ctx->clk_mgr = dc->clk_mgr;
2854 
2855 	/* Initialise DIG link encoder resource tracking variables. */
2856 	link_enc_cfg_init(dc, dst_ctx);
2857 }
2858 
2859 
dc_resource_is_dsc_encoding_supported(const struct dc * dc)2860 bool dc_resource_is_dsc_encoding_supported(const struct dc *dc)
2861 {
2862 	if (dc->res_pool == NULL)
2863 		return false;
2864 
2865 	return dc->res_pool->res_cap->num_dsc > 0;
2866 }
2867 
planes_changed_for_existing_stream(struct dc_state * context,struct dc_stream_state * stream,const struct dc_validation_set set[],int set_count)2868 static bool planes_changed_for_existing_stream(struct dc_state *context,
2869 					       struct dc_stream_state *stream,
2870 					       const struct dc_validation_set set[],
2871 					       int set_count)
2872 {
2873 	int i, j;
2874 	struct dc_stream_status *stream_status = NULL;
2875 
2876 	for (i = 0; i < context->stream_count; i++) {
2877 		if (context->streams[i] == stream) {
2878 			stream_status = &context->stream_status[i];
2879 			break;
2880 		}
2881 	}
2882 
2883 	if (!stream_status) {
2884 		ASSERT(0);
2885 		return false;
2886 	}
2887 
2888 	for (i = 0; i < set_count; i++)
2889 		if (set[i].stream == stream)
2890 			break;
2891 
2892 	if (i == set_count)
2893 		ASSERT(0);
2894 
2895 	if (set[i].plane_count != stream_status->plane_count)
2896 		return true;
2897 
2898 	for (j = 0; j < set[i].plane_count; j++)
2899 		if (set[i].plane_states[j] != stream_status->plane_states[j])
2900 			return true;
2901 
2902 	return false;
2903 }
2904 
2905 /**
2906  * dc_validate_with_context - Validate and update the potential new stream in the context object
2907  *
2908  * @dc: Used to get the current state status
2909  * @set: An array of dc_validation_set with all the current streams reference
2910  * @set_count: Total of streams
2911  * @context: New context
2912  * @fast_validate: Enable or disable fast validation
2913  *
2914  * This function updates the potential new stream in the context object. It
2915  * creates multiple lists for the add, remove, and unchanged streams. In
2916  * particular, if the unchanged streams have a plane that changed, it is
2917  * necessary to remove all planes from the unchanged streams. In summary, this
2918  * function is responsible for validating the new context.
2919  *
2920  * Return:
2921  * In case of success, return DC_OK (1), otherwise, return a DC error.
2922  */
dc_validate_with_context(struct dc * dc,const struct dc_validation_set set[],int set_count,struct dc_state * context,bool fast_validate)2923 enum dc_status dc_validate_with_context(struct dc *dc,
2924 					const struct dc_validation_set set[],
2925 					int set_count,
2926 					struct dc_state *context,
2927 					bool fast_validate)
2928 {
2929 	struct dc_stream_state *unchanged_streams[MAX_PIPES] = { 0 };
2930 	struct dc_stream_state *del_streams[MAX_PIPES] = { 0 };
2931 	struct dc_stream_state *add_streams[MAX_PIPES] = { 0 };
2932 	int old_stream_count = context->stream_count;
2933 	enum dc_status res = DC_ERROR_UNEXPECTED;
2934 	int unchanged_streams_count = 0;
2935 	int del_streams_count = 0;
2936 	int add_streams_count = 0;
2937 	bool found = false;
2938 	int i, j, k;
2939 
2940 	DC_LOGGER_INIT(dc->ctx->logger);
2941 
2942 	/* First build a list of streams to be remove from current context */
2943 	for (i = 0; i < old_stream_count; i++) {
2944 		struct dc_stream_state *stream = context->streams[i];
2945 
2946 		for (j = 0; j < set_count; j++) {
2947 			if (stream == set[j].stream) {
2948 				found = true;
2949 				break;
2950 			}
2951 		}
2952 
2953 		if (!found)
2954 			del_streams[del_streams_count++] = stream;
2955 
2956 		found = false;
2957 	}
2958 
2959 	/* Second, build a list of new streams */
2960 	for (i = 0; i < set_count; i++) {
2961 		struct dc_stream_state *stream = set[i].stream;
2962 
2963 		for (j = 0; j < old_stream_count; j++) {
2964 			if (stream == context->streams[j]) {
2965 				found = true;
2966 				break;
2967 			}
2968 		}
2969 
2970 		if (!found)
2971 			add_streams[add_streams_count++] = stream;
2972 
2973 		found = false;
2974 	}
2975 
2976 	/* Build a list of unchanged streams which is necessary for handling
2977 	 * planes change such as added, removed, and updated.
2978 	 */
2979 	for (i = 0; i < set_count; i++) {
2980 		/* Check if stream is part of the delete list */
2981 		for (j = 0; j < del_streams_count; j++) {
2982 			if (set[i].stream == del_streams[j]) {
2983 				found = true;
2984 				break;
2985 			}
2986 		}
2987 
2988 		if (!found) {
2989 			/* Check if stream is part of the add list */
2990 			for (j = 0; j < add_streams_count; j++) {
2991 				if (set[i].stream == add_streams[j]) {
2992 					found = true;
2993 					break;
2994 				}
2995 			}
2996 		}
2997 
2998 		if (!found)
2999 			unchanged_streams[unchanged_streams_count++] = set[i].stream;
3000 
3001 		found = false;
3002 	}
3003 
3004 	/* Remove all planes for unchanged streams if planes changed */
3005 	for (i = 0; i < unchanged_streams_count; i++) {
3006 		if (planes_changed_for_existing_stream(context,
3007 						       unchanged_streams[i],
3008 						       set,
3009 						       set_count)) {
3010 			if (!dc_rem_all_planes_for_stream(dc,
3011 							  unchanged_streams[i],
3012 							  context)) {
3013 				res = DC_FAIL_DETACH_SURFACES;
3014 				goto fail;
3015 			}
3016 		}
3017 	}
3018 
3019 	/* Remove all planes for removed streams and then remove the streams */
3020 	for (i = 0; i < del_streams_count; i++) {
3021 		/* Need to cpy the dwb data from the old stream in order to efc to work */
3022 		if (del_streams[i]->num_wb_info > 0) {
3023 			for (j = 0; j < add_streams_count; j++) {
3024 				if (del_streams[i]->sink == add_streams[j]->sink) {
3025 					add_streams[j]->num_wb_info = del_streams[i]->num_wb_info;
3026 					for (k = 0; k < del_streams[i]->num_wb_info; k++)
3027 						add_streams[j]->writeback_info[k] = del_streams[i]->writeback_info[k];
3028 				}
3029 			}
3030 		}
3031 
3032 		if (!dc_rem_all_planes_for_stream(dc, del_streams[i], context)) {
3033 			res = DC_FAIL_DETACH_SURFACES;
3034 			goto fail;
3035 		}
3036 
3037 		res = dc_remove_stream_from_ctx(dc, context, del_streams[i]);
3038 		if (res != DC_OK)
3039 			goto fail;
3040 	}
3041 
3042 	/* Swap seamless boot stream to pipe 0 (if needed) to ensure pipe_ctx
3043 	 * matches. This may change in the future if seamless_boot_stream can be
3044 	 * multiple.
3045 	 */
3046 	for (i = 0; i < add_streams_count; i++) {
3047 		mark_seamless_boot_stream(dc, add_streams[i]);
3048 		if (add_streams[i]->apply_seamless_boot_optimization && i != 0) {
3049 			struct dc_stream_state *temp = add_streams[0];
3050 
3051 			add_streams[0] = add_streams[i];
3052 			add_streams[i] = temp;
3053 			break;
3054 		}
3055 	}
3056 
3057 	/* Add new streams and then add all planes for the new stream */
3058 	for (i = 0; i < add_streams_count; i++) {
3059 		calculate_phy_pix_clks(add_streams[i]);
3060 		res = dc_add_stream_to_ctx(dc, context, add_streams[i]);
3061 		if (res != DC_OK)
3062 			goto fail;
3063 
3064 		if (!add_all_planes_for_stream(dc, add_streams[i], set, set_count, context)) {
3065 			res = DC_FAIL_ATTACH_SURFACES;
3066 			goto fail;
3067 		}
3068 	}
3069 
3070 	/* Add all planes for unchanged streams if planes changed */
3071 	for (i = 0; i < unchanged_streams_count; i++) {
3072 		if (planes_changed_for_existing_stream(context,
3073 						       unchanged_streams[i],
3074 						       set,
3075 						       set_count)) {
3076 			if (!add_all_planes_for_stream(dc, unchanged_streams[i], set, set_count, context)) {
3077 				res = DC_FAIL_ATTACH_SURFACES;
3078 				goto fail;
3079 			}
3080 		}
3081 	}
3082 
3083 	res = dc_validate_global_state(dc, context, fast_validate);
3084 
3085 fail:
3086 	if (res != DC_OK)
3087 		DC_LOG_WARNING("%s:resource validation failed, dc_status:%d\n",
3088 			       __func__,
3089 			       res);
3090 
3091 	return res;
3092 }
3093 
3094 /**
3095  * dc_validate_global_state() - Determine if hardware can support a given state
3096  *
3097  * @dc: dc struct for this driver
3098  * @new_ctx: state to be validated
3099  * @fast_validate: set to true if only yes/no to support matters
3100  *
3101  * Checks hardware resource availability and bandwidth requirement.
3102  *
3103  * Return:
3104  * DC_OK if the result can be programmed. Otherwise, an error code.
3105  */
dc_validate_global_state(struct dc * dc,struct dc_state * new_ctx,bool fast_validate)3106 enum dc_status dc_validate_global_state(
3107 		struct dc *dc,
3108 		struct dc_state *new_ctx,
3109 		bool fast_validate)
3110 {
3111 	enum dc_status result = DC_ERROR_UNEXPECTED;
3112 	int i, j;
3113 
3114 	if (!new_ctx)
3115 		return DC_ERROR_UNEXPECTED;
3116 
3117 	if (dc->res_pool->funcs->validate_global) {
3118 		result = dc->res_pool->funcs->validate_global(dc, new_ctx);
3119 		if (result != DC_OK)
3120 			return result;
3121 	}
3122 
3123 	for (i = 0; i < new_ctx->stream_count; i++) {
3124 		struct dc_stream_state *stream = new_ctx->streams[i];
3125 
3126 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3127 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
3128 
3129 			if (pipe_ctx->stream != stream)
3130 				continue;
3131 
3132 			if (dc->res_pool->funcs->patch_unknown_plane_state &&
3133 					pipe_ctx->plane_state &&
3134 					pipe_ctx->plane_state->tiling_info.gfx9.swizzle == DC_SW_UNKNOWN) {
3135 				result = dc->res_pool->funcs->patch_unknown_plane_state(pipe_ctx->plane_state);
3136 				if (result != DC_OK)
3137 					return result;
3138 			}
3139 
3140 			/* Switch to dp clock source only if there is
3141 			 * no non dp stream that shares the same timing
3142 			 * with the dp stream.
3143 			 */
3144 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
3145 				!find_pll_sharable_stream(stream, new_ctx)) {
3146 
3147 				resource_unreference_clock_source(
3148 						&new_ctx->res_ctx,
3149 						dc->res_pool,
3150 						pipe_ctx->clock_source);
3151 
3152 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
3153 				resource_reference_clock_source(
3154 						&new_ctx->res_ctx,
3155 						dc->res_pool,
3156 						 pipe_ctx->clock_source);
3157 			}
3158 		}
3159 	}
3160 
3161 	result = resource_build_scaling_params_for_context(dc, new_ctx);
3162 
3163 	if (result == DC_OK)
3164 		if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx, fast_validate))
3165 			result = DC_FAIL_BANDWIDTH_VALIDATE;
3166 
3167 	/*
3168 	 * Only update link encoder to stream assignment after bandwidth validation passed.
3169 	 * TODO: Split out assignment and validation.
3170 	 */
3171 	if (result == DC_OK && dc->res_pool->funcs->link_encs_assign && fast_validate == false)
3172 		dc->res_pool->funcs->link_encs_assign(
3173 			dc, new_ctx, new_ctx->streams, new_ctx->stream_count);
3174 
3175 	return result;
3176 }
3177 
patch_gamut_packet_checksum(struct dc_info_packet * gamut_packet)3178 static void patch_gamut_packet_checksum(
3179 		struct dc_info_packet *gamut_packet)
3180 {
3181 	/* For gamut we recalc checksum */
3182 	if (gamut_packet->valid) {
3183 		uint8_t chk_sum = 0;
3184 		uint8_t *ptr;
3185 		uint8_t i;
3186 
3187 		/*start of the Gamut data. */
3188 		ptr = &gamut_packet->sb[3];
3189 
3190 		for (i = 0; i <= gamut_packet->sb[1]; i++)
3191 			chk_sum += ptr[i];
3192 
3193 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
3194 	}
3195 }
3196 
set_avi_info_frame(struct dc_info_packet * info_packet,struct pipe_ctx * pipe_ctx)3197 static void set_avi_info_frame(
3198 		struct dc_info_packet *info_packet,
3199 		struct pipe_ctx *pipe_ctx)
3200 {
3201 	struct dc_stream_state *stream = pipe_ctx->stream;
3202 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
3203 	uint32_t pixel_encoding = 0;
3204 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
3205 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
3206 	bool itc = false;
3207 	uint8_t itc_value = 0;
3208 	uint8_t cn0_cn1 = 0;
3209 	unsigned int cn0_cn1_value = 0;
3210 	uint8_t *check_sum = NULL;
3211 	uint8_t byte_index = 0;
3212 	union hdmi_info_packet hdmi_info;
3213 	union display_content_support support = {0};
3214 	unsigned int vic = pipe_ctx->stream->timing.vic;
3215 	unsigned int rid = pipe_ctx->stream->timing.rid;
3216 	unsigned int fr_ind = pipe_ctx->stream->timing.fr_index;
3217 	enum dc_timing_3d_format format;
3218 
3219 	memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
3220 
3221 	color_space = pipe_ctx->stream->output_color_space;
3222 	if (color_space == COLOR_SPACE_UNKNOWN)
3223 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
3224 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
3225 
3226 	/* Initialize header */
3227 	hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
3228 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
3229 	* not be used in HDMI 2.0 (Section 10.1) */
3230 	hdmi_info.bits.header.version = 2;
3231 	hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
3232 
3233 	/*
3234 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
3235 	 * according to HDMI 2.0 spec (Section 10.1)
3236 	 */
3237 
3238 	switch (stream->timing.pixel_encoding) {
3239 	case PIXEL_ENCODING_YCBCR422:
3240 		pixel_encoding = 1;
3241 		break;
3242 
3243 	case PIXEL_ENCODING_YCBCR444:
3244 		pixel_encoding = 2;
3245 		break;
3246 	case PIXEL_ENCODING_YCBCR420:
3247 		pixel_encoding = 3;
3248 		break;
3249 
3250 	case PIXEL_ENCODING_RGB:
3251 	default:
3252 		pixel_encoding = 0;
3253 	}
3254 
3255 	/* Y0_Y1_Y2 : The pixel encoding */
3256 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
3257 	hdmi_info.bits.Y0_Y1_Y2 = pixel_encoding;
3258 
3259 	/* A0 = 1 Active Format Information valid */
3260 	hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
3261 
3262 	/* B0, B1 = 3; Bar info data is valid */
3263 	hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
3264 
3265 	hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
3266 
3267 	/* S0, S1 : Underscan / Overscan */
3268 	/* TODO: un-hardcode scan type */
3269 	scan_type = SCANNING_TYPE_UNDERSCAN;
3270 	hdmi_info.bits.S0_S1 = scan_type;
3271 
3272 	/* C0, C1 : Colorimetry */
3273 	switch (color_space) {
3274 	case COLOR_SPACE_YCBCR709:
3275 	case COLOR_SPACE_YCBCR709_LIMITED:
3276 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
3277 		break;
3278 	case COLOR_SPACE_YCBCR601:
3279 	case COLOR_SPACE_YCBCR601_LIMITED:
3280 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
3281 		break;
3282 	case COLOR_SPACE_2020_RGB_FULLRANGE:
3283 	case COLOR_SPACE_2020_RGB_LIMITEDRANGE:
3284 	case COLOR_SPACE_2020_YCBCR:
3285 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
3286 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
3287 		break;
3288 	case COLOR_SPACE_ADOBERGB:
3289 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
3290 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
3291 		break;
3292 	case COLOR_SPACE_SRGB:
3293 	default:
3294 		hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
3295 		break;
3296 	}
3297 
3298 	if (pixel_encoding && color_space == COLOR_SPACE_2020_YCBCR &&
3299 			stream->out_transfer_func->tf == TRANSFER_FUNCTION_GAMMA22) {
3300 		hdmi_info.bits.EC0_EC2 = 0;
3301 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
3302 	}
3303 
3304 	/* TODO: un-hardcode aspect ratio */
3305 	aspect = stream->timing.aspect_ratio;
3306 
3307 	switch (aspect) {
3308 	case ASPECT_RATIO_4_3:
3309 	case ASPECT_RATIO_16_9:
3310 		hdmi_info.bits.M0_M1 = aspect;
3311 		break;
3312 
3313 	case ASPECT_RATIO_NO_DATA:
3314 	case ASPECT_RATIO_64_27:
3315 	case ASPECT_RATIO_256_135:
3316 	default:
3317 		hdmi_info.bits.M0_M1 = 0;
3318 	}
3319 
3320 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
3321 	hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
3322 
3323 	/* TODO: un-hardcode cn0_cn1 and itc */
3324 
3325 	cn0_cn1 = 0;
3326 	cn0_cn1_value = 0;
3327 
3328 	itc = true;
3329 	itc_value = 1;
3330 
3331 	support = stream->content_support;
3332 
3333 	if (itc) {
3334 		if (!support.bits.valid_content_type) {
3335 			cn0_cn1_value = 0;
3336 		} else {
3337 			if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GRAPHICS) {
3338 				if (support.bits.graphics_content == 1) {
3339 					cn0_cn1_value = 0;
3340 				}
3341 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_PHOTO) {
3342 				if (support.bits.photo_content == 1) {
3343 					cn0_cn1_value = 1;
3344 				} else {
3345 					cn0_cn1_value = 0;
3346 					itc_value = 0;
3347 				}
3348 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_CINEMA) {
3349 				if (support.bits.cinema_content == 1) {
3350 					cn0_cn1_value = 2;
3351 				} else {
3352 					cn0_cn1_value = 0;
3353 					itc_value = 0;
3354 				}
3355 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GAME) {
3356 				if (support.bits.game_content == 1) {
3357 					cn0_cn1_value = 3;
3358 				} else {
3359 					cn0_cn1_value = 0;
3360 					itc_value = 0;
3361 				}
3362 			}
3363 		}
3364 		hdmi_info.bits.CN0_CN1 = cn0_cn1_value;
3365 		hdmi_info.bits.ITC = itc_value;
3366 	}
3367 
3368 	if (stream->qs_bit == 1) {
3369 		if (color_space == COLOR_SPACE_SRGB ||
3370 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE)
3371 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
3372 		else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
3373 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
3374 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
3375 		else
3376 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
3377 	} else
3378 		hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
3379 
3380 	/* TODO : We should handle YCC quantization */
3381 	/* but we do not have matrix calculation */
3382 	hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
3383 
3384 	///VIC
3385 	if (pipe_ctx->stream->timing.hdmi_vic != 0)
3386 		vic = 0;
3387 	format = stream->timing.timing_3d_format;
3388 	/*todo, add 3DStereo support*/
3389 	if (format != TIMING_3D_FORMAT_NONE) {
3390 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
3391 		switch (pipe_ctx->stream->timing.hdmi_vic) {
3392 		case 1:
3393 			vic = 95;
3394 			break;
3395 		case 2:
3396 			vic = 94;
3397 			break;
3398 		case 3:
3399 			vic = 93;
3400 			break;
3401 		case 4:
3402 			vic = 98;
3403 			break;
3404 		default:
3405 			break;
3406 		}
3407 	}
3408 	/* If VIC >= 128, the Source shall use AVI InfoFrame Version 3*/
3409 	hdmi_info.bits.VIC0_VIC7 = vic;
3410 	if (vic >= 128)
3411 		hdmi_info.bits.header.version = 3;
3412 	/* If (C1, C0)=(1, 1) and (EC2, EC1, EC0)=(1, 1, 1),
3413 	 * the Source shall use 20 AVI InfoFrame Version 4
3414 	 */
3415 	if (hdmi_info.bits.C0_C1 == COLORIMETRY_EXTENDED &&
3416 			hdmi_info.bits.EC0_EC2 == COLORIMETRYEX_RESERVED) {
3417 		hdmi_info.bits.header.version = 4;
3418 		hdmi_info.bits.header.length = 14;
3419 	}
3420 
3421 	if (rid != 0 && fr_ind != 0) {
3422 		hdmi_info.bits.header.version = 5;
3423 		hdmi_info.bits.header.length = 15;
3424 
3425 		hdmi_info.bits.FR0_FR3 = fr_ind & 0xF;
3426 		hdmi_info.bits.FR4 = (fr_ind >> 4) & 0x1;
3427 		hdmi_info.bits.RID0_RID5 = rid;
3428 	}
3429 
3430 	/* pixel repetition
3431 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
3432 	 * repetition start from 1 */
3433 	hdmi_info.bits.PR0_PR3 = 0;
3434 
3435 	/* Bar Info
3436 	 * barTop:    Line Number of End of Top Bar.
3437 	 * barBottom: Line Number of Start of Bottom Bar.
3438 	 * barLeft:   Pixel Number of End of Left Bar.
3439 	 * barRight:  Pixel Number of Start of Right Bar. */
3440 	hdmi_info.bits.bar_top = stream->timing.v_border_top;
3441 	hdmi_info.bits.bar_bottom = (stream->timing.v_total
3442 			- stream->timing.v_border_bottom + 1);
3443 	hdmi_info.bits.bar_left  = stream->timing.h_border_left;
3444 	hdmi_info.bits.bar_right = (stream->timing.h_total
3445 			- stream->timing.h_border_right + 1);
3446 
3447     /* Additional Colorimetry Extension
3448      * Used in conduction with C0-C1 and EC0-EC2
3449      * 0 = DCI-P3 RGB (D65)
3450      * 1 = DCI-P3 RGB (theater)
3451      */
3452 	hdmi_info.bits.ACE0_ACE3 = 0;
3453 
3454 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
3455 	check_sum = &hdmi_info.packet_raw_data.sb[0];
3456 
3457 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + hdmi_info.bits.header.length + hdmi_info.bits.header.version;
3458 
3459 	for (byte_index = 1; byte_index <= hdmi_info.bits.header.length; byte_index++)
3460 		*check_sum += hdmi_info.packet_raw_data.sb[byte_index];
3461 
3462 	/* one byte complement */
3463 	*check_sum = (uint8_t) (0x100 - *check_sum);
3464 
3465 	/* Store in hw_path_mode */
3466 	info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
3467 	info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
3468 	info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
3469 
3470 	for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
3471 		info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
3472 
3473 	info_packet->valid = true;
3474 }
3475 
set_vendor_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3476 static void set_vendor_info_packet(
3477 		struct dc_info_packet *info_packet,
3478 		struct dc_stream_state *stream)
3479 {
3480 	/* SPD info packet for FreeSync */
3481 
3482 	/* Check if Freesync is supported. Return if false. If true,
3483 	 * set the corresponding bit in the info packet
3484 	 */
3485 	if (!stream->vsp_infopacket.valid)
3486 		return;
3487 
3488 	*info_packet = stream->vsp_infopacket;
3489 }
3490 
set_spd_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3491 static void set_spd_info_packet(
3492 		struct dc_info_packet *info_packet,
3493 		struct dc_stream_state *stream)
3494 {
3495 	/* SPD info packet for FreeSync */
3496 
3497 	/* Check if Freesync is supported. Return if false. If true,
3498 	 * set the corresponding bit in the info packet
3499 	 */
3500 	if (!stream->vrr_infopacket.valid)
3501 		return;
3502 
3503 	*info_packet = stream->vrr_infopacket;
3504 }
3505 
set_hdr_static_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3506 static void set_hdr_static_info_packet(
3507 		struct dc_info_packet *info_packet,
3508 		struct dc_stream_state *stream)
3509 {
3510 	/* HDR Static Metadata info packet for HDR10 */
3511 
3512 	if (!stream->hdr_static_metadata.valid ||
3513 			stream->use_dynamic_meta)
3514 		return;
3515 
3516 	*info_packet = stream->hdr_static_metadata;
3517 }
3518 
set_vsc_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3519 static void set_vsc_info_packet(
3520 		struct dc_info_packet *info_packet,
3521 		struct dc_stream_state *stream)
3522 {
3523 	if (!stream->vsc_infopacket.valid)
3524 		return;
3525 
3526 	*info_packet = stream->vsc_infopacket;
3527 }
set_hfvs_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3528 static void set_hfvs_info_packet(
3529 		struct dc_info_packet *info_packet,
3530 		struct dc_stream_state *stream)
3531 {
3532 	if (!stream->hfvsif_infopacket.valid)
3533 		return;
3534 
3535 	*info_packet = stream->hfvsif_infopacket;
3536 }
3537 
adaptive_sync_override_dp_info_packets_sdp_line_num(const struct dc_crtc_timing * timing,struct enc_sdp_line_num * sdp_line_num,struct _vcs_dpi_display_pipe_dest_params_st * pipe_dlg_param)3538 static void adaptive_sync_override_dp_info_packets_sdp_line_num(
3539 		const struct dc_crtc_timing *timing,
3540 		struct enc_sdp_line_num *sdp_line_num,
3541 		struct _vcs_dpi_display_pipe_dest_params_st *pipe_dlg_param)
3542 {
3543 	uint32_t asic_blank_start = 0;
3544 	uint32_t asic_blank_end   = 0;
3545 	uint32_t v_update = 0;
3546 
3547 	const struct dc_crtc_timing *tg = timing;
3548 
3549 	/* blank_start = frame end - front porch */
3550 	asic_blank_start = tg->v_total - tg->v_front_porch;
3551 
3552 	/* blank_end = blank_start - active */
3553 	asic_blank_end = (asic_blank_start - tg->v_border_bottom -
3554 						tg->v_addressable - tg->v_border_top);
3555 
3556 	if (pipe_dlg_param->vstartup_start > asic_blank_end) {
3557 		v_update = (tg->v_total - (pipe_dlg_param->vstartup_start - asic_blank_end));
3558 		sdp_line_num->adaptive_sync_line_num_valid = true;
3559 		sdp_line_num->adaptive_sync_line_num = (tg->v_total - v_update - 1);
3560 	} else {
3561 		sdp_line_num->adaptive_sync_line_num_valid = false;
3562 		sdp_line_num->adaptive_sync_line_num = 0;
3563 	}
3564 }
3565 
set_adaptive_sync_info_packet(struct dc_info_packet * info_packet,const struct dc_stream_state * stream,struct encoder_info_frame * info_frame,struct _vcs_dpi_display_pipe_dest_params_st * pipe_dlg_param)3566 static void set_adaptive_sync_info_packet(
3567 		struct dc_info_packet *info_packet,
3568 		const struct dc_stream_state *stream,
3569 		struct encoder_info_frame *info_frame,
3570 		struct _vcs_dpi_display_pipe_dest_params_st *pipe_dlg_param)
3571 {
3572 	if (!stream->adaptive_sync_infopacket.valid)
3573 		return;
3574 
3575 	adaptive_sync_override_dp_info_packets_sdp_line_num(
3576 			&stream->timing,
3577 			&info_frame->sdp_line_num,
3578 			pipe_dlg_param);
3579 
3580 	*info_packet = stream->adaptive_sync_infopacket;
3581 }
3582 
set_vtem_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3583 static void set_vtem_info_packet(
3584 		struct dc_info_packet *info_packet,
3585 		struct dc_stream_state *stream)
3586 {
3587 	if (!stream->vtem_infopacket.valid)
3588 		return;
3589 
3590 	*info_packet = stream->vtem_infopacket;
3591 }
3592 
dc_resource_state_destruct(struct dc_state * context)3593 void dc_resource_state_destruct(struct dc_state *context)
3594 {
3595 	int i, j;
3596 
3597 	for (i = 0; i < context->stream_count; i++) {
3598 		for (j = 0; j < context->stream_status[i].plane_count; j++)
3599 			dc_plane_state_release(
3600 				context->stream_status[i].plane_states[j]);
3601 
3602 		context->stream_status[i].plane_count = 0;
3603 		dc_stream_release(context->streams[i]);
3604 		context->streams[i] = NULL;
3605 	}
3606 	context->stream_count = 0;
3607 }
3608 
dc_resource_state_copy_construct(const struct dc_state * src_ctx,struct dc_state * dst_ctx)3609 void dc_resource_state_copy_construct(
3610 		const struct dc_state *src_ctx,
3611 		struct dc_state *dst_ctx)
3612 {
3613 	int i, j;
3614 	struct kref refcount = dst_ctx->refcount;
3615 
3616 	*dst_ctx = *src_ctx;
3617 
3618 	for (i = 0; i < MAX_PIPES; i++) {
3619 		struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
3620 
3621 		if (cur_pipe->top_pipe)
3622 			cur_pipe->top_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
3623 
3624 		if (cur_pipe->bottom_pipe)
3625 			cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
3626 
3627 		if (cur_pipe->next_odm_pipe)
3628 			cur_pipe->next_odm_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
3629 
3630 		if (cur_pipe->prev_odm_pipe)
3631 			cur_pipe->prev_odm_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
3632 	}
3633 
3634 	for (i = 0; i < dst_ctx->stream_count; i++) {
3635 		dc_stream_retain(dst_ctx->streams[i]);
3636 		for (j = 0; j < dst_ctx->stream_status[i].plane_count; j++)
3637 			dc_plane_state_retain(
3638 				dst_ctx->stream_status[i].plane_states[j]);
3639 	}
3640 
3641 	/* context refcount should not be overridden */
3642 	dst_ctx->refcount = refcount;
3643 
3644 }
3645 
dc_resource_find_first_free_pll(struct resource_context * res_ctx,const struct resource_pool * pool)3646 struct clock_source *dc_resource_find_first_free_pll(
3647 		struct resource_context *res_ctx,
3648 		const struct resource_pool *pool)
3649 {
3650 	int i;
3651 
3652 	for (i = 0; i < pool->clk_src_count; ++i) {
3653 		if (res_ctx->clock_source_ref_count[i] == 0)
3654 			return pool->clock_sources[i];
3655 	}
3656 
3657 	return NULL;
3658 }
3659 
resource_build_info_frame(struct pipe_ctx * pipe_ctx)3660 void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
3661 {
3662 	enum signal_type signal = SIGNAL_TYPE_NONE;
3663 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
3664 
3665 	/* default all packets to invalid */
3666 	info->avi.valid = false;
3667 	info->gamut.valid = false;
3668 	info->vendor.valid = false;
3669 	info->spd.valid = false;
3670 	info->hdrsmd.valid = false;
3671 	info->vsc.valid = false;
3672 	info->hfvsif.valid = false;
3673 	info->vtem.valid = false;
3674 	info->adaptive_sync.valid = false;
3675 	signal = pipe_ctx->stream->signal;
3676 
3677 	/* HDMi and DP have different info packets*/
3678 	if (dc_is_hdmi_signal(signal)) {
3679 		set_avi_info_frame(&info->avi, pipe_ctx);
3680 
3681 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
3682 		set_hfvs_info_packet(&info->hfvsif, pipe_ctx->stream);
3683 		set_vtem_info_packet(&info->vtem, pipe_ctx->stream);
3684 
3685 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
3686 
3687 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
3688 
3689 	} else if (dc_is_dp_signal(signal)) {
3690 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
3691 
3692 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
3693 
3694 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
3695 		set_adaptive_sync_info_packet(&info->adaptive_sync,
3696 										pipe_ctx->stream,
3697 										info,
3698 										&pipe_ctx->pipe_dlg_param);
3699 	}
3700 
3701 	patch_gamut_packet_checksum(&info->gamut);
3702 }
3703 
resource_map_clock_resources(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)3704 enum dc_status resource_map_clock_resources(
3705 		const struct dc  *dc,
3706 		struct dc_state *context,
3707 		struct dc_stream_state *stream)
3708 {
3709 	/* acquire new resources */
3710 	const struct resource_pool *pool = dc->res_pool;
3711 	struct pipe_ctx *pipe_ctx = resource_get_otg_master_for_stream(
3712 				&context->res_ctx, stream);
3713 
3714 	if (!pipe_ctx)
3715 		return DC_ERROR_UNEXPECTED;
3716 
3717 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
3718 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
3719 		pipe_ctx->clock_source = pool->dp_clock_source;
3720 	else {
3721 		pipe_ctx->clock_source = NULL;
3722 
3723 		if (!dc->config.disable_disp_pll_sharing)
3724 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
3725 				&context->res_ctx,
3726 				pipe_ctx);
3727 
3728 		if (pipe_ctx->clock_source == NULL)
3729 			pipe_ctx->clock_source =
3730 				dc_resource_find_first_free_pll(
3731 					&context->res_ctx,
3732 					pool);
3733 	}
3734 
3735 	if (pipe_ctx->clock_source == NULL)
3736 		return DC_NO_CLOCK_SOURCE_RESOURCE;
3737 
3738 	resource_reference_clock_source(
3739 		&context->res_ctx, pool,
3740 		pipe_ctx->clock_source);
3741 
3742 	return DC_OK;
3743 }
3744 
3745 /*
3746  * Note: We need to disable output if clock sources change,
3747  * since bios does optimization and doesn't apply if changing
3748  * PHY when not already disabled.
3749  */
pipe_need_reprogram(struct pipe_ctx * pipe_ctx_old,struct pipe_ctx * pipe_ctx)3750 bool pipe_need_reprogram(
3751 		struct pipe_ctx *pipe_ctx_old,
3752 		struct pipe_ctx *pipe_ctx)
3753 {
3754 	if (!pipe_ctx_old->stream)
3755 		return false;
3756 
3757 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
3758 		return true;
3759 
3760 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
3761 		return true;
3762 
3763 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
3764 		return true;
3765 
3766 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
3767 			&& pipe_ctx_old->stream != pipe_ctx->stream)
3768 		return true;
3769 
3770 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
3771 		return true;
3772 
3773 	if (dc_is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
3774 		return true;
3775 
3776 	if (pipe_ctx_old->stream->dpms_off != pipe_ctx->stream->dpms_off)
3777 		return true;
3778 
3779 	if (false == pipe_ctx_old->stream->link->link_state_valid &&
3780 		false == pipe_ctx_old->stream->dpms_off)
3781 		return true;
3782 
3783 	if (pipe_ctx_old->stream_res.dsc != pipe_ctx->stream_res.dsc)
3784 		return true;
3785 
3786 	if (pipe_ctx_old->stream_res.hpo_dp_stream_enc != pipe_ctx->stream_res.hpo_dp_stream_enc)
3787 		return true;
3788 	if (pipe_ctx_old->link_res.hpo_dp_link_enc != pipe_ctx->link_res.hpo_dp_link_enc)
3789 		return true;
3790 
3791 	/* DIG link encoder resource assignment for stream changed. */
3792 	if (pipe_ctx_old->stream->ctx->dc->res_pool->funcs->link_encs_assign) {
3793 		bool need_reprogram = false;
3794 		struct dc *dc = pipe_ctx_old->stream->ctx->dc;
3795 		struct link_encoder *link_enc_prev =
3796 			link_enc_cfg_get_link_enc_used_by_stream_current(dc, pipe_ctx_old->stream);
3797 
3798 		if (link_enc_prev != pipe_ctx->stream->link_enc)
3799 			need_reprogram = true;
3800 
3801 		return need_reprogram;
3802 	}
3803 
3804 	return false;
3805 }
3806 
resource_build_bit_depth_reduction_params(struct dc_stream_state * stream,struct bit_depth_reduction_params * fmt_bit_depth)3807 void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
3808 		struct bit_depth_reduction_params *fmt_bit_depth)
3809 {
3810 	enum dc_dither_option option = stream->dither_option;
3811 	enum dc_pixel_encoding pixel_encoding =
3812 			stream->timing.pixel_encoding;
3813 
3814 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
3815 
3816 	if (option == DITHER_OPTION_DEFAULT) {
3817 		switch (stream->timing.display_color_depth) {
3818 		case COLOR_DEPTH_666:
3819 			option = DITHER_OPTION_SPATIAL6;
3820 			break;
3821 		case COLOR_DEPTH_888:
3822 			option = DITHER_OPTION_SPATIAL8;
3823 			break;
3824 		case COLOR_DEPTH_101010:
3825 			option = DITHER_OPTION_SPATIAL10;
3826 			break;
3827 		default:
3828 			option = DITHER_OPTION_DISABLE;
3829 		}
3830 	}
3831 
3832 	if (option == DITHER_OPTION_DISABLE)
3833 		return;
3834 
3835 	if (option == DITHER_OPTION_TRUN6) {
3836 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
3837 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
3838 	} else if (option == DITHER_OPTION_TRUN8 ||
3839 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
3840 			option == DITHER_OPTION_TRUN8_FM6) {
3841 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
3842 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
3843 	} else if (option == DITHER_OPTION_TRUN10        ||
3844 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
3845 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
3846 			option == DITHER_OPTION_TRUN10_FM8     ||
3847 			option == DITHER_OPTION_TRUN10_FM6     ||
3848 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
3849 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
3850 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
3851 	}
3852 
3853 	/* special case - Formatter can only reduce by 4 bits at most.
3854 	 * When reducing from 12 to 6 bits,
3855 	 * HW recommends we use trunc with round mode
3856 	 * (if we did nothing, trunc to 10 bits would be used)
3857 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
3858 	 * as the input was 10 bits.
3859 	 */
3860 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
3861 			option == DITHER_OPTION_SPATIAL6 ||
3862 			option == DITHER_OPTION_FM6) {
3863 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
3864 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
3865 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
3866 	}
3867 
3868 	/* spatial dither
3869 	 * note that spatial modes 1-3 are never used
3870 	 */
3871 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
3872 			option == DITHER_OPTION_SPATIAL6 ||
3873 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
3874 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
3875 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
3876 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
3877 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
3878 		fmt_bit_depth->flags.RGB_RANDOM =
3879 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
3880 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
3881 			option == DITHER_OPTION_SPATIAL8 ||
3882 			option == DITHER_OPTION_SPATIAL8_FM6        ||
3883 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
3884 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
3885 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
3886 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
3887 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
3888 		fmt_bit_depth->flags.RGB_RANDOM =
3889 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
3890 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
3891 			option == DITHER_OPTION_SPATIAL10 ||
3892 			option == DITHER_OPTION_SPATIAL10_FM8 ||
3893 			option == DITHER_OPTION_SPATIAL10_FM6) {
3894 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
3895 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
3896 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
3897 		fmt_bit_depth->flags.RGB_RANDOM =
3898 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
3899 	}
3900 
3901 	if (option == DITHER_OPTION_SPATIAL6 ||
3902 			option == DITHER_OPTION_SPATIAL8 ||
3903 			option == DITHER_OPTION_SPATIAL10) {
3904 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
3905 	} else {
3906 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
3907 	}
3908 
3909 	//////////////////////
3910 	//// temporal dither
3911 	//////////////////////
3912 	if (option == DITHER_OPTION_FM6           ||
3913 			option == DITHER_OPTION_SPATIAL8_FM6     ||
3914 			option == DITHER_OPTION_SPATIAL10_FM6     ||
3915 			option == DITHER_OPTION_TRUN10_FM6     ||
3916 			option == DITHER_OPTION_TRUN8_FM6      ||
3917 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
3918 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
3919 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
3920 	} else if (option == DITHER_OPTION_FM8        ||
3921 			option == DITHER_OPTION_SPATIAL10_FM8  ||
3922 			option == DITHER_OPTION_TRUN10_FM8) {
3923 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
3924 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
3925 	} else if (option == DITHER_OPTION_FM10) {
3926 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
3927 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
3928 	}
3929 
3930 	fmt_bit_depth->pixel_encoding = pixel_encoding;
3931 }
3932 
dc_validate_stream(struct dc * dc,struct dc_stream_state * stream)3933 enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
3934 {
3935 	if (dc == NULL || stream == NULL)
3936 		return DC_ERROR_UNEXPECTED;
3937 
3938 	struct dc_link *link = stream->link;
3939 	struct timing_generator *tg = dc->res_pool->timing_generators[0];
3940 	enum dc_status res = DC_OK;
3941 
3942 	calculate_phy_pix_clks(stream);
3943 
3944 	if (!tg->funcs->validate_timing(tg, &stream->timing))
3945 		res = DC_FAIL_CONTROLLER_VALIDATE;
3946 
3947 	if (res == DC_OK) {
3948 		if (link->ep_type == DISPLAY_ENDPOINT_PHY &&
3949 				!link->link_enc->funcs->validate_output_with_stream(
3950 						link->link_enc, stream))
3951 			res = DC_FAIL_ENC_VALIDATE;
3952 	}
3953 
3954 	/* TODO: validate audio ASIC caps, encoder */
3955 
3956 	if (res == DC_OK)
3957 		res = dc->link_srv->validate_mode_timing(stream,
3958 		      link,
3959 		      &stream->timing);
3960 
3961 	return res;
3962 }
3963 
dc_validate_plane(struct dc * dc,const struct dc_plane_state * plane_state)3964 enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
3965 {
3966 	enum dc_status res = DC_OK;
3967 
3968 	/* check if surface has invalid dimensions */
3969 	if (plane_state->src_rect.width == 0 || plane_state->src_rect.height == 0 ||
3970 		plane_state->dst_rect.width == 0 || plane_state->dst_rect.height == 0)
3971 		return DC_FAIL_SURFACE_VALIDATE;
3972 
3973 	/* TODO For now validates pixel format only */
3974 	if (dc->res_pool->funcs->validate_plane)
3975 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
3976 
3977 	return res;
3978 }
3979 
resource_pixel_format_to_bpp(enum surface_pixel_format format)3980 unsigned int resource_pixel_format_to_bpp(enum surface_pixel_format format)
3981 {
3982 	switch (format) {
3983 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
3984 		return 8;
3985 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
3986 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
3987 		return 12;
3988 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
3989 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
3990 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
3991 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
3992 		return 16;
3993 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
3994 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
3995 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
3996 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
3997 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
3998 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE:
3999 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE_ALPHA:
4000 		return 32;
4001 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
4002 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
4003 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
4004 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
4005 		return 64;
4006 	default:
4007 		ASSERT_CRITICAL(false);
4008 		return -1;
4009 	}
4010 }
get_max_audio_sample_rate(struct audio_mode * modes)4011 static unsigned int get_max_audio_sample_rate(struct audio_mode *modes)
4012 {
4013 	if (modes) {
4014 		if (modes->sample_rates.rate.RATE_192)
4015 			return 192000;
4016 		if (modes->sample_rates.rate.RATE_176_4)
4017 			return 176400;
4018 		if (modes->sample_rates.rate.RATE_96)
4019 			return 96000;
4020 		if (modes->sample_rates.rate.RATE_88_2)
4021 			return 88200;
4022 		if (modes->sample_rates.rate.RATE_48)
4023 			return 48000;
4024 		if (modes->sample_rates.rate.RATE_44_1)
4025 			return 44100;
4026 		if (modes->sample_rates.rate.RATE_32)
4027 			return 32000;
4028 	}
4029 	/*original logic when no audio info*/
4030 	return 441000;
4031 }
4032 
get_audio_check(struct audio_info * aud_modes,struct audio_check * audio_chk)4033 void get_audio_check(struct audio_info *aud_modes,
4034 	struct audio_check *audio_chk)
4035 {
4036 	unsigned int i;
4037 	unsigned int max_sample_rate = 0;
4038 
4039 	if (aud_modes) {
4040 		audio_chk->audio_packet_type = 0x2;/*audio sample packet AP = .25 for layout0, 1 for layout1*/
4041 
4042 		audio_chk->max_audiosample_rate = 0;
4043 		for (i = 0; i < aud_modes->mode_count; i++) {
4044 			max_sample_rate = get_max_audio_sample_rate(&aud_modes->modes[i]);
4045 			if (audio_chk->max_audiosample_rate < max_sample_rate)
4046 				audio_chk->max_audiosample_rate = max_sample_rate;
4047 			/*dts takes the same as type 2: AP = 0.25*/
4048 		}
4049 		/*check which one take more bandwidth*/
4050 		if (audio_chk->max_audiosample_rate > 192000)
4051 			audio_chk->audio_packet_type = 0x9;/*AP =1*/
4052 		audio_chk->acat = 0;/*not support*/
4053 	}
4054 }
4055 
get_temp_hpo_dp_link_enc(const struct resource_context * res_ctx,const struct resource_pool * const pool,const struct dc_link * link)4056 static struct hpo_dp_link_encoder *get_temp_hpo_dp_link_enc(
4057 		const struct resource_context *res_ctx,
4058 		const struct resource_pool *const pool,
4059 		const struct dc_link *link)
4060 {
4061 	struct hpo_dp_link_encoder *hpo_dp_link_enc = NULL;
4062 	int enc_index;
4063 
4064 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, link);
4065 
4066 	if (enc_index < 0)
4067 		enc_index = find_free_hpo_dp_link_enc(res_ctx, pool);
4068 
4069 	if (enc_index >= 0)
4070 		hpo_dp_link_enc = pool->hpo_dp_link_enc[enc_index];
4071 
4072 	return hpo_dp_link_enc;
4073 }
4074 
get_temp_dp_link_res(struct dc_link * link,struct link_resource * link_res,struct dc_link_settings * link_settings)4075 bool get_temp_dp_link_res(struct dc_link *link,
4076 		struct link_resource *link_res,
4077 		struct dc_link_settings *link_settings)
4078 {
4079 	const struct dc *dc  = link->dc;
4080 	const struct resource_context *res_ctx = &dc->current_state->res_ctx;
4081 
4082 	memset(link_res, 0, sizeof(*link_res));
4083 
4084 	if (dc->link_srv->dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING) {
4085 		link_res->hpo_dp_link_enc = get_temp_hpo_dp_link_enc(res_ctx,
4086 				dc->res_pool, link);
4087 		if (!link_res->hpo_dp_link_enc)
4088 			return false;
4089 	}
4090 	return true;
4091 }
4092 
reset_syncd_pipes_from_disabled_pipes(struct dc * dc,struct dc_state * context)4093 void reset_syncd_pipes_from_disabled_pipes(struct dc *dc,
4094 		struct dc_state *context)
4095 {
4096 	int i, j;
4097 	struct pipe_ctx *pipe_ctx_old, *pipe_ctx, *pipe_ctx_syncd;
4098 
4099 	/* If pipe backend is reset, need to reset pipe syncd status */
4100 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4101 		pipe_ctx_old =	&dc->current_state->res_ctx.pipe_ctx[i];
4102 		pipe_ctx = &context->res_ctx.pipe_ctx[i];
4103 
4104 		if (!resource_is_pipe_type(pipe_ctx_old, OTG_MASTER))
4105 			continue;
4106 
4107 		if (!pipe_ctx->stream ||
4108 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
4109 
4110 			/* Reset all the syncd pipes from the disabled pipe */
4111 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4112 				pipe_ctx_syncd = &context->res_ctx.pipe_ctx[j];
4113 				if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_syncd) == pipe_ctx_old->pipe_idx) ||
4114 					!IS_PIPE_SYNCD_VALID(pipe_ctx_syncd))
4115 					SET_PIPE_SYNCD_TO_PIPE(pipe_ctx_syncd, j);
4116 			}
4117 		}
4118 	}
4119 }
4120 
check_syncd_pipes_for_disabled_master_pipe(struct dc * dc,struct dc_state * context,uint8_t disabled_master_pipe_idx)4121 void check_syncd_pipes_for_disabled_master_pipe(struct dc *dc,
4122 	struct dc_state *context,
4123 	uint8_t disabled_master_pipe_idx)
4124 {
4125 	int i;
4126 	struct pipe_ctx *pipe_ctx, *pipe_ctx_check;
4127 
4128 	pipe_ctx = &context->res_ctx.pipe_ctx[disabled_master_pipe_idx];
4129 	if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx) != disabled_master_pipe_idx) ||
4130 		!IS_PIPE_SYNCD_VALID(pipe_ctx))
4131 		SET_PIPE_SYNCD_TO_PIPE(pipe_ctx, disabled_master_pipe_idx);
4132 
4133 	/* for the pipe disabled, check if any slave pipe exists and assert */
4134 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4135 		pipe_ctx_check = &context->res_ctx.pipe_ctx[i];
4136 
4137 		if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_check) == disabled_master_pipe_idx) &&
4138 		    IS_PIPE_SYNCD_VALID(pipe_ctx_check) && (i != disabled_master_pipe_idx)) {
4139 			struct pipe_ctx *first_pipe = pipe_ctx_check;
4140 
4141 			while (first_pipe->prev_odm_pipe)
4142 				first_pipe = first_pipe->prev_odm_pipe;
4143 			/* When ODM combine is enabled, this case is expected. If the disabled pipe
4144 			 * is part of the ODM tree, then we should not print an error.
4145 			 * */
4146 			if (first_pipe->pipe_idx == disabled_master_pipe_idx)
4147 				continue;
4148 
4149 			DC_ERR("DC: Failure: pipe_idx[%d] syncd with disabled master pipe_idx[%d]\n",
4150 				   i, disabled_master_pipe_idx);
4151 		}
4152 	}
4153 }
4154 
reset_sync_context_for_pipe(const struct dc * dc,struct dc_state * context,uint8_t pipe_idx)4155 void reset_sync_context_for_pipe(const struct dc *dc,
4156 	struct dc_state *context,
4157 	uint8_t pipe_idx)
4158 {
4159 	int i;
4160 	struct pipe_ctx *pipe_ctx_reset;
4161 
4162 	/* reset the otg sync context for the pipe and its slave pipes if any */
4163 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4164 		pipe_ctx_reset = &context->res_ctx.pipe_ctx[i];
4165 
4166 		if (((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_reset) == pipe_idx) &&
4167 			IS_PIPE_SYNCD_VALID(pipe_ctx_reset)) || (i == pipe_idx))
4168 			SET_PIPE_SYNCD_TO_PIPE(pipe_ctx_reset, i);
4169 	}
4170 }
4171 
resource_transmitter_to_phy_idx(const struct dc * dc,enum transmitter transmitter)4172 uint8_t resource_transmitter_to_phy_idx(const struct dc *dc, enum transmitter transmitter)
4173 {
4174 	/* TODO - get transmitter to phy idx mapping from DMUB */
4175 	uint8_t phy_idx = transmitter - TRANSMITTER_UNIPHY_A;
4176 
4177 	if (dc->ctx->dce_version == DCN_VERSION_3_1 &&
4178 			dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0) {
4179 		switch (transmitter) {
4180 		case TRANSMITTER_UNIPHY_A:
4181 			phy_idx = 0;
4182 			break;
4183 		case TRANSMITTER_UNIPHY_B:
4184 			phy_idx = 1;
4185 			break;
4186 		case TRANSMITTER_UNIPHY_C:
4187 			phy_idx = 5;
4188 			break;
4189 		case TRANSMITTER_UNIPHY_D:
4190 			phy_idx = 6;
4191 			break;
4192 		case TRANSMITTER_UNIPHY_E:
4193 			phy_idx = 4;
4194 			break;
4195 		default:
4196 			phy_idx = 0;
4197 			break;
4198 		}
4199 	}
4200 
4201 	return phy_idx;
4202 }
4203 
get_link_hwss(const struct dc_link * link,const struct link_resource * link_res)4204 const struct link_hwss *get_link_hwss(const struct dc_link *link,
4205 		const struct link_resource *link_res)
4206 {
4207 	/* Link_hwss is only accessible by getter function instead of accessing
4208 	 * by pointers in dc with the intent to protect against breaking polymorphism.
4209 	 */
4210 	if (can_use_hpo_dp_link_hwss(link, link_res))
4211 		/* TODO: some assumes that if decided link settings is 128b/132b
4212 		 * channel coding format hpo_dp_link_enc should be used.
4213 		 * Others believe that if hpo_dp_link_enc is available in link
4214 		 * resource then hpo_dp_link_enc must be used. This bound between
4215 		 * hpo_dp_link_enc != NULL and decided link settings is loosely coupled
4216 		 * with a premise that both hpo_dp_link_enc pointer and decided link
4217 		 * settings are determined based on single policy function like
4218 		 * "decide_link_settings" from upper layer. This "convention"
4219 		 * cannot be maintained and enforced at current level.
4220 		 * Therefore a refactor is due so we can enforce a strong bound
4221 		 * between those two parameters at this level.
4222 		 *
4223 		 * To put it simple, we want to make enforcement at low level so that
4224 		 * we will not return link hwss if caller plans to do 8b/10b
4225 		 * with an hpo encoder. Or we can return a very dummy one that doesn't
4226 		 * do work for all functions
4227 		 */
4228 		return (requires_fixed_vs_pe_retimer_hpo_link_hwss(link) ?
4229 				get_hpo_fixed_vs_pe_retimer_dp_link_hwss() : get_hpo_dp_link_hwss());
4230 	else if (can_use_dpia_link_hwss(link, link_res))
4231 		return get_dpia_link_hwss();
4232 	else if (can_use_dio_link_hwss(link, link_res))
4233 		return (requires_fixed_vs_pe_retimer_dio_link_hwss(link)) ?
4234 				get_dio_fixed_vs_pe_retimer_link_hwss() : get_dio_link_hwss();
4235 	else
4236 		return get_virtual_link_hwss();
4237 }
4238 
is_h_timing_divisible_by_2(struct dc_stream_state * stream)4239 bool is_h_timing_divisible_by_2(struct dc_stream_state *stream)
4240 {
4241 	bool divisible = false;
4242 	uint16_t h_blank_start = 0;
4243 	uint16_t h_blank_end = 0;
4244 
4245 	if (stream) {
4246 		h_blank_start = stream->timing.h_total - stream->timing.h_front_porch;
4247 		h_blank_end = h_blank_start - stream->timing.h_addressable;
4248 
4249 		/* HTOTAL, Hblank start/end, and Hsync start/end all must be
4250 		 * divisible by 2 in order for the horizontal timing params
4251 		 * to be considered divisible by 2. Hsync start is always 0.
4252 		 */
4253 		divisible = (stream->timing.h_total % 2 == 0) &&
4254 				(h_blank_start % 2 == 0) &&
4255 				(h_blank_end % 2 == 0) &&
4256 				(stream->timing.h_sync_width % 2 == 0);
4257 	}
4258 	return divisible;
4259 }
4260 
dc_resource_acquire_secondary_pipe_for_mpc_odm(const struct dc * dc,struct dc_state * state,struct pipe_ctx * pri_pipe,struct pipe_ctx * sec_pipe,bool odm)4261 bool dc_resource_acquire_secondary_pipe_for_mpc_odm(
4262 		const struct dc *dc,
4263 		struct dc_state *state,
4264 		struct pipe_ctx *pri_pipe,
4265 		struct pipe_ctx *sec_pipe,
4266 		bool odm)
4267 {
4268 	int pipe_idx = sec_pipe->pipe_idx;
4269 	struct pipe_ctx *sec_top, *sec_bottom, *sec_next, *sec_prev;
4270 	const struct resource_pool *pool = dc->res_pool;
4271 
4272 	sec_top = sec_pipe->top_pipe;
4273 	sec_bottom = sec_pipe->bottom_pipe;
4274 	sec_next = sec_pipe->next_odm_pipe;
4275 	sec_prev = sec_pipe->prev_odm_pipe;
4276 
4277 	*sec_pipe = *pri_pipe;
4278 
4279 	sec_pipe->top_pipe = sec_top;
4280 	sec_pipe->bottom_pipe = sec_bottom;
4281 	sec_pipe->next_odm_pipe = sec_next;
4282 	sec_pipe->prev_odm_pipe = sec_prev;
4283 
4284 	sec_pipe->pipe_idx = pipe_idx;
4285 	sec_pipe->plane_res.mi = pool->mis[pipe_idx];
4286 	sec_pipe->plane_res.hubp = pool->hubps[pipe_idx];
4287 	sec_pipe->plane_res.ipp = pool->ipps[pipe_idx];
4288 	sec_pipe->plane_res.xfm = pool->transforms[pipe_idx];
4289 	sec_pipe->plane_res.dpp = pool->dpps[pipe_idx];
4290 	sec_pipe->plane_res.mpcc_inst = pool->dpps[pipe_idx]->inst;
4291 	sec_pipe->stream_res.dsc = NULL;
4292 	if (odm) {
4293 		if (!sec_pipe->top_pipe)
4294 			sec_pipe->stream_res.opp = pool->opps[pipe_idx];
4295 		else
4296 			sec_pipe->stream_res.opp = sec_pipe->top_pipe->stream_res.opp;
4297 		if (sec_pipe->stream->timing.flags.DSC == 1) {
4298 #if defined(CONFIG_DRM_AMD_DC_FP)
4299 			dcn20_acquire_dsc(dc, &state->res_ctx, &sec_pipe->stream_res.dsc, pipe_idx);
4300 #endif
4301 			ASSERT(sec_pipe->stream_res.dsc);
4302 			if (sec_pipe->stream_res.dsc == NULL)
4303 				return false;
4304 		}
4305 #if defined(CONFIG_DRM_AMD_DC_FP)
4306 		dcn20_build_mapped_resource(dc, state, sec_pipe->stream);
4307 #endif
4308 	}
4309 
4310 	return true;
4311 }
4312 
update_dp_encoder_resources_for_test_harness(const struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe_ctx)4313 enum dc_status update_dp_encoder_resources_for_test_harness(const struct dc *dc,
4314 		struct dc_state *context,
4315 		struct pipe_ctx *pipe_ctx)
4316 {
4317 	if (dc->link_srv->dp_get_encoding_format(&pipe_ctx->link_config.dp_link_settings) == DP_128b_132b_ENCODING) {
4318 		if (pipe_ctx->stream_res.hpo_dp_stream_enc == NULL) {
4319 			pipe_ctx->stream_res.hpo_dp_stream_enc =
4320 					find_first_free_match_hpo_dp_stream_enc_for_link(
4321 							&context->res_ctx, dc->res_pool, pipe_ctx->stream);
4322 
4323 			if (!pipe_ctx->stream_res.hpo_dp_stream_enc)
4324 				return DC_NO_STREAM_ENC_RESOURCE;
4325 
4326 			update_hpo_dp_stream_engine_usage(
4327 					&context->res_ctx, dc->res_pool,
4328 					pipe_ctx->stream_res.hpo_dp_stream_enc,
4329 					true);
4330 		}
4331 
4332 		if (pipe_ctx->link_res.hpo_dp_link_enc == NULL) {
4333 			if (!add_hpo_dp_link_enc_to_ctx(&context->res_ctx, dc->res_pool, pipe_ctx, pipe_ctx->stream))
4334 				return DC_NO_LINK_ENC_RESOURCE;
4335 		}
4336 	} else {
4337 		if (pipe_ctx->stream_res.hpo_dp_stream_enc) {
4338 			update_hpo_dp_stream_engine_usage(
4339 					&context->res_ctx, dc->res_pool,
4340 					pipe_ctx->stream_res.hpo_dp_stream_enc,
4341 					false);
4342 			pipe_ctx->stream_res.hpo_dp_stream_enc = NULL;
4343 		}
4344 		if (pipe_ctx->link_res.hpo_dp_link_enc)
4345 			remove_hpo_dp_link_enc_from_ctx(&context->res_ctx, pipe_ctx, pipe_ctx->stream);
4346 	}
4347 
4348 	return DC_OK;
4349 }
4350 
4351