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->dst_rect.width ||
1398 			!plane_state->dst_rect.height ||
1399 			!plane_state->src_rect.width ||
1400 			!plane_state->src_rect.height) {
1401 		ASSERT(0);
1402 		return false;
1403 	}
1404 
1405 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
1406 			pipe_ctx->plane_state->format);
1407 
1408 	/* Timing borders are part of vactive that we are also supposed to skip in addition
1409 	 * to any stream dst offset. Since dm logic assumes dst is in addressable
1410 	 * space we need to add the left and top borders to dst offsets temporarily.
1411 	 * TODO: fix in DM, stream dst is supposed to be in vactive
1412 	 */
1413 	pipe_ctx->stream->dst.x += timing->h_border_left;
1414 	pipe_ctx->stream->dst.y += timing->v_border_top;
1415 
1416 	/* Calculate H and V active size */
1417 	pipe_ctx->plane_res.scl_data.h_active = odm_slice_rec.width;
1418 	pipe_ctx->plane_res.scl_data.v_active = odm_slice_rec.height;
1419 
1420 	/* depends on h_active */
1421 	calculate_recout(pipe_ctx);
1422 	/* depends on pixel format */
1423 	calculate_scaling_ratios(pipe_ctx);
1424 	/* depends on scaling ratios and recout, does not calculate offset yet */
1425 	calculate_viewport_size(pipe_ctx);
1426 
1427 	if (!pipe_ctx->stream->ctx->dc->config.enable_windowed_mpo_odm) {
1428 		/* Stopgap for validation of ODM + MPO on one side of screen case */
1429 		if (pipe_ctx->plane_res.scl_data.viewport.height < 1 ||
1430 				pipe_ctx->plane_res.scl_data.viewport.width < 1)
1431 			return false;
1432 	}
1433 
1434 	/*
1435 	 * LB calculations depend on vp size, h/v_active and scaling ratios
1436 	 * Setting line buffer pixel depth to 24bpp yields banding
1437 	 * on certain displays, such as the Sharp 4k. 36bpp is needed
1438 	 * to support SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616 and
1439 	 * SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616 with actual > 10 bpc
1440 	 * precision on DCN display engines, but apparently not for DCE, as
1441 	 * far as testing on DCE-11.2 and DCE-8 showed. Various DCE parts have
1442 	 * problems: Carrizo with DCE_VERSION_11_0 does not like 36 bpp lb depth,
1443 	 * neither do DCE-8 at 4k resolution, or DCE-11.2 (broken identify pixel
1444 	 * passthrough). Therefore only use 36 bpp on DCN where it is actually needed.
1445 	 */
1446 	if (plane_state->ctx->dce_version > DCE_VERSION_MAX)
1447 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_36BPP;
1448 	else
1449 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
1450 
1451 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = plane_state->per_pixel_alpha;
1452 
1453 	if (pipe_ctx->plane_res.xfm != NULL)
1454 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1455 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1456 
1457 	if (pipe_ctx->plane_res.dpp != NULL)
1458 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1459 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1460 
1461 
1462 	if (!res) {
1463 		/* Try 24 bpp linebuffer */
1464 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
1465 
1466 		if (pipe_ctx->plane_res.xfm != NULL)
1467 			res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1468 					pipe_ctx->plane_res.xfm,
1469 					&pipe_ctx->plane_res.scl_data,
1470 					&plane_state->scaling_quality);
1471 
1472 		if (pipe_ctx->plane_res.dpp != NULL)
1473 			res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1474 					pipe_ctx->plane_res.dpp,
1475 					&pipe_ctx->plane_res.scl_data,
1476 					&plane_state->scaling_quality);
1477 	}
1478 
1479 	/*
1480 	 * Depends on recout, scaling ratios, h_active and taps
1481 	 * May need to re-check lb size after this in some obscure scenario
1482 	 */
1483 	if (res)
1484 		calculate_inits_and_viewports(pipe_ctx);
1485 
1486 	/*
1487 	 * Handle side by side and top bottom 3d recout offsets after vp calculation
1488 	 * since 3d is special and needs to calculate vp as if there is no recout offset
1489 	 * This may break with rotation, good thing we aren't mixing hw rotation and 3d
1490 	 */
1491 	if (pipe_ctx->top_pipe && pipe_ctx->top_pipe->plane_state == plane_state) {
1492 		ASSERT(plane_state->rotation == ROTATION_ANGLE_0 ||
1493 			(pipe_ctx->stream->view_format != VIEW_3D_FORMAT_TOP_AND_BOTTOM &&
1494 				pipe_ctx->stream->view_format != VIEW_3D_FORMAT_SIDE_BY_SIDE));
1495 		if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
1496 			pipe_ctx->plane_res.scl_data.recout.y += pipe_ctx->plane_res.scl_data.recout.height;
1497 		else if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
1498 			pipe_ctx->plane_res.scl_data.recout.x += pipe_ctx->plane_res.scl_data.recout.width;
1499 	}
1500 
1501 	/* Clamp minimum viewport size */
1502 	if (pipe_ctx->plane_res.scl_data.viewport.height < MIN_VIEWPORT_SIZE)
1503 		pipe_ctx->plane_res.scl_data.viewport.height = MIN_VIEWPORT_SIZE;
1504 	if (pipe_ctx->plane_res.scl_data.viewport.width < MIN_VIEWPORT_SIZE)
1505 		pipe_ctx->plane_res.scl_data.viewport.width = MIN_VIEWPORT_SIZE;
1506 
1507 
1508 	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"
1509 			"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",
1510 			__func__,
1511 			pipe_ctx->pipe_idx,
1512 			pipe_ctx->plane_res.scl_data.viewport.height,
1513 			pipe_ctx->plane_res.scl_data.viewport.width,
1514 			pipe_ctx->plane_res.scl_data.viewport.x,
1515 			pipe_ctx->plane_res.scl_data.viewport.y,
1516 			pipe_ctx->plane_res.scl_data.recout.height,
1517 			pipe_ctx->plane_res.scl_data.recout.width,
1518 			pipe_ctx->plane_res.scl_data.recout.x,
1519 			pipe_ctx->plane_res.scl_data.recout.y,
1520 			pipe_ctx->plane_res.scl_data.h_active,
1521 			pipe_ctx->plane_res.scl_data.v_active,
1522 			plane_state->src_rect.height,
1523 			plane_state->src_rect.width,
1524 			plane_state->src_rect.x,
1525 			plane_state->src_rect.y,
1526 			plane_state->dst_rect.height,
1527 			plane_state->dst_rect.width,
1528 			plane_state->dst_rect.x,
1529 			plane_state->dst_rect.y,
1530 			plane_state->clip_rect.height,
1531 			plane_state->clip_rect.width,
1532 			plane_state->clip_rect.x,
1533 			plane_state->clip_rect.y);
1534 
1535 	pipe_ctx->stream->dst.x -= timing->h_border_left;
1536 	pipe_ctx->stream->dst.y -= timing->v_border_top;
1537 
1538 	return res;
1539 }
1540 
1541 
resource_build_scaling_params_for_context(const struct dc * dc,struct dc_state * context)1542 enum dc_status resource_build_scaling_params_for_context(
1543 	const struct dc  *dc,
1544 	struct dc_state *context)
1545 {
1546 	int i;
1547 
1548 	for (i = 0; i < MAX_PIPES; i++) {
1549 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
1550 				context->res_ctx.pipe_ctx[i].stream != NULL)
1551 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1552 				return DC_FAIL_SCALING;
1553 	}
1554 
1555 	return DC_OK;
1556 }
1557 
resource_find_free_secondary_pipe_legacy(struct resource_context * res_ctx,const struct resource_pool * pool,const struct pipe_ctx * primary_pipe)1558 struct pipe_ctx *resource_find_free_secondary_pipe_legacy(
1559 		struct resource_context *res_ctx,
1560 		const struct resource_pool *pool,
1561 		const struct pipe_ctx *primary_pipe)
1562 {
1563 	int i;
1564 	struct pipe_ctx *secondary_pipe = NULL;
1565 
1566 	/*
1567 	 * We add a preferred pipe mapping to avoid the chance that
1568 	 * MPCCs already in use will need to be reassigned to other trees.
1569 	 * For example, if we went with the strict, assign backwards logic:
1570 	 *
1571 	 * (State 1)
1572 	 * Display A on, no surface, top pipe = 0
1573 	 * Display B on, no surface, top pipe = 1
1574 	 *
1575 	 * (State 2)
1576 	 * Display A on, no surface, top pipe = 0
1577 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 5
1578 	 *
1579 	 * (State 3)
1580 	 * Display A on, surface enable, top pipe = 0, bottom pipe = 5
1581 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1582 	 *
1583 	 * The state 2->3 transition requires remapping MPCC 5 from display B
1584 	 * to display A.
1585 	 *
1586 	 * However, with the preferred pipe logic, state 2 would look like:
1587 	 *
1588 	 * (State 2)
1589 	 * Display A on, no surface, top pipe = 0
1590 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1591 	 *
1592 	 * This would then cause 2->3 to not require remapping any MPCCs.
1593 	 */
1594 	if (primary_pipe) {
1595 		int preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
1596 		if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1597 			secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1598 			secondary_pipe->pipe_idx = preferred_pipe_idx;
1599 		}
1600 	}
1601 
1602 	/*
1603 	 * search backwards for the second pipe to keep pipe
1604 	 * assignment more consistent
1605 	 */
1606 	if (!secondary_pipe)
1607 		for (i = pool->pipe_count - 1; i >= 0; i--) {
1608 			if (res_ctx->pipe_ctx[i].stream == NULL) {
1609 				secondary_pipe = &res_ctx->pipe_ctx[i];
1610 				secondary_pipe->pipe_idx = i;
1611 				break;
1612 			}
1613 		}
1614 
1615 	return secondary_pipe;
1616 }
1617 
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)1618 int resource_find_free_pipe_used_in_cur_mpc_blending_tree(
1619 		const struct resource_context *cur_res_ctx,
1620 		struct resource_context *new_res_ctx,
1621 		const struct pipe_ctx *cur_opp_head)
1622 {
1623 	const struct pipe_ctx *cur_sec_dpp = cur_opp_head->bottom_pipe;
1624 	struct pipe_ctx *new_pipe;
1625 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1626 
1627 	while (cur_sec_dpp) {
1628 		/* find a free pipe used in current opp blend tree,
1629 		 * this is to avoid MPO pipe switching to different opp blending
1630 		 * tree
1631 		 */
1632 		new_pipe = &new_res_ctx->pipe_ctx[cur_sec_dpp->pipe_idx];
1633 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1634 			free_pipe_idx = cur_sec_dpp->pipe_idx;
1635 			break;
1636 		}
1637 		cur_sec_dpp = cur_sec_dpp->bottom_pipe;
1638 	}
1639 
1640 	return free_pipe_idx;
1641 }
1642 
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)1643 int recource_find_free_pipe_not_used_in_cur_res_ctx(
1644 		const struct resource_context *cur_res_ctx,
1645 		struct resource_context *new_res_ctx,
1646 		const struct resource_pool *pool)
1647 {
1648 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1649 	const struct pipe_ctx *new_pipe, *cur_pipe;
1650 	int i;
1651 
1652 	for (i = 0; i < pool->pipe_count; i++) {
1653 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1654 		new_pipe = &new_res_ctx->pipe_ctx[i];
1655 
1656 		if (resource_is_pipe_type(cur_pipe, FREE_PIPE) &&
1657 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1658 			free_pipe_idx = i;
1659 			break;
1660 		}
1661 	}
1662 
1663 	return free_pipe_idx;
1664 }
1665 
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)1666 int resource_find_free_pipe_used_as_cur_sec_dpp_in_mpcc_combine(
1667 		const struct resource_context *cur_res_ctx,
1668 		struct resource_context *new_res_ctx,
1669 		const struct resource_pool *pool)
1670 {
1671 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1672 	const struct pipe_ctx *new_pipe, *cur_pipe;
1673 	int i;
1674 
1675 	for (i = 0; i < pool->pipe_count; i++) {
1676 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1677 		new_pipe = &new_res_ctx->pipe_ctx[i];
1678 
1679 		if (resource_is_pipe_type(cur_pipe, DPP_PIPE) &&
1680 				!resource_is_pipe_type(cur_pipe, OPP_HEAD) &&
1681 				resource_is_for_mpcc_combine(cur_pipe) &&
1682 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1683 			free_pipe_idx = i;
1684 			break;
1685 		}
1686 	}
1687 
1688 	return free_pipe_idx;
1689 }
1690 
resource_find_any_free_pipe(struct resource_context * new_res_ctx,const struct resource_pool * pool)1691 int resource_find_any_free_pipe(struct resource_context *new_res_ctx,
1692 		const struct resource_pool *pool)
1693 {
1694 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1695 	const struct pipe_ctx *new_pipe;
1696 	int i;
1697 
1698 	for (i = 0; i < pool->pipe_count; i++) {
1699 		new_pipe = &new_res_ctx->pipe_ctx[i];
1700 
1701 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1702 			free_pipe_idx = i;
1703 			break;
1704 		}
1705 	}
1706 
1707 	return free_pipe_idx;
1708 }
1709 
resource_is_pipe_type(const struct pipe_ctx * pipe_ctx,enum pipe_type type)1710 bool resource_is_pipe_type(const struct pipe_ctx *pipe_ctx, enum pipe_type type)
1711 {
1712 #ifdef DBG
1713 	if (pipe_ctx->stream == NULL) {
1714 		/* a free pipe with dangling states */
1715 		ASSERT(!pipe_ctx->plane_state);
1716 		ASSERT(!pipe_ctx->prev_odm_pipe);
1717 		ASSERT(!pipe_ctx->next_odm_pipe);
1718 		ASSERT(!pipe_ctx->top_pipe);
1719 		ASSERT(!pipe_ctx->bottom_pipe);
1720 	} else if (pipe_ctx->top_pipe) {
1721 		/* a secondary DPP pipe must be signed to a plane */
1722 		ASSERT(pipe_ctx->plane_state)
1723 	}
1724 	/* Add more checks here to prevent corrupted pipe ctx. It is very hard
1725 	 * to debug this issue afterwards because we can't pinpoint the code
1726 	 * location causing inconsistent pipe context states.
1727 	 */
1728 #endif
1729 	switch (type) {
1730 	case OTG_MASTER:
1731 		return !pipe_ctx->prev_odm_pipe &&
1732 				!pipe_ctx->top_pipe &&
1733 				pipe_ctx->stream;
1734 	case OPP_HEAD:
1735 		return !pipe_ctx->top_pipe && pipe_ctx->stream;
1736 	case DPP_PIPE:
1737 		return pipe_ctx->plane_state && pipe_ctx->stream;
1738 	case FREE_PIPE:
1739 		return !pipe_ctx->plane_state && !pipe_ctx->stream;
1740 	default:
1741 		return false;
1742 	}
1743 }
1744 
resource_is_for_mpcc_combine(const struct pipe_ctx * pipe_ctx)1745 bool resource_is_for_mpcc_combine(const struct pipe_ctx *pipe_ctx)
1746 {
1747 	return resource_get_num_mpc_splits(pipe_ctx) > 0;
1748 }
1749 
resource_get_otg_master_for_stream(struct resource_context * res_ctx,struct dc_stream_state * stream)1750 struct pipe_ctx *resource_get_otg_master_for_stream(
1751 		struct resource_context *res_ctx,
1752 		struct dc_stream_state *stream)
1753 {
1754 	int i;
1755 
1756 	for (i = 0; i < MAX_PIPES; i++) {
1757 		if (res_ctx->pipe_ctx[i].stream == stream &&
1758 				resource_is_pipe_type(&res_ctx->pipe_ctx[i], OTG_MASTER))
1759 			return &res_ctx->pipe_ctx[i];
1760 	}
1761 	return NULL;
1762 }
1763 
resource_get_otg_master(const struct pipe_ctx * pipe_ctx)1764 struct pipe_ctx *resource_get_otg_master(const struct pipe_ctx *pipe_ctx)
1765 {
1766 	struct pipe_ctx *otg_master = resource_get_opp_head(pipe_ctx);
1767 
1768 	while (otg_master->prev_odm_pipe)
1769 		otg_master = otg_master->prev_odm_pipe;
1770 	return otg_master;
1771 }
1772 
resource_get_opp_head(const struct pipe_ctx * pipe_ctx)1773 struct pipe_ctx *resource_get_opp_head(const struct pipe_ctx *pipe_ctx)
1774 {
1775 	struct pipe_ctx *opp_head = (struct pipe_ctx *) pipe_ctx;
1776 
1777 	ASSERT(!resource_is_pipe_type(opp_head, FREE_PIPE));
1778 	while (opp_head->top_pipe)
1779 		opp_head = opp_head->top_pipe;
1780 	return opp_head;
1781 }
1782 
get_tail_pipe(struct pipe_ctx * head_pipe)1783 static struct pipe_ctx *get_tail_pipe(
1784 		struct pipe_ctx *head_pipe)
1785 {
1786 	struct pipe_ctx *tail_pipe = head_pipe->bottom_pipe;
1787 
1788 	while (tail_pipe) {
1789 		head_pipe = tail_pipe;
1790 		tail_pipe = tail_pipe->bottom_pipe;
1791 	}
1792 
1793 	return head_pipe;
1794 }
1795 
acquire_first_split_pipe(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)1796 static int acquire_first_split_pipe(
1797 		struct resource_context *res_ctx,
1798 		const struct resource_pool *pool,
1799 		struct dc_stream_state *stream)
1800 {
1801 	int i;
1802 
1803 	for (i = 0; i < pool->pipe_count; i++) {
1804 		struct pipe_ctx *split_pipe = &res_ctx->pipe_ctx[i];
1805 
1806 		if (split_pipe->top_pipe &&
1807 				split_pipe->top_pipe->plane_state == split_pipe->plane_state) {
1808 			split_pipe->top_pipe->bottom_pipe = split_pipe->bottom_pipe;
1809 			if (split_pipe->bottom_pipe)
1810 				split_pipe->bottom_pipe->top_pipe = split_pipe->top_pipe;
1811 
1812 			if (split_pipe->top_pipe->plane_state)
1813 				resource_build_scaling_params(split_pipe->top_pipe);
1814 
1815 			memset(split_pipe, 0, sizeof(*split_pipe));
1816 			split_pipe->stream_res.tg = pool->timing_generators[i];
1817 			split_pipe->plane_res.hubp = pool->hubps[i];
1818 			split_pipe->plane_res.ipp = pool->ipps[i];
1819 			split_pipe->plane_res.dpp = pool->dpps[i];
1820 			split_pipe->stream_res.opp = pool->opps[i];
1821 			split_pipe->plane_res.mpcc_inst = pool->dpps[i]->inst;
1822 			split_pipe->pipe_idx = i;
1823 
1824 			split_pipe->stream = stream;
1825 			return i;
1826 		}
1827 	}
1828 	return UNABLE_TO_SPLIT;
1829 }
1830 
add_plane_to_opp_head_pipes(struct pipe_ctx * otg_master_pipe,struct dc_plane_state * plane_state,struct dc_state * context)1831 static bool add_plane_to_opp_head_pipes(struct pipe_ctx *otg_master_pipe,
1832 		struct dc_plane_state *plane_state,
1833 		struct dc_state *context)
1834 {
1835 	struct pipe_ctx *opp_head_pipe = otg_master_pipe;
1836 
1837 	while (opp_head_pipe) {
1838 		if (opp_head_pipe->plane_state) {
1839 			ASSERT(0);
1840 			return false;
1841 		}
1842 		opp_head_pipe->plane_state = plane_state;
1843 		opp_head_pipe = opp_head_pipe->next_odm_pipe;
1844 	}
1845 
1846 	return true;
1847 }
1848 
insert_secondary_dpp_pipe_with_plane(struct pipe_ctx * opp_head_pipe,struct pipe_ctx * sec_pipe,struct dc_plane_state * plane_state)1849 static void insert_secondary_dpp_pipe_with_plane(struct pipe_ctx *opp_head_pipe,
1850 		struct pipe_ctx *sec_pipe, struct dc_plane_state *plane_state)
1851 {
1852 	struct pipe_ctx *tail_pipe = get_tail_pipe(opp_head_pipe);
1853 
1854 	tail_pipe->bottom_pipe = sec_pipe;
1855 	sec_pipe->top_pipe = tail_pipe;
1856 	if (tail_pipe->prev_odm_pipe) {
1857 		ASSERT(tail_pipe->prev_odm_pipe->bottom_pipe);
1858 		sec_pipe->prev_odm_pipe = tail_pipe->prev_odm_pipe->bottom_pipe;
1859 		tail_pipe->prev_odm_pipe->bottom_pipe->next_odm_pipe = sec_pipe;
1860 	}
1861 	sec_pipe->plane_state = plane_state;
1862 }
1863 
1864 /* for each opp head pipe of an otg master pipe, acquire a secondary dpp pipe
1865  * and add the plane. So the plane is added to all MPC blend trees associated
1866  * with the otg master pipe.
1867  */
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)1868 static bool acquire_secondary_dpp_pipes_and_add_plane(
1869 		struct pipe_ctx *otg_master_pipe,
1870 		struct dc_plane_state *plane_state,
1871 		struct dc_state *new_ctx,
1872 		struct dc_state *cur_ctx,
1873 		struct resource_pool *pool)
1874 {
1875 	struct pipe_ctx *opp_head_pipe, *sec_pipe;
1876 
1877 	if (!pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe)
1878 		return false;
1879 
1880 	opp_head_pipe = otg_master_pipe;
1881 	while (opp_head_pipe) {
1882 		sec_pipe = pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe(
1883 				cur_ctx,
1884 				new_ctx,
1885 				pool,
1886 				opp_head_pipe);
1887 		if (!sec_pipe) {
1888 			/* try tearing down MPCC combine */
1889 			int pipe_idx = acquire_first_split_pipe(
1890 					&new_ctx->res_ctx, pool,
1891 					otg_master_pipe->stream);
1892 
1893 			if (pipe_idx >= 0)
1894 				sec_pipe = &new_ctx->res_ctx.pipe_ctx[pipe_idx];
1895 		}
1896 
1897 		if (!sec_pipe)
1898 			return false;
1899 
1900 		insert_secondary_dpp_pipe_with_plane(opp_head_pipe, sec_pipe,
1901 				plane_state);
1902 		opp_head_pipe = opp_head_pipe->next_odm_pipe;
1903 	}
1904 	return true;
1905 }
1906 
dc_add_plane_to_context(const struct dc * dc,struct dc_stream_state * stream,struct dc_plane_state * plane_state,struct dc_state * context)1907 bool dc_add_plane_to_context(
1908 		const struct dc *dc,
1909 		struct dc_stream_state *stream,
1910 		struct dc_plane_state *plane_state,
1911 		struct dc_state *context)
1912 {
1913 	struct resource_pool *pool = dc->res_pool;
1914 	struct pipe_ctx *otg_master_pipe;
1915 	struct dc_stream_status *stream_status = NULL;
1916 	bool added = false;
1917 
1918 	stream_status = dc_stream_get_status_from_state(context, stream);
1919 	if (stream_status == NULL) {
1920 		dm_error("Existing stream not found; failed to attach surface!\n");
1921 		goto out;
1922 	} else if (stream_status->plane_count == MAX_SURFACE_NUM) {
1923 		dm_error("Surface: can not attach plane_state %p! Maximum is: %d\n",
1924 				plane_state, MAX_SURFACE_NUM);
1925 		goto out;
1926 	}
1927 
1928 	otg_master_pipe = resource_get_otg_master_for_stream(
1929 			&context->res_ctx, stream);
1930 	if (otg_master_pipe->plane_state == NULL)
1931 		added = add_plane_to_opp_head_pipes(otg_master_pipe,
1932 				plane_state, context);
1933 	else
1934 		added = acquire_secondary_dpp_pipes_and_add_plane(
1935 				otg_master_pipe, plane_state, context,
1936 				dc->current_state, pool);
1937 	if (added) {
1938 		stream_status->plane_states[stream_status->plane_count] =
1939 				plane_state;
1940 		stream_status->plane_count++;
1941 		dc_plane_state_retain(plane_state);
1942 	}
1943 
1944 out:
1945 	return added;
1946 }
1947 
dc_remove_plane_from_context(const struct dc * dc,struct dc_stream_state * stream,struct dc_plane_state * plane_state,struct dc_state * context)1948 bool dc_remove_plane_from_context(
1949 		const struct dc *dc,
1950 		struct dc_stream_state *stream,
1951 		struct dc_plane_state *plane_state,
1952 		struct dc_state *context)
1953 {
1954 	int i;
1955 	struct dc_stream_status *stream_status = NULL;
1956 	struct resource_pool *pool = dc->res_pool;
1957 
1958 	if (!plane_state)
1959 		return true;
1960 
1961 	for (i = 0; i < context->stream_count; i++)
1962 		if (context->streams[i] == stream) {
1963 			stream_status = &context->stream_status[i];
1964 			break;
1965 		}
1966 
1967 	if (stream_status == NULL) {
1968 		dm_error("Existing stream not found; failed to remove plane.\n");
1969 		return false;
1970 	}
1971 
1972 	/* release pipe for plane*/
1973 	for (i = pool->pipe_count - 1; i >= 0; i--) {
1974 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
1975 
1976 		if (pipe_ctx->plane_state == plane_state) {
1977 			if (pipe_ctx->top_pipe)
1978 				pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
1979 
1980 			/* Second condition is to avoid setting NULL to top pipe
1981 			 * of tail pipe making it look like head pipe in subsequent
1982 			 * deletes
1983 			 */
1984 			if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
1985 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
1986 
1987 			/*
1988 			 * For head pipe detach surfaces from pipe for tail
1989 			 * pipe just zero it out
1990 			 */
1991 			if (!pipe_ctx->top_pipe)
1992 				pipe_ctx->plane_state = NULL;
1993 			else
1994 				memset(pipe_ctx, 0, sizeof(*pipe_ctx));
1995 		}
1996 	}
1997 
1998 
1999 	for (i = 0; i < stream_status->plane_count; i++) {
2000 		if (stream_status->plane_states[i] == plane_state) {
2001 			dc_plane_state_release(stream_status->plane_states[i]);
2002 			break;
2003 		}
2004 	}
2005 
2006 	if (i == stream_status->plane_count) {
2007 		dm_error("Existing plane_state not found; failed to detach it!\n");
2008 		return false;
2009 	}
2010 
2011 	stream_status->plane_count--;
2012 
2013 	/* Start at the plane we've just released, and move all the planes one index forward to "trim" the array */
2014 	for (; i < stream_status->plane_count; i++)
2015 		stream_status->plane_states[i] = stream_status->plane_states[i + 1];
2016 
2017 	stream_status->plane_states[stream_status->plane_count] = NULL;
2018 
2019 	return true;
2020 }
2021 
2022 /**
2023  * dc_rem_all_planes_for_stream - Remove planes attached to the target stream.
2024  *
2025  * @dc: Current dc state.
2026  * @stream: Target stream, which we want to remove the attached plans.
2027  * @context: New context.
2028  *
2029  * Return:
2030  * Return true if DC was able to remove all planes from the target
2031  * stream, otherwise, return false.
2032  */
dc_rem_all_planes_for_stream(const struct dc * dc,struct dc_stream_state * stream,struct dc_state * context)2033 bool dc_rem_all_planes_for_stream(
2034 		const struct dc *dc,
2035 		struct dc_stream_state *stream,
2036 		struct dc_state *context)
2037 {
2038 	int i, old_plane_count;
2039 	struct dc_stream_status *stream_status = NULL;
2040 	struct dc_plane_state *del_planes[MAX_SURFACE_NUM] = { 0 };
2041 
2042 	for (i = 0; i < context->stream_count; i++)
2043 			if (context->streams[i] == stream) {
2044 				stream_status = &context->stream_status[i];
2045 				break;
2046 			}
2047 
2048 	if (stream_status == NULL) {
2049 		dm_error("Existing stream %p not found!\n", stream);
2050 		return false;
2051 	}
2052 
2053 	old_plane_count = stream_status->plane_count;
2054 
2055 	for (i = 0; i < old_plane_count; i++)
2056 		del_planes[i] = stream_status->plane_states[i];
2057 
2058 	for (i = 0; i < old_plane_count; i++)
2059 		if (!dc_remove_plane_from_context(dc, stream, del_planes[i], context))
2060 			return false;
2061 
2062 	return true;
2063 }
2064 
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)2065 static bool add_all_planes_for_stream(
2066 		const struct dc *dc,
2067 		struct dc_stream_state *stream,
2068 		const struct dc_validation_set set[],
2069 		int set_count,
2070 		struct dc_state *context)
2071 {
2072 	int i, j;
2073 
2074 	for (i = 0; i < set_count; i++)
2075 		if (set[i].stream == stream)
2076 			break;
2077 
2078 	if (i == set_count) {
2079 		dm_error("Stream %p not found in set!\n", stream);
2080 		return false;
2081 	}
2082 
2083 	for (j = 0; j < set[i].plane_count; j++)
2084 		if (!dc_add_plane_to_context(dc, stream, set[i].plane_states[j], context))
2085 			return false;
2086 
2087 	return true;
2088 }
2089 
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)2090 bool dc_add_all_planes_for_stream(
2091 		const struct dc *dc,
2092 		struct dc_stream_state *stream,
2093 		struct dc_plane_state * const *plane_states,
2094 		int plane_count,
2095 		struct dc_state *context)
2096 {
2097 	struct dc_validation_set set;
2098 	int i;
2099 
2100 	set.stream = stream;
2101 	set.plane_count = plane_count;
2102 
2103 	for (i = 0; i < plane_count; i++)
2104 		set.plane_states[i] = plane_states[i];
2105 
2106 	return add_all_planes_for_stream(dc, stream, &set, 1, context);
2107 }
2108 
dc_is_timing_changed(struct dc_stream_state * cur_stream,struct dc_stream_state * new_stream)2109 bool dc_is_timing_changed(struct dc_stream_state *cur_stream,
2110 		       struct dc_stream_state *new_stream)
2111 {
2112 	if (cur_stream == NULL)
2113 		return true;
2114 
2115 	/* If output color space is changed, need to reprogram info frames */
2116 	if (cur_stream->output_color_space != new_stream->output_color_space)
2117 		return true;
2118 
2119 	return memcmp(
2120 		&cur_stream->timing,
2121 		&new_stream->timing,
2122 		sizeof(struct dc_crtc_timing)) != 0;
2123 }
2124 
are_stream_backends_same(struct dc_stream_state * stream_a,struct dc_stream_state * stream_b)2125 static bool are_stream_backends_same(
2126 	struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
2127 {
2128 	if (stream_a == stream_b)
2129 		return true;
2130 
2131 	if (stream_a == NULL || stream_b == NULL)
2132 		return false;
2133 
2134 	if (dc_is_timing_changed(stream_a, stream_b))
2135 		return false;
2136 
2137 	if (stream_a->signal != stream_b->signal)
2138 		return false;
2139 
2140 	if (stream_a->dpms_off != stream_b->dpms_off)
2141 		return false;
2142 
2143 	return true;
2144 }
2145 
2146 /*
2147  * dc_is_stream_unchanged() - Compare two stream states for equivalence.
2148  *
2149  * Checks if there a difference between the two states
2150  * that would require a mode change.
2151  *
2152  * Does not compare cursor position or attributes.
2153  */
dc_is_stream_unchanged(struct dc_stream_state * old_stream,struct dc_stream_state * stream)2154 bool dc_is_stream_unchanged(
2155 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
2156 {
2157 
2158 	if (!are_stream_backends_same(old_stream, stream))
2159 		return false;
2160 
2161 	if (old_stream->ignore_msa_timing_param != stream->ignore_msa_timing_param)
2162 		return false;
2163 
2164 	/*compare audio info*/
2165 	if (memcmp(&old_stream->audio_info, &stream->audio_info, sizeof(stream->audio_info)) != 0)
2166 		return false;
2167 
2168 	return true;
2169 }
2170 
2171 /*
2172  * dc_is_stream_scaling_unchanged() - Compare scaling rectangles of two streams.
2173  */
dc_is_stream_scaling_unchanged(struct dc_stream_state * old_stream,struct dc_stream_state * stream)2174 bool dc_is_stream_scaling_unchanged(struct dc_stream_state *old_stream,
2175 				    struct dc_stream_state *stream)
2176 {
2177 	if (old_stream == stream)
2178 		return true;
2179 
2180 	if (old_stream == NULL || stream == NULL)
2181 		return false;
2182 
2183 	if (memcmp(&old_stream->src,
2184 			&stream->src,
2185 			sizeof(struct rect)) != 0)
2186 		return false;
2187 
2188 	if (memcmp(&old_stream->dst,
2189 			&stream->dst,
2190 			sizeof(struct rect)) != 0)
2191 		return false;
2192 
2193 	return true;
2194 }
2195 
update_stream_engine_usage(struct resource_context * res_ctx,const struct resource_pool * pool,struct stream_encoder * stream_enc,bool acquired)2196 static void update_stream_engine_usage(
2197 		struct resource_context *res_ctx,
2198 		const struct resource_pool *pool,
2199 		struct stream_encoder *stream_enc,
2200 		bool acquired)
2201 {
2202 	int i;
2203 
2204 	for (i = 0; i < pool->stream_enc_count; i++) {
2205 		if (pool->stream_enc[i] == stream_enc)
2206 			res_ctx->is_stream_enc_acquired[i] = acquired;
2207 	}
2208 }
2209 
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)2210 static void update_hpo_dp_stream_engine_usage(
2211 		struct resource_context *res_ctx,
2212 		const struct resource_pool *pool,
2213 		struct hpo_dp_stream_encoder *hpo_dp_stream_enc,
2214 		bool acquired)
2215 {
2216 	int i;
2217 
2218 	for (i = 0; i < pool->hpo_dp_stream_enc_count; i++) {
2219 		if (pool->hpo_dp_stream_enc[i] == hpo_dp_stream_enc)
2220 			res_ctx->is_hpo_dp_stream_enc_acquired[i] = acquired;
2221 	}
2222 }
2223 
find_acquired_hpo_dp_link_enc_for_link(const struct resource_context * res_ctx,const struct dc_link * link)2224 static inline int find_acquired_hpo_dp_link_enc_for_link(
2225 		const struct resource_context *res_ctx,
2226 		const struct dc_link *link)
2227 {
2228 	int i;
2229 
2230 	for (i = 0; i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_to_link_idx); i++)
2231 		if (res_ctx->hpo_dp_link_enc_ref_cnts[i] > 0 &&
2232 				res_ctx->hpo_dp_link_enc_to_link_idx[i] == link->link_index)
2233 			return i;
2234 
2235 	return -1;
2236 }
2237 
find_free_hpo_dp_link_enc(const struct resource_context * res_ctx,const struct resource_pool * pool)2238 static inline int find_free_hpo_dp_link_enc(const struct resource_context *res_ctx,
2239 		const struct resource_pool *pool)
2240 {
2241 	int i;
2242 
2243 	for (i = 0; i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_ref_cnts); i++)
2244 		if (res_ctx->hpo_dp_link_enc_ref_cnts[i] == 0)
2245 			break;
2246 
2247 	return (i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_ref_cnts) &&
2248 			i < pool->hpo_dp_link_enc_count) ? i : -1;
2249 }
2250 
acquire_hpo_dp_link_enc(struct resource_context * res_ctx,unsigned int link_index,int enc_index)2251 static inline void acquire_hpo_dp_link_enc(
2252 		struct resource_context *res_ctx,
2253 		unsigned int link_index,
2254 		int enc_index)
2255 {
2256 	res_ctx->hpo_dp_link_enc_to_link_idx[enc_index] = link_index;
2257 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index] = 1;
2258 }
2259 
retain_hpo_dp_link_enc(struct resource_context * res_ctx,int enc_index)2260 static inline void retain_hpo_dp_link_enc(
2261 		struct resource_context *res_ctx,
2262 		int enc_index)
2263 {
2264 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index]++;
2265 }
2266 
release_hpo_dp_link_enc(struct resource_context * res_ctx,int enc_index)2267 static inline void release_hpo_dp_link_enc(
2268 		struct resource_context *res_ctx,
2269 		int enc_index)
2270 {
2271 	ASSERT(res_ctx->hpo_dp_link_enc_ref_cnts[enc_index] > 0);
2272 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index]--;
2273 }
2274 
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)2275 static bool add_hpo_dp_link_enc_to_ctx(struct resource_context *res_ctx,
2276 		const struct resource_pool *pool,
2277 		struct pipe_ctx *pipe_ctx,
2278 		struct dc_stream_state *stream)
2279 {
2280 	int enc_index;
2281 
2282 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, stream->link);
2283 
2284 	if (enc_index >= 0) {
2285 		retain_hpo_dp_link_enc(res_ctx, enc_index);
2286 	} else {
2287 		enc_index = find_free_hpo_dp_link_enc(res_ctx, pool);
2288 		if (enc_index >= 0)
2289 			acquire_hpo_dp_link_enc(res_ctx, stream->link->link_index, enc_index);
2290 	}
2291 
2292 	if (enc_index >= 0)
2293 		pipe_ctx->link_res.hpo_dp_link_enc = pool->hpo_dp_link_enc[enc_index];
2294 
2295 	return pipe_ctx->link_res.hpo_dp_link_enc != NULL;
2296 }
2297 
remove_hpo_dp_link_enc_from_ctx(struct resource_context * res_ctx,struct pipe_ctx * pipe_ctx,struct dc_stream_state * stream)2298 static void remove_hpo_dp_link_enc_from_ctx(struct resource_context *res_ctx,
2299 		struct pipe_ctx *pipe_ctx,
2300 		struct dc_stream_state *stream)
2301 {
2302 	int enc_index;
2303 
2304 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, stream->link);
2305 
2306 	if (enc_index >= 0) {
2307 		release_hpo_dp_link_enc(res_ctx, enc_index);
2308 		pipe_ctx->link_res.hpo_dp_link_enc = NULL;
2309 	}
2310 }
2311 
2312 /* TODO: release audio object */
update_audio_usage(struct resource_context * res_ctx,const struct resource_pool * pool,struct audio * audio,bool acquired)2313 void update_audio_usage(
2314 		struct resource_context *res_ctx,
2315 		const struct resource_pool *pool,
2316 		struct audio *audio,
2317 		bool acquired)
2318 {
2319 	int i;
2320 	for (i = 0; i < pool->audio_count; i++) {
2321 		if (pool->audios[i] == audio)
2322 			res_ctx->is_audio_acquired[i] = acquired;
2323 	}
2324 }
2325 
acquire_first_free_pipe(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)2326 static int acquire_first_free_pipe(
2327 		struct resource_context *res_ctx,
2328 		const struct resource_pool *pool,
2329 		struct dc_stream_state *stream)
2330 {
2331 	int i;
2332 
2333 	for (i = 0; i < pool->pipe_count; i++) {
2334 		if (!res_ctx->pipe_ctx[i].stream) {
2335 			struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
2336 
2337 			pipe_ctx->stream_res.tg = pool->timing_generators[i];
2338 			pipe_ctx->plane_res.mi = pool->mis[i];
2339 			pipe_ctx->plane_res.hubp = pool->hubps[i];
2340 			pipe_ctx->plane_res.ipp = pool->ipps[i];
2341 			pipe_ctx->plane_res.xfm = pool->transforms[i];
2342 			pipe_ctx->plane_res.dpp = pool->dpps[i];
2343 			pipe_ctx->stream_res.opp = pool->opps[i];
2344 			if (pool->dpps[i])
2345 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[i]->inst;
2346 			pipe_ctx->pipe_idx = i;
2347 
2348 			if (i >= pool->timing_generator_count) {
2349 				int tg_inst = pool->timing_generator_count - 1;
2350 
2351 				pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
2352 				pipe_ctx->stream_res.opp = pool->opps[tg_inst];
2353 			}
2354 
2355 			pipe_ctx->stream = stream;
2356 			return i;
2357 		}
2358 	}
2359 	return -1;
2360 }
2361 
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)2362 static struct hpo_dp_stream_encoder *find_first_free_match_hpo_dp_stream_enc_for_link(
2363 		struct resource_context *res_ctx,
2364 		const struct resource_pool *pool,
2365 		struct dc_stream_state *stream)
2366 {
2367 	int i;
2368 
2369 	for (i = 0; i < pool->hpo_dp_stream_enc_count; i++) {
2370 		if (!res_ctx->is_hpo_dp_stream_enc_acquired[i] &&
2371 				pool->hpo_dp_stream_enc[i]) {
2372 
2373 			return pool->hpo_dp_stream_enc[i];
2374 		}
2375 	}
2376 
2377 	return NULL;
2378 }
2379 
find_first_free_audio(struct resource_context * res_ctx,const struct resource_pool * pool,enum engine_id id,enum dce_version dc_version)2380 static struct audio *find_first_free_audio(
2381 		struct resource_context *res_ctx,
2382 		const struct resource_pool *pool,
2383 		enum engine_id id,
2384 		enum dce_version dc_version)
2385 {
2386 	int i, available_audio_count;
2387 
2388 	if (id == ENGINE_ID_UNKNOWN)
2389 		return NULL;
2390 
2391 	available_audio_count = pool->audio_count;
2392 
2393 	for (i = 0; i < available_audio_count; i++) {
2394 		if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
2395 			/*we have enough audio endpoint, find the matching inst*/
2396 			if (id != i)
2397 				continue;
2398 			return pool->audios[i];
2399 		}
2400 	}
2401 
2402 	/* use engine id to find free audio */
2403 	if ((id < available_audio_count) && (res_ctx->is_audio_acquired[id] == false)) {
2404 		return pool->audios[id];
2405 	}
2406 	/*not found the matching one, first come first serve*/
2407 	for (i = 0; i < available_audio_count; i++) {
2408 		if (res_ctx->is_audio_acquired[i] == false) {
2409 			return pool->audios[i];
2410 		}
2411 	}
2412 	return NULL;
2413 }
2414 
2415 /*
2416  * dc_add_stream_to_ctx() - Add a new dc_stream_state to a dc_state.
2417  */
dc_add_stream_to_ctx(struct dc * dc,struct dc_state * new_ctx,struct dc_stream_state * stream)2418 enum dc_status dc_add_stream_to_ctx(
2419 		struct dc *dc,
2420 		struct dc_state *new_ctx,
2421 		struct dc_stream_state *stream)
2422 {
2423 	enum dc_status res;
2424 	DC_LOGGER_INIT(dc->ctx->logger);
2425 
2426 	if (new_ctx->stream_count >= dc->res_pool->timing_generator_count) {
2427 		DC_LOG_WARNING("Max streams reached, can't add stream %p !\n", stream);
2428 		return DC_ERROR_UNEXPECTED;
2429 	}
2430 
2431 	new_ctx->streams[new_ctx->stream_count] = stream;
2432 	dc_stream_retain(stream);
2433 	new_ctx->stream_count++;
2434 
2435 	res = dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
2436 	if (res != DC_OK)
2437 		DC_LOG_WARNING("Adding stream %p to context failed with err %d!\n", stream, res);
2438 
2439 	return res;
2440 }
2441 
2442 /*
2443  * dc_remove_stream_from_ctx() - Remove a stream from a dc_state.
2444  */
dc_remove_stream_from_ctx(struct dc * dc,struct dc_state * new_ctx,struct dc_stream_state * stream)2445 enum dc_status dc_remove_stream_from_ctx(
2446 			struct dc *dc,
2447 			struct dc_state *new_ctx,
2448 			struct dc_stream_state *stream)
2449 {
2450 	int i;
2451 	struct dc_context *dc_ctx = dc->ctx;
2452 	struct pipe_ctx *del_pipe = resource_get_otg_master_for_stream(&new_ctx->res_ctx, stream);
2453 	struct pipe_ctx *odm_pipe;
2454 
2455 	if (!del_pipe) {
2456 		DC_ERROR("Pipe not found for stream %p !\n", stream);
2457 		return DC_ERROR_UNEXPECTED;
2458 	}
2459 
2460 	odm_pipe = del_pipe->next_odm_pipe;
2461 
2462 	/* Release primary pipe */
2463 	ASSERT(del_pipe->stream_res.stream_enc);
2464 	update_stream_engine_usage(
2465 			&new_ctx->res_ctx,
2466 				dc->res_pool,
2467 			del_pipe->stream_res.stream_enc,
2468 			false);
2469 
2470 	if (dc->link_srv->dp_is_128b_132b_signal(del_pipe)) {
2471 		update_hpo_dp_stream_engine_usage(
2472 			&new_ctx->res_ctx, dc->res_pool,
2473 			del_pipe->stream_res.hpo_dp_stream_enc,
2474 			false);
2475 		remove_hpo_dp_link_enc_from_ctx(&new_ctx->res_ctx, del_pipe, del_pipe->stream);
2476 	}
2477 
2478 	if (del_pipe->stream_res.audio)
2479 		update_audio_usage(
2480 			&new_ctx->res_ctx,
2481 			dc->res_pool,
2482 			del_pipe->stream_res.audio,
2483 			false);
2484 
2485 	resource_unreference_clock_source(&new_ctx->res_ctx,
2486 					  dc->res_pool,
2487 					  del_pipe->clock_source);
2488 
2489 	if (dc->res_pool->funcs->remove_stream_from_ctx)
2490 		dc->res_pool->funcs->remove_stream_from_ctx(dc, new_ctx, stream);
2491 
2492 	while (odm_pipe) {
2493 		struct pipe_ctx *next_odm_pipe = odm_pipe->next_odm_pipe;
2494 
2495 		memset(odm_pipe, 0, sizeof(*odm_pipe));
2496 		odm_pipe = next_odm_pipe;
2497 	}
2498 	memset(del_pipe, 0, sizeof(*del_pipe));
2499 
2500 	for (i = 0; i < new_ctx->stream_count; i++)
2501 		if (new_ctx->streams[i] == stream)
2502 			break;
2503 
2504 	if (new_ctx->streams[i] != stream) {
2505 		DC_ERROR("Context doesn't have stream %p !\n", stream);
2506 		return DC_ERROR_UNEXPECTED;
2507 	}
2508 
2509 	dc_stream_release(new_ctx->streams[i]);
2510 	new_ctx->stream_count--;
2511 
2512 	/* Trim back arrays */
2513 	for (; i < new_ctx->stream_count; i++) {
2514 		new_ctx->streams[i] = new_ctx->streams[i + 1];
2515 		new_ctx->stream_status[i] = new_ctx->stream_status[i + 1];
2516 	}
2517 
2518 	new_ctx->streams[new_ctx->stream_count] = NULL;
2519 	memset(
2520 			&new_ctx->stream_status[new_ctx->stream_count],
2521 			0,
2522 			sizeof(new_ctx->stream_status[0]));
2523 
2524 	return DC_OK;
2525 }
2526 
find_pll_sharable_stream(struct dc_stream_state * stream_needs_pll,struct dc_state * context)2527 static struct dc_stream_state *find_pll_sharable_stream(
2528 		struct dc_stream_state *stream_needs_pll,
2529 		struct dc_state *context)
2530 {
2531 	int i;
2532 
2533 	for (i = 0; i < context->stream_count; i++) {
2534 		struct dc_stream_state *stream_has_pll = context->streams[i];
2535 
2536 		/* We are looking for non dp, non virtual stream */
2537 		if (resource_are_streams_timing_synchronizable(
2538 			stream_needs_pll, stream_has_pll)
2539 			&& !dc_is_dp_signal(stream_has_pll->signal)
2540 			&& stream_has_pll->link->connector_signal
2541 			!= SIGNAL_TYPE_VIRTUAL)
2542 			return stream_has_pll;
2543 
2544 	}
2545 
2546 	return NULL;
2547 }
2548 
get_norm_pix_clk(const struct dc_crtc_timing * timing)2549 static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
2550 {
2551 	uint32_t pix_clk = timing->pix_clk_100hz;
2552 	uint32_t normalized_pix_clk = pix_clk;
2553 
2554 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2555 		pix_clk /= 2;
2556 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
2557 		switch (timing->display_color_depth) {
2558 		case COLOR_DEPTH_666:
2559 		case COLOR_DEPTH_888:
2560 			normalized_pix_clk = pix_clk;
2561 			break;
2562 		case COLOR_DEPTH_101010:
2563 			normalized_pix_clk = (pix_clk * 30) / 24;
2564 			break;
2565 		case COLOR_DEPTH_121212:
2566 			normalized_pix_clk = (pix_clk * 36) / 24;
2567 		break;
2568 		case COLOR_DEPTH_161616:
2569 			normalized_pix_clk = (pix_clk * 48) / 24;
2570 		break;
2571 		default:
2572 			ASSERT(0);
2573 		break;
2574 		}
2575 	}
2576 	return normalized_pix_clk;
2577 }
2578 
calculate_phy_pix_clks(struct dc_stream_state * stream)2579 static void calculate_phy_pix_clks(struct dc_stream_state *stream)
2580 {
2581 	/* update actual pixel clock on all streams */
2582 	if (dc_is_hdmi_signal(stream->signal))
2583 		stream->phy_pix_clk = get_norm_pix_clk(
2584 			&stream->timing) / 10;
2585 	else
2586 		stream->phy_pix_clk =
2587 			stream->timing.pix_clk_100hz / 10;
2588 
2589 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
2590 		stream->phy_pix_clk *= 2;
2591 }
2592 
acquire_resource_from_hw_enabled_state(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)2593 static int acquire_resource_from_hw_enabled_state(
2594 		struct resource_context *res_ctx,
2595 		const struct resource_pool *pool,
2596 		struct dc_stream_state *stream)
2597 {
2598 	struct dc_link *link = stream->link;
2599 	unsigned int i, inst, tg_inst = 0;
2600 	uint32_t numPipes = 1;
2601 	uint32_t id_src[4] = {0};
2602 
2603 	/* Check for enabled DIG to identify enabled display */
2604 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
2605 		return -1;
2606 
2607 	inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
2608 
2609 	if (inst == ENGINE_ID_UNKNOWN)
2610 		return -1;
2611 
2612 	for (i = 0; i < pool->stream_enc_count; i++) {
2613 		if (pool->stream_enc[i]->id == inst) {
2614 			tg_inst = pool->stream_enc[i]->funcs->dig_source_otg(
2615 				pool->stream_enc[i]);
2616 			break;
2617 		}
2618 	}
2619 
2620 	// tg_inst not found
2621 	if (i == pool->stream_enc_count)
2622 		return -1;
2623 
2624 	if (tg_inst >= pool->timing_generator_count)
2625 		return -1;
2626 
2627 	if (!res_ctx->pipe_ctx[tg_inst].stream) {
2628 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[tg_inst];
2629 
2630 		pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
2631 		id_src[0] = tg_inst;
2632 
2633 		if (pipe_ctx->stream_res.tg->funcs->get_optc_source)
2634 			pipe_ctx->stream_res.tg->funcs->get_optc_source(pipe_ctx->stream_res.tg,
2635 						&numPipes, &id_src[0], &id_src[1]);
2636 
2637 		if (id_src[0] == 0xf && id_src[1] == 0xf) {
2638 			id_src[0] = tg_inst;
2639 			numPipes = 1;
2640 		}
2641 
2642 		for (i = 0; i < numPipes; i++) {
2643 			//Check if src id invalid
2644 			if (id_src[i] == 0xf)
2645 				return -1;
2646 
2647 			pipe_ctx = &res_ctx->pipe_ctx[id_src[i]];
2648 
2649 			pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
2650 			pipe_ctx->plane_res.mi = pool->mis[id_src[i]];
2651 			pipe_ctx->plane_res.hubp = pool->hubps[id_src[i]];
2652 			pipe_ctx->plane_res.ipp = pool->ipps[id_src[i]];
2653 			pipe_ctx->plane_res.xfm = pool->transforms[id_src[i]];
2654 			pipe_ctx->plane_res.dpp = pool->dpps[id_src[i]];
2655 			pipe_ctx->stream_res.opp = pool->opps[id_src[i]];
2656 
2657 			if (pool->dpps[id_src[i]]) {
2658 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[id_src[i]]->inst;
2659 
2660 				if (pool->mpc->funcs->read_mpcc_state) {
2661 					struct mpcc_state s = {0};
2662 
2663 					pool->mpc->funcs->read_mpcc_state(pool->mpc, pipe_ctx->plane_res.mpcc_inst, &s);
2664 
2665 					if (s.dpp_id < MAX_MPCC)
2666 						pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].dpp_id =
2667 								s.dpp_id;
2668 
2669 					if (s.bot_mpcc_id < MAX_MPCC)
2670 						pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].mpcc_bot =
2671 								&pool->mpc->mpcc_array[s.bot_mpcc_id];
2672 
2673 					if (s.opp_id < MAX_OPP)
2674 						pipe_ctx->stream_res.opp->mpc_tree_params.opp_id = s.opp_id;
2675 				}
2676 			}
2677 			pipe_ctx->pipe_idx = id_src[i];
2678 
2679 			if (id_src[i] >= pool->timing_generator_count) {
2680 				id_src[i] = pool->timing_generator_count - 1;
2681 
2682 				pipe_ctx->stream_res.tg = pool->timing_generators[id_src[i]];
2683 				pipe_ctx->stream_res.opp = pool->opps[id_src[i]];
2684 			}
2685 
2686 			pipe_ctx->stream = stream;
2687 		}
2688 
2689 		if (numPipes == 2) {
2690 			stream->apply_boot_odm_mode = dm_odm_combine_policy_2to1;
2691 			res_ctx->pipe_ctx[id_src[0]].next_odm_pipe = &res_ctx->pipe_ctx[id_src[1]];
2692 			res_ctx->pipe_ctx[id_src[0]].prev_odm_pipe = NULL;
2693 			res_ctx->pipe_ctx[id_src[1]].next_odm_pipe = NULL;
2694 			res_ctx->pipe_ctx[id_src[1]].prev_odm_pipe = &res_ctx->pipe_ctx[id_src[0]];
2695 		} else
2696 			stream->apply_boot_odm_mode = dm_odm_combine_mode_disabled;
2697 
2698 		return id_src[0];
2699 	}
2700 
2701 	return -1;
2702 }
2703 
mark_seamless_boot_stream(const struct dc * dc,struct dc_stream_state * stream)2704 static void mark_seamless_boot_stream(
2705 		const struct dc  *dc,
2706 		struct dc_stream_state *stream)
2707 {
2708 	struct dc_bios *dcb = dc->ctx->dc_bios;
2709 
2710 	if (dc->config.allow_seamless_boot_optimization &&
2711 			!dcb->funcs->is_accelerated_mode(dcb)) {
2712 		if (dc_validate_boot_timing(dc, stream->sink, &stream->timing))
2713 			stream->apply_seamless_boot_optimization = true;
2714 	}
2715 }
2716 
resource_map_pool_resources(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)2717 enum dc_status resource_map_pool_resources(
2718 		const struct dc  *dc,
2719 		struct dc_state *context,
2720 		struct dc_stream_state *stream)
2721 {
2722 	const struct resource_pool *pool = dc->res_pool;
2723 	int i;
2724 	struct dc_context *dc_ctx = dc->ctx;
2725 	struct pipe_ctx *pipe_ctx = NULL;
2726 	int pipe_idx = -1;
2727 
2728 	calculate_phy_pix_clks(stream);
2729 
2730 	mark_seamless_boot_stream(dc, stream);
2731 
2732 	if (stream->apply_seamless_boot_optimization) {
2733 		pipe_idx = acquire_resource_from_hw_enabled_state(
2734 				&context->res_ctx,
2735 				pool,
2736 				stream);
2737 		if (pipe_idx < 0)
2738 			/* hw resource was assigned to other stream */
2739 			stream->apply_seamless_boot_optimization = false;
2740 	}
2741 
2742 	if (pipe_idx < 0)
2743 		/* acquire new resources */
2744 		pipe_idx = acquire_first_free_pipe(&context->res_ctx, pool, stream);
2745 
2746 	if (pipe_idx < 0)
2747 		pipe_idx = acquire_first_split_pipe(&context->res_ctx, pool, stream);
2748 
2749 	if (pipe_idx < 0 || context->res_ctx.pipe_ctx[pipe_idx].stream_res.tg == NULL)
2750 		return DC_NO_CONTROLLER_RESOURCE;
2751 
2752 	pipe_ctx = &context->res_ctx.pipe_ctx[pipe_idx];
2753 
2754 	pipe_ctx->stream_res.stream_enc =
2755 		dc->res_pool->funcs->find_first_free_match_stream_enc_for_link(
2756 			&context->res_ctx, pool, stream);
2757 
2758 	if (!pipe_ctx->stream_res.stream_enc)
2759 		return DC_NO_STREAM_ENC_RESOURCE;
2760 
2761 	update_stream_engine_usage(
2762 		&context->res_ctx, pool,
2763 		pipe_ctx->stream_res.stream_enc,
2764 		true);
2765 
2766 	/* Allocate DP HPO Stream Encoder based on signal, hw capabilities
2767 	 * and link settings
2768 	 */
2769 	if (dc_is_dp_signal(stream->signal)) {
2770 		if (!dc->link_srv->dp_decide_link_settings(stream, &pipe_ctx->link_config.dp_link_settings))
2771 			return DC_FAIL_DP_LINK_BANDWIDTH;
2772 		if (dc->link_srv->dp_get_encoding_format(
2773 				&pipe_ctx->link_config.dp_link_settings) == DP_128b_132b_ENCODING) {
2774 			pipe_ctx->stream_res.hpo_dp_stream_enc =
2775 					find_first_free_match_hpo_dp_stream_enc_for_link(
2776 							&context->res_ctx, pool, stream);
2777 
2778 			if (!pipe_ctx->stream_res.hpo_dp_stream_enc)
2779 				return DC_NO_STREAM_ENC_RESOURCE;
2780 
2781 			update_hpo_dp_stream_engine_usage(
2782 					&context->res_ctx, pool,
2783 					pipe_ctx->stream_res.hpo_dp_stream_enc,
2784 					true);
2785 			if (!add_hpo_dp_link_enc_to_ctx(&context->res_ctx, pool, pipe_ctx, stream))
2786 				return DC_NO_LINK_ENC_RESOURCE;
2787 		}
2788 	}
2789 
2790 	/* TODO: Add check if ASIC support and EDID audio */
2791 	if (!stream->converter_disable_audio &&
2792 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
2793 	    stream->audio_info.mode_count && stream->audio_info.flags.all) {
2794 		pipe_ctx->stream_res.audio = find_first_free_audio(
2795 		&context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id, dc_ctx->dce_version);
2796 
2797 		/*
2798 		 * Audio assigned in order first come first get.
2799 		 * There are asics which has number of audio
2800 		 * resources less then number of pipes
2801 		 */
2802 		if (pipe_ctx->stream_res.audio)
2803 			update_audio_usage(&context->res_ctx, pool,
2804 					   pipe_ctx->stream_res.audio, true);
2805 	}
2806 
2807 	/* Add ABM to the resource if on EDP */
2808 	if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal)) {
2809 		if (pool->abm)
2810 			pipe_ctx->stream_res.abm = pool->abm;
2811 		else
2812 			pipe_ctx->stream_res.abm = pool->multiple_abms[pipe_ctx->stream_res.tg->inst];
2813 	}
2814 
2815 	for (i = 0; i < context->stream_count; i++)
2816 		if (context->streams[i] == stream) {
2817 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
2818 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->stream_enc_inst;
2819 			context->stream_status[i].audio_inst =
2820 				pipe_ctx->stream_res.audio ? pipe_ctx->stream_res.audio->inst : -1;
2821 
2822 			return DC_OK;
2823 		}
2824 
2825 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
2826 	return DC_ERROR_UNEXPECTED;
2827 }
2828 
2829 /**
2830  * dc_resource_state_copy_construct_current() - Creates a new dc_state from existing state
2831  *
2832  * @dc: copy out of dc->current_state
2833  * @dst_ctx: copy into this
2834  *
2835  * This function makes a shallow copy of the current DC state and increments
2836  * refcounts on existing streams and planes.
2837  */
dc_resource_state_copy_construct_current(const struct dc * dc,struct dc_state * dst_ctx)2838 void dc_resource_state_copy_construct_current(
2839 		const struct dc *dc,
2840 		struct dc_state *dst_ctx)
2841 {
2842 	dc_resource_state_copy_construct(dc->current_state, dst_ctx);
2843 }
2844 
2845 
dc_resource_state_construct(const struct dc * dc,struct dc_state * dst_ctx)2846 void dc_resource_state_construct(
2847 		const struct dc *dc,
2848 		struct dc_state *dst_ctx)
2849 {
2850 	dst_ctx->clk_mgr = dc->clk_mgr;
2851 
2852 	/* Initialise DIG link encoder resource tracking variables. */
2853 	link_enc_cfg_init(dc, dst_ctx);
2854 }
2855 
2856 
dc_resource_is_dsc_encoding_supported(const struct dc * dc)2857 bool dc_resource_is_dsc_encoding_supported(const struct dc *dc)
2858 {
2859 	if (dc->res_pool == NULL)
2860 		return false;
2861 
2862 	return dc->res_pool->res_cap->num_dsc > 0;
2863 }
2864 
planes_changed_for_existing_stream(struct dc_state * context,struct dc_stream_state * stream,const struct dc_validation_set set[],int set_count)2865 static bool planes_changed_for_existing_stream(struct dc_state *context,
2866 					       struct dc_stream_state *stream,
2867 					       const struct dc_validation_set set[],
2868 					       int set_count)
2869 {
2870 	int i, j;
2871 	struct dc_stream_status *stream_status = NULL;
2872 
2873 	for (i = 0; i < context->stream_count; i++) {
2874 		if (context->streams[i] == stream) {
2875 			stream_status = &context->stream_status[i];
2876 			break;
2877 		}
2878 	}
2879 
2880 	if (!stream_status)
2881 		ASSERT(0);
2882 
2883 	for (i = 0; i < set_count; i++)
2884 		if (set[i].stream == stream)
2885 			break;
2886 
2887 	if (i == set_count)
2888 		ASSERT(0);
2889 
2890 	if (set[i].plane_count != stream_status->plane_count)
2891 		return true;
2892 
2893 	for (j = 0; j < set[i].plane_count; j++)
2894 		if (set[i].plane_states[j] != stream_status->plane_states[j])
2895 			return true;
2896 
2897 	return false;
2898 }
2899 
2900 /**
2901  * dc_validate_with_context - Validate and update the potential new stream in the context object
2902  *
2903  * @dc: Used to get the current state status
2904  * @set: An array of dc_validation_set with all the current streams reference
2905  * @set_count: Total of streams
2906  * @context: New context
2907  * @fast_validate: Enable or disable fast validation
2908  *
2909  * This function updates the potential new stream in the context object. It
2910  * creates multiple lists for the add, remove, and unchanged streams. In
2911  * particular, if the unchanged streams have a plane that changed, it is
2912  * necessary to remove all planes from the unchanged streams. In summary, this
2913  * function is responsible for validating the new context.
2914  *
2915  * Return:
2916  * In case of success, return DC_OK (1), otherwise, return a DC error.
2917  */
dc_validate_with_context(struct dc * dc,const struct dc_validation_set set[],int set_count,struct dc_state * context,bool fast_validate)2918 enum dc_status dc_validate_with_context(struct dc *dc,
2919 					const struct dc_validation_set set[],
2920 					int set_count,
2921 					struct dc_state *context,
2922 					bool fast_validate)
2923 {
2924 	struct dc_stream_state *unchanged_streams[MAX_PIPES] = { 0 };
2925 	struct dc_stream_state *del_streams[MAX_PIPES] = { 0 };
2926 	struct dc_stream_state *add_streams[MAX_PIPES] = { 0 };
2927 	int old_stream_count = context->stream_count;
2928 	enum dc_status res = DC_ERROR_UNEXPECTED;
2929 	int unchanged_streams_count = 0;
2930 	int del_streams_count = 0;
2931 	int add_streams_count = 0;
2932 	bool found = false;
2933 	int i, j, k;
2934 
2935 	DC_LOGGER_INIT(dc->ctx->logger);
2936 
2937 	/* First build a list of streams to be remove from current context */
2938 	for (i = 0; i < old_stream_count; i++) {
2939 		struct dc_stream_state *stream = context->streams[i];
2940 
2941 		for (j = 0; j < set_count; j++) {
2942 			if (stream == set[j].stream) {
2943 				found = true;
2944 				break;
2945 			}
2946 		}
2947 
2948 		if (!found)
2949 			del_streams[del_streams_count++] = stream;
2950 
2951 		found = false;
2952 	}
2953 
2954 	/* Second, build a list of new streams */
2955 	for (i = 0; i < set_count; i++) {
2956 		struct dc_stream_state *stream = set[i].stream;
2957 
2958 		for (j = 0; j < old_stream_count; j++) {
2959 			if (stream == context->streams[j]) {
2960 				found = true;
2961 				break;
2962 			}
2963 		}
2964 
2965 		if (!found)
2966 			add_streams[add_streams_count++] = stream;
2967 
2968 		found = false;
2969 	}
2970 
2971 	/* Build a list of unchanged streams which is necessary for handling
2972 	 * planes change such as added, removed, and updated.
2973 	 */
2974 	for (i = 0; i < set_count; i++) {
2975 		/* Check if stream is part of the delete list */
2976 		for (j = 0; j < del_streams_count; j++) {
2977 			if (set[i].stream == del_streams[j]) {
2978 				found = true;
2979 				break;
2980 			}
2981 		}
2982 
2983 		if (!found) {
2984 			/* Check if stream is part of the add list */
2985 			for (j = 0; j < add_streams_count; j++) {
2986 				if (set[i].stream == add_streams[j]) {
2987 					found = true;
2988 					break;
2989 				}
2990 			}
2991 		}
2992 
2993 		if (!found)
2994 			unchanged_streams[unchanged_streams_count++] = set[i].stream;
2995 
2996 		found = false;
2997 	}
2998 
2999 	/* Remove all planes for unchanged streams if planes changed */
3000 	for (i = 0; i < unchanged_streams_count; i++) {
3001 		if (planes_changed_for_existing_stream(context,
3002 						       unchanged_streams[i],
3003 						       set,
3004 						       set_count)) {
3005 			if (!dc_rem_all_planes_for_stream(dc,
3006 							  unchanged_streams[i],
3007 							  context)) {
3008 				res = DC_FAIL_DETACH_SURFACES;
3009 				goto fail;
3010 			}
3011 		}
3012 	}
3013 
3014 	/* Remove all planes for removed streams and then remove the streams */
3015 	for (i = 0; i < del_streams_count; i++) {
3016 		/* Need to cpy the dwb data from the old stream in order to efc to work */
3017 		if (del_streams[i]->num_wb_info > 0) {
3018 			for (j = 0; j < add_streams_count; j++) {
3019 				if (del_streams[i]->sink == add_streams[j]->sink) {
3020 					add_streams[j]->num_wb_info = del_streams[i]->num_wb_info;
3021 					for (k = 0; k < del_streams[i]->num_wb_info; k++)
3022 						add_streams[j]->writeback_info[k] = del_streams[i]->writeback_info[k];
3023 				}
3024 			}
3025 		}
3026 
3027 		if (!dc_rem_all_planes_for_stream(dc, del_streams[i], context)) {
3028 			res = DC_FAIL_DETACH_SURFACES;
3029 			goto fail;
3030 		}
3031 
3032 		res = dc_remove_stream_from_ctx(dc, context, del_streams[i]);
3033 		if (res != DC_OK)
3034 			goto fail;
3035 	}
3036 
3037 	/* Swap seamless boot stream to pipe 0 (if needed) to ensure pipe_ctx
3038 	 * matches. This may change in the future if seamless_boot_stream can be
3039 	 * multiple.
3040 	 */
3041 	for (i = 0; i < add_streams_count; i++) {
3042 		mark_seamless_boot_stream(dc, add_streams[i]);
3043 		if (add_streams[i]->apply_seamless_boot_optimization && i != 0) {
3044 			struct dc_stream_state *temp = add_streams[0];
3045 
3046 			add_streams[0] = add_streams[i];
3047 			add_streams[i] = temp;
3048 			break;
3049 		}
3050 	}
3051 
3052 	/* Add new streams and then add all planes for the new stream */
3053 	for (i = 0; i < add_streams_count; i++) {
3054 		calculate_phy_pix_clks(add_streams[i]);
3055 		res = dc_add_stream_to_ctx(dc, context, add_streams[i]);
3056 		if (res != DC_OK)
3057 			goto fail;
3058 
3059 		if (!add_all_planes_for_stream(dc, add_streams[i], set, set_count, context)) {
3060 			res = DC_FAIL_ATTACH_SURFACES;
3061 			goto fail;
3062 		}
3063 	}
3064 
3065 	/* Add all planes for unchanged streams if planes changed */
3066 	for (i = 0; i < unchanged_streams_count; i++) {
3067 		if (planes_changed_for_existing_stream(context,
3068 						       unchanged_streams[i],
3069 						       set,
3070 						       set_count)) {
3071 			if (!add_all_planes_for_stream(dc, unchanged_streams[i], set, set_count, context)) {
3072 				res = DC_FAIL_ATTACH_SURFACES;
3073 				goto fail;
3074 			}
3075 		}
3076 	}
3077 
3078 	res = dc_validate_global_state(dc, context, fast_validate);
3079 
3080 fail:
3081 	if (res != DC_OK)
3082 		DC_LOG_WARNING("%s:resource validation failed, dc_status:%d\n",
3083 			       __func__,
3084 			       res);
3085 
3086 	return res;
3087 }
3088 
3089 /**
3090  * dc_validate_global_state() - Determine if hardware can support a given state
3091  *
3092  * @dc: dc struct for this driver
3093  * @new_ctx: state to be validated
3094  * @fast_validate: set to true if only yes/no to support matters
3095  *
3096  * Checks hardware resource availability and bandwidth requirement.
3097  *
3098  * Return:
3099  * DC_OK if the result can be programmed. Otherwise, an error code.
3100  */
dc_validate_global_state(struct dc * dc,struct dc_state * new_ctx,bool fast_validate)3101 enum dc_status dc_validate_global_state(
3102 		struct dc *dc,
3103 		struct dc_state *new_ctx,
3104 		bool fast_validate)
3105 {
3106 	enum dc_status result = DC_ERROR_UNEXPECTED;
3107 	int i, j;
3108 
3109 	if (!new_ctx)
3110 		return DC_ERROR_UNEXPECTED;
3111 
3112 	if (dc->res_pool->funcs->validate_global) {
3113 		result = dc->res_pool->funcs->validate_global(dc, new_ctx);
3114 		if (result != DC_OK)
3115 			return result;
3116 	}
3117 
3118 	for (i = 0; i < new_ctx->stream_count; i++) {
3119 		struct dc_stream_state *stream = new_ctx->streams[i];
3120 
3121 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
3122 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
3123 
3124 			if (pipe_ctx->stream != stream)
3125 				continue;
3126 
3127 			if (dc->res_pool->funcs->patch_unknown_plane_state &&
3128 					pipe_ctx->plane_state &&
3129 					pipe_ctx->plane_state->tiling_info.gfx9.swizzle == DC_SW_UNKNOWN) {
3130 				result = dc->res_pool->funcs->patch_unknown_plane_state(pipe_ctx->plane_state);
3131 				if (result != DC_OK)
3132 					return result;
3133 			}
3134 
3135 			/* Switch to dp clock source only if there is
3136 			 * no non dp stream that shares the same timing
3137 			 * with the dp stream.
3138 			 */
3139 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
3140 				!find_pll_sharable_stream(stream, new_ctx)) {
3141 
3142 				resource_unreference_clock_source(
3143 						&new_ctx->res_ctx,
3144 						dc->res_pool,
3145 						pipe_ctx->clock_source);
3146 
3147 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
3148 				resource_reference_clock_source(
3149 						&new_ctx->res_ctx,
3150 						dc->res_pool,
3151 						 pipe_ctx->clock_source);
3152 			}
3153 		}
3154 	}
3155 
3156 	result = resource_build_scaling_params_for_context(dc, new_ctx);
3157 
3158 	if (result == DC_OK)
3159 		if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx, fast_validate))
3160 			result = DC_FAIL_BANDWIDTH_VALIDATE;
3161 
3162 	/*
3163 	 * Only update link encoder to stream assignment after bandwidth validation passed.
3164 	 * TODO: Split out assignment and validation.
3165 	 */
3166 	if (result == DC_OK && dc->res_pool->funcs->link_encs_assign && fast_validate == false)
3167 		dc->res_pool->funcs->link_encs_assign(
3168 			dc, new_ctx, new_ctx->streams, new_ctx->stream_count);
3169 
3170 	return result;
3171 }
3172 
patch_gamut_packet_checksum(struct dc_info_packet * gamut_packet)3173 static void patch_gamut_packet_checksum(
3174 		struct dc_info_packet *gamut_packet)
3175 {
3176 	/* For gamut we recalc checksum */
3177 	if (gamut_packet->valid) {
3178 		uint8_t chk_sum = 0;
3179 		uint8_t *ptr;
3180 		uint8_t i;
3181 
3182 		/*start of the Gamut data. */
3183 		ptr = &gamut_packet->sb[3];
3184 
3185 		for (i = 0; i <= gamut_packet->sb[1]; i++)
3186 			chk_sum += ptr[i];
3187 
3188 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
3189 	}
3190 }
3191 
set_avi_info_frame(struct dc_info_packet * info_packet,struct pipe_ctx * pipe_ctx)3192 static void set_avi_info_frame(
3193 		struct dc_info_packet *info_packet,
3194 		struct pipe_ctx *pipe_ctx)
3195 {
3196 	struct dc_stream_state *stream = pipe_ctx->stream;
3197 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
3198 	uint32_t pixel_encoding = 0;
3199 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
3200 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
3201 	bool itc = false;
3202 	uint8_t itc_value = 0;
3203 	uint8_t cn0_cn1 = 0;
3204 	unsigned int cn0_cn1_value = 0;
3205 	uint8_t *check_sum = NULL;
3206 	uint8_t byte_index = 0;
3207 	union hdmi_info_packet hdmi_info;
3208 	union display_content_support support = {0};
3209 	unsigned int vic = pipe_ctx->stream->timing.vic;
3210 	unsigned int rid = pipe_ctx->stream->timing.rid;
3211 	unsigned int fr_ind = pipe_ctx->stream->timing.fr_index;
3212 	enum dc_timing_3d_format format;
3213 
3214 	memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
3215 
3216 	color_space = pipe_ctx->stream->output_color_space;
3217 	if (color_space == COLOR_SPACE_UNKNOWN)
3218 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
3219 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
3220 
3221 	/* Initialize header */
3222 	hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
3223 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
3224 	* not be used in HDMI 2.0 (Section 10.1) */
3225 	hdmi_info.bits.header.version = 2;
3226 	hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
3227 
3228 	/*
3229 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
3230 	 * according to HDMI 2.0 spec (Section 10.1)
3231 	 */
3232 
3233 	switch (stream->timing.pixel_encoding) {
3234 	case PIXEL_ENCODING_YCBCR422:
3235 		pixel_encoding = 1;
3236 		break;
3237 
3238 	case PIXEL_ENCODING_YCBCR444:
3239 		pixel_encoding = 2;
3240 		break;
3241 	case PIXEL_ENCODING_YCBCR420:
3242 		pixel_encoding = 3;
3243 		break;
3244 
3245 	case PIXEL_ENCODING_RGB:
3246 	default:
3247 		pixel_encoding = 0;
3248 	}
3249 
3250 	/* Y0_Y1_Y2 : The pixel encoding */
3251 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
3252 	hdmi_info.bits.Y0_Y1_Y2 = pixel_encoding;
3253 
3254 	/* A0 = 1 Active Format Information valid */
3255 	hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
3256 
3257 	/* B0, B1 = 3; Bar info data is valid */
3258 	hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
3259 
3260 	hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
3261 
3262 	/* S0, S1 : Underscan / Overscan */
3263 	/* TODO: un-hardcode scan type */
3264 	scan_type = SCANNING_TYPE_UNDERSCAN;
3265 	hdmi_info.bits.S0_S1 = scan_type;
3266 
3267 	/* C0, C1 : Colorimetry */
3268 	switch (color_space) {
3269 	case COLOR_SPACE_YCBCR709:
3270 	case COLOR_SPACE_YCBCR709_LIMITED:
3271 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
3272 		break;
3273 	case COLOR_SPACE_YCBCR601:
3274 	case COLOR_SPACE_YCBCR601_LIMITED:
3275 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
3276 		break;
3277 	case COLOR_SPACE_2020_RGB_FULLRANGE:
3278 	case COLOR_SPACE_2020_RGB_LIMITEDRANGE:
3279 	case COLOR_SPACE_2020_YCBCR:
3280 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
3281 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
3282 		break;
3283 	case COLOR_SPACE_ADOBERGB:
3284 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
3285 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
3286 		break;
3287 	case COLOR_SPACE_SRGB:
3288 	default:
3289 		hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
3290 		break;
3291 	}
3292 
3293 	if (pixel_encoding && color_space == COLOR_SPACE_2020_YCBCR &&
3294 			stream->out_transfer_func->tf == TRANSFER_FUNCTION_GAMMA22) {
3295 		hdmi_info.bits.EC0_EC2 = 0;
3296 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
3297 	}
3298 
3299 	/* TODO: un-hardcode aspect ratio */
3300 	aspect = stream->timing.aspect_ratio;
3301 
3302 	switch (aspect) {
3303 	case ASPECT_RATIO_4_3:
3304 	case ASPECT_RATIO_16_9:
3305 		hdmi_info.bits.M0_M1 = aspect;
3306 		break;
3307 
3308 	case ASPECT_RATIO_NO_DATA:
3309 	case ASPECT_RATIO_64_27:
3310 	case ASPECT_RATIO_256_135:
3311 	default:
3312 		hdmi_info.bits.M0_M1 = 0;
3313 	}
3314 
3315 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
3316 	hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
3317 
3318 	/* TODO: un-hardcode cn0_cn1 and itc */
3319 
3320 	cn0_cn1 = 0;
3321 	cn0_cn1_value = 0;
3322 
3323 	itc = true;
3324 	itc_value = 1;
3325 
3326 	support = stream->content_support;
3327 
3328 	if (itc) {
3329 		if (!support.bits.valid_content_type) {
3330 			cn0_cn1_value = 0;
3331 		} else {
3332 			if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GRAPHICS) {
3333 				if (support.bits.graphics_content == 1) {
3334 					cn0_cn1_value = 0;
3335 				}
3336 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_PHOTO) {
3337 				if (support.bits.photo_content == 1) {
3338 					cn0_cn1_value = 1;
3339 				} else {
3340 					cn0_cn1_value = 0;
3341 					itc_value = 0;
3342 				}
3343 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_CINEMA) {
3344 				if (support.bits.cinema_content == 1) {
3345 					cn0_cn1_value = 2;
3346 				} else {
3347 					cn0_cn1_value = 0;
3348 					itc_value = 0;
3349 				}
3350 			} else if (cn0_cn1 == DISPLAY_CONTENT_TYPE_GAME) {
3351 				if (support.bits.game_content == 1) {
3352 					cn0_cn1_value = 3;
3353 				} else {
3354 					cn0_cn1_value = 0;
3355 					itc_value = 0;
3356 				}
3357 			}
3358 		}
3359 		hdmi_info.bits.CN0_CN1 = cn0_cn1_value;
3360 		hdmi_info.bits.ITC = itc_value;
3361 	}
3362 
3363 	if (stream->qs_bit == 1) {
3364 		if (color_space == COLOR_SPACE_SRGB ||
3365 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE)
3366 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
3367 		else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
3368 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
3369 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
3370 		else
3371 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
3372 	} else
3373 		hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
3374 
3375 	/* TODO : We should handle YCC quantization */
3376 	/* but we do not have matrix calculation */
3377 	hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
3378 
3379 	///VIC
3380 	if (pipe_ctx->stream->timing.hdmi_vic != 0)
3381 		vic = 0;
3382 	format = stream->timing.timing_3d_format;
3383 	/*todo, add 3DStereo support*/
3384 	if (format != TIMING_3D_FORMAT_NONE) {
3385 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
3386 		switch (pipe_ctx->stream->timing.hdmi_vic) {
3387 		case 1:
3388 			vic = 95;
3389 			break;
3390 		case 2:
3391 			vic = 94;
3392 			break;
3393 		case 3:
3394 			vic = 93;
3395 			break;
3396 		case 4:
3397 			vic = 98;
3398 			break;
3399 		default:
3400 			break;
3401 		}
3402 	}
3403 	/* If VIC >= 128, the Source shall use AVI InfoFrame Version 3*/
3404 	hdmi_info.bits.VIC0_VIC7 = vic;
3405 	if (vic >= 128)
3406 		hdmi_info.bits.header.version = 3;
3407 	/* If (C1, C0)=(1, 1) and (EC2, EC1, EC0)=(1, 1, 1),
3408 	 * the Source shall use 20 AVI InfoFrame Version 4
3409 	 */
3410 	if (hdmi_info.bits.C0_C1 == COLORIMETRY_EXTENDED &&
3411 			hdmi_info.bits.EC0_EC2 == COLORIMETRYEX_RESERVED) {
3412 		hdmi_info.bits.header.version = 4;
3413 		hdmi_info.bits.header.length = 14;
3414 	}
3415 
3416 	if (rid != 0 && fr_ind != 0) {
3417 		hdmi_info.bits.header.version = 5;
3418 		hdmi_info.bits.header.length = 15;
3419 
3420 		hdmi_info.bits.FR0_FR3 = fr_ind & 0xF;
3421 		hdmi_info.bits.FR4 = (fr_ind >> 4) & 0x1;
3422 		hdmi_info.bits.RID0_RID5 = rid;
3423 	}
3424 
3425 	/* pixel repetition
3426 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
3427 	 * repetition start from 1 */
3428 	hdmi_info.bits.PR0_PR3 = 0;
3429 
3430 	/* Bar Info
3431 	 * barTop:    Line Number of End of Top Bar.
3432 	 * barBottom: Line Number of Start of Bottom Bar.
3433 	 * barLeft:   Pixel Number of End of Left Bar.
3434 	 * barRight:  Pixel Number of Start of Right Bar. */
3435 	hdmi_info.bits.bar_top = stream->timing.v_border_top;
3436 	hdmi_info.bits.bar_bottom = (stream->timing.v_total
3437 			- stream->timing.v_border_bottom + 1);
3438 	hdmi_info.bits.bar_left  = stream->timing.h_border_left;
3439 	hdmi_info.bits.bar_right = (stream->timing.h_total
3440 			- stream->timing.h_border_right + 1);
3441 
3442     /* Additional Colorimetry Extension
3443      * Used in conduction with C0-C1 and EC0-EC2
3444      * 0 = DCI-P3 RGB (D65)
3445      * 1 = DCI-P3 RGB (theater)
3446      */
3447 	hdmi_info.bits.ACE0_ACE3 = 0;
3448 
3449 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
3450 	check_sum = &hdmi_info.packet_raw_data.sb[0];
3451 
3452 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + hdmi_info.bits.header.length + hdmi_info.bits.header.version;
3453 
3454 	for (byte_index = 1; byte_index <= hdmi_info.bits.header.length; byte_index++)
3455 		*check_sum += hdmi_info.packet_raw_data.sb[byte_index];
3456 
3457 	/* one byte complement */
3458 	*check_sum = (uint8_t) (0x100 - *check_sum);
3459 
3460 	/* Store in hw_path_mode */
3461 	info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
3462 	info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
3463 	info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
3464 
3465 	for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
3466 		info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
3467 
3468 	info_packet->valid = true;
3469 }
3470 
set_vendor_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3471 static void set_vendor_info_packet(
3472 		struct dc_info_packet *info_packet,
3473 		struct dc_stream_state *stream)
3474 {
3475 	/* SPD info packet for FreeSync */
3476 
3477 	/* Check if Freesync is supported. Return if false. If true,
3478 	 * set the corresponding bit in the info packet
3479 	 */
3480 	if (!stream->vsp_infopacket.valid)
3481 		return;
3482 
3483 	*info_packet = stream->vsp_infopacket;
3484 }
3485 
set_spd_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3486 static void set_spd_info_packet(
3487 		struct dc_info_packet *info_packet,
3488 		struct dc_stream_state *stream)
3489 {
3490 	/* SPD info packet for FreeSync */
3491 
3492 	/* Check if Freesync is supported. Return if false. If true,
3493 	 * set the corresponding bit in the info packet
3494 	 */
3495 	if (!stream->vrr_infopacket.valid)
3496 		return;
3497 
3498 	*info_packet = stream->vrr_infopacket;
3499 }
3500 
set_hdr_static_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3501 static void set_hdr_static_info_packet(
3502 		struct dc_info_packet *info_packet,
3503 		struct dc_stream_state *stream)
3504 {
3505 	/* HDR Static Metadata info packet for HDR10 */
3506 
3507 	if (!stream->hdr_static_metadata.valid ||
3508 			stream->use_dynamic_meta)
3509 		return;
3510 
3511 	*info_packet = stream->hdr_static_metadata;
3512 }
3513 
set_vsc_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3514 static void set_vsc_info_packet(
3515 		struct dc_info_packet *info_packet,
3516 		struct dc_stream_state *stream)
3517 {
3518 	if (!stream->vsc_infopacket.valid)
3519 		return;
3520 
3521 	*info_packet = stream->vsc_infopacket;
3522 }
set_hfvs_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3523 static void set_hfvs_info_packet(
3524 		struct dc_info_packet *info_packet,
3525 		struct dc_stream_state *stream)
3526 {
3527 	if (!stream->hfvsif_infopacket.valid)
3528 		return;
3529 
3530 	*info_packet = stream->hfvsif_infopacket;
3531 }
3532 
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)3533 static void adaptive_sync_override_dp_info_packets_sdp_line_num(
3534 		const struct dc_crtc_timing *timing,
3535 		struct enc_sdp_line_num *sdp_line_num,
3536 		struct _vcs_dpi_display_pipe_dest_params_st *pipe_dlg_param)
3537 {
3538 	uint32_t asic_blank_start = 0;
3539 	uint32_t asic_blank_end   = 0;
3540 	uint32_t v_update = 0;
3541 
3542 	const struct dc_crtc_timing *tg = timing;
3543 
3544 	/* blank_start = frame end - front porch */
3545 	asic_blank_start = tg->v_total - tg->v_front_porch;
3546 
3547 	/* blank_end = blank_start - active */
3548 	asic_blank_end = (asic_blank_start - tg->v_border_bottom -
3549 						tg->v_addressable - tg->v_border_top);
3550 
3551 	if (pipe_dlg_param->vstartup_start > asic_blank_end) {
3552 		v_update = (tg->v_total - (pipe_dlg_param->vstartup_start - asic_blank_end));
3553 		sdp_line_num->adaptive_sync_line_num_valid = true;
3554 		sdp_line_num->adaptive_sync_line_num = (tg->v_total - v_update - 1);
3555 	} else {
3556 		sdp_line_num->adaptive_sync_line_num_valid = false;
3557 		sdp_line_num->adaptive_sync_line_num = 0;
3558 	}
3559 }
3560 
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)3561 static void set_adaptive_sync_info_packet(
3562 		struct dc_info_packet *info_packet,
3563 		const struct dc_stream_state *stream,
3564 		struct encoder_info_frame *info_frame,
3565 		struct _vcs_dpi_display_pipe_dest_params_st *pipe_dlg_param)
3566 {
3567 	if (!stream->adaptive_sync_infopacket.valid)
3568 		return;
3569 
3570 	adaptive_sync_override_dp_info_packets_sdp_line_num(
3571 			&stream->timing,
3572 			&info_frame->sdp_line_num,
3573 			pipe_dlg_param);
3574 
3575 	*info_packet = stream->adaptive_sync_infopacket;
3576 }
3577 
set_vtem_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)3578 static void set_vtem_info_packet(
3579 		struct dc_info_packet *info_packet,
3580 		struct dc_stream_state *stream)
3581 {
3582 	if (!stream->vtem_infopacket.valid)
3583 		return;
3584 
3585 	*info_packet = stream->vtem_infopacket;
3586 }
3587 
dc_resource_state_destruct(struct dc_state * context)3588 void dc_resource_state_destruct(struct dc_state *context)
3589 {
3590 	int i, j;
3591 
3592 	for (i = 0; i < context->stream_count; i++) {
3593 		for (j = 0; j < context->stream_status[i].plane_count; j++)
3594 			dc_plane_state_release(
3595 				context->stream_status[i].plane_states[j]);
3596 
3597 		context->stream_status[i].plane_count = 0;
3598 		dc_stream_release(context->streams[i]);
3599 		context->streams[i] = NULL;
3600 	}
3601 	context->stream_count = 0;
3602 }
3603 
dc_resource_state_copy_construct(const struct dc_state * src_ctx,struct dc_state * dst_ctx)3604 void dc_resource_state_copy_construct(
3605 		const struct dc_state *src_ctx,
3606 		struct dc_state *dst_ctx)
3607 {
3608 	int i, j;
3609 	struct kref refcount = dst_ctx->refcount;
3610 
3611 	*dst_ctx = *src_ctx;
3612 
3613 	for (i = 0; i < MAX_PIPES; i++) {
3614 		struct pipe_ctx *cur_pipe = &dst_ctx->res_ctx.pipe_ctx[i];
3615 
3616 		if (cur_pipe->top_pipe)
3617 			cur_pipe->top_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->top_pipe->pipe_idx];
3618 
3619 		if (cur_pipe->bottom_pipe)
3620 			cur_pipe->bottom_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->bottom_pipe->pipe_idx];
3621 
3622 		if (cur_pipe->next_odm_pipe)
3623 			cur_pipe->next_odm_pipe =  &dst_ctx->res_ctx.pipe_ctx[cur_pipe->next_odm_pipe->pipe_idx];
3624 
3625 		if (cur_pipe->prev_odm_pipe)
3626 			cur_pipe->prev_odm_pipe = &dst_ctx->res_ctx.pipe_ctx[cur_pipe->prev_odm_pipe->pipe_idx];
3627 	}
3628 
3629 	for (i = 0; i < dst_ctx->stream_count; i++) {
3630 		dc_stream_retain(dst_ctx->streams[i]);
3631 		for (j = 0; j < dst_ctx->stream_status[i].plane_count; j++)
3632 			dc_plane_state_retain(
3633 				dst_ctx->stream_status[i].plane_states[j]);
3634 	}
3635 
3636 	/* context refcount should not be overridden */
3637 	dst_ctx->refcount = refcount;
3638 
3639 }
3640 
dc_resource_find_first_free_pll(struct resource_context * res_ctx,const struct resource_pool * pool)3641 struct clock_source *dc_resource_find_first_free_pll(
3642 		struct resource_context *res_ctx,
3643 		const struct resource_pool *pool)
3644 {
3645 	int i;
3646 
3647 	for (i = 0; i < pool->clk_src_count; ++i) {
3648 		if (res_ctx->clock_source_ref_count[i] == 0)
3649 			return pool->clock_sources[i];
3650 	}
3651 
3652 	return NULL;
3653 }
3654 
resource_build_info_frame(struct pipe_ctx * pipe_ctx)3655 void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
3656 {
3657 	enum signal_type signal = SIGNAL_TYPE_NONE;
3658 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
3659 
3660 	/* default all packets to invalid */
3661 	info->avi.valid = false;
3662 	info->gamut.valid = false;
3663 	info->vendor.valid = false;
3664 	info->spd.valid = false;
3665 	info->hdrsmd.valid = false;
3666 	info->vsc.valid = false;
3667 	info->hfvsif.valid = false;
3668 	info->vtem.valid = false;
3669 	info->adaptive_sync.valid = false;
3670 	signal = pipe_ctx->stream->signal;
3671 
3672 	/* HDMi and DP have different info packets*/
3673 	if (dc_is_hdmi_signal(signal)) {
3674 		set_avi_info_frame(&info->avi, pipe_ctx);
3675 
3676 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
3677 		set_hfvs_info_packet(&info->hfvsif, pipe_ctx->stream);
3678 		set_vtem_info_packet(&info->vtem, pipe_ctx->stream);
3679 
3680 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
3681 
3682 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
3683 
3684 	} else if (dc_is_dp_signal(signal)) {
3685 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
3686 
3687 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
3688 
3689 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
3690 		set_adaptive_sync_info_packet(&info->adaptive_sync,
3691 										pipe_ctx->stream,
3692 										info,
3693 										&pipe_ctx->pipe_dlg_param);
3694 	}
3695 
3696 	patch_gamut_packet_checksum(&info->gamut);
3697 }
3698 
resource_map_clock_resources(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)3699 enum dc_status resource_map_clock_resources(
3700 		const struct dc  *dc,
3701 		struct dc_state *context,
3702 		struct dc_stream_state *stream)
3703 {
3704 	/* acquire new resources */
3705 	const struct resource_pool *pool = dc->res_pool;
3706 	struct pipe_ctx *pipe_ctx = resource_get_otg_master_for_stream(
3707 				&context->res_ctx, stream);
3708 
3709 	if (!pipe_ctx)
3710 		return DC_ERROR_UNEXPECTED;
3711 
3712 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
3713 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
3714 		pipe_ctx->clock_source = pool->dp_clock_source;
3715 	else {
3716 		pipe_ctx->clock_source = NULL;
3717 
3718 		if (!dc->config.disable_disp_pll_sharing)
3719 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
3720 				&context->res_ctx,
3721 				pipe_ctx);
3722 
3723 		if (pipe_ctx->clock_source == NULL)
3724 			pipe_ctx->clock_source =
3725 				dc_resource_find_first_free_pll(
3726 					&context->res_ctx,
3727 					pool);
3728 	}
3729 
3730 	if (pipe_ctx->clock_source == NULL)
3731 		return DC_NO_CLOCK_SOURCE_RESOURCE;
3732 
3733 	resource_reference_clock_source(
3734 		&context->res_ctx, pool,
3735 		pipe_ctx->clock_source);
3736 
3737 	return DC_OK;
3738 }
3739 
3740 /*
3741  * Note: We need to disable output if clock sources change,
3742  * since bios does optimization and doesn't apply if changing
3743  * PHY when not already disabled.
3744  */
pipe_need_reprogram(struct pipe_ctx * pipe_ctx_old,struct pipe_ctx * pipe_ctx)3745 bool pipe_need_reprogram(
3746 		struct pipe_ctx *pipe_ctx_old,
3747 		struct pipe_ctx *pipe_ctx)
3748 {
3749 	if (!pipe_ctx_old->stream)
3750 		return false;
3751 
3752 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
3753 		return true;
3754 
3755 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
3756 		return true;
3757 
3758 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
3759 		return true;
3760 
3761 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
3762 			&& pipe_ctx_old->stream != pipe_ctx->stream)
3763 		return true;
3764 
3765 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
3766 		return true;
3767 
3768 	if (dc_is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
3769 		return true;
3770 
3771 	if (pipe_ctx_old->stream->dpms_off != pipe_ctx->stream->dpms_off)
3772 		return true;
3773 
3774 	if (false == pipe_ctx_old->stream->link->link_state_valid &&
3775 		false == pipe_ctx_old->stream->dpms_off)
3776 		return true;
3777 
3778 	if (pipe_ctx_old->stream_res.dsc != pipe_ctx->stream_res.dsc)
3779 		return true;
3780 
3781 	if (pipe_ctx_old->stream_res.hpo_dp_stream_enc != pipe_ctx->stream_res.hpo_dp_stream_enc)
3782 		return true;
3783 	if (pipe_ctx_old->link_res.hpo_dp_link_enc != pipe_ctx->link_res.hpo_dp_link_enc)
3784 		return true;
3785 
3786 	/* DIG link encoder resource assignment for stream changed. */
3787 	if (pipe_ctx_old->stream->ctx->dc->res_pool->funcs->link_encs_assign) {
3788 		bool need_reprogram = false;
3789 		struct dc *dc = pipe_ctx_old->stream->ctx->dc;
3790 		struct link_encoder *link_enc_prev =
3791 			link_enc_cfg_get_link_enc_used_by_stream_current(dc, pipe_ctx_old->stream);
3792 
3793 		if (link_enc_prev != pipe_ctx->stream->link_enc)
3794 			need_reprogram = true;
3795 
3796 		return need_reprogram;
3797 	}
3798 
3799 	return false;
3800 }
3801 
resource_build_bit_depth_reduction_params(struct dc_stream_state * stream,struct bit_depth_reduction_params * fmt_bit_depth)3802 void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
3803 		struct bit_depth_reduction_params *fmt_bit_depth)
3804 {
3805 	enum dc_dither_option option = stream->dither_option;
3806 	enum dc_pixel_encoding pixel_encoding =
3807 			stream->timing.pixel_encoding;
3808 
3809 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
3810 
3811 	if (option == DITHER_OPTION_DEFAULT) {
3812 		switch (stream->timing.display_color_depth) {
3813 		case COLOR_DEPTH_666:
3814 			option = DITHER_OPTION_SPATIAL6;
3815 			break;
3816 		case COLOR_DEPTH_888:
3817 			option = DITHER_OPTION_SPATIAL8;
3818 			break;
3819 		case COLOR_DEPTH_101010:
3820 			option = DITHER_OPTION_SPATIAL10;
3821 			break;
3822 		default:
3823 			option = DITHER_OPTION_DISABLE;
3824 		}
3825 	}
3826 
3827 	if (option == DITHER_OPTION_DISABLE)
3828 		return;
3829 
3830 	if (option == DITHER_OPTION_TRUN6) {
3831 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
3832 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
3833 	} else if (option == DITHER_OPTION_TRUN8 ||
3834 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
3835 			option == DITHER_OPTION_TRUN8_FM6) {
3836 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
3837 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
3838 	} else if (option == DITHER_OPTION_TRUN10        ||
3839 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
3840 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
3841 			option == DITHER_OPTION_TRUN10_FM8     ||
3842 			option == DITHER_OPTION_TRUN10_FM6     ||
3843 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
3844 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
3845 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
3846 	}
3847 
3848 	/* special case - Formatter can only reduce by 4 bits at most.
3849 	 * When reducing from 12 to 6 bits,
3850 	 * HW recommends we use trunc with round mode
3851 	 * (if we did nothing, trunc to 10 bits would be used)
3852 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
3853 	 * as the input was 10 bits.
3854 	 */
3855 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
3856 			option == DITHER_OPTION_SPATIAL6 ||
3857 			option == DITHER_OPTION_FM6) {
3858 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
3859 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
3860 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
3861 	}
3862 
3863 	/* spatial dither
3864 	 * note that spatial modes 1-3 are never used
3865 	 */
3866 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
3867 			option == DITHER_OPTION_SPATIAL6 ||
3868 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
3869 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
3870 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
3871 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
3872 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
3873 		fmt_bit_depth->flags.RGB_RANDOM =
3874 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
3875 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
3876 			option == DITHER_OPTION_SPATIAL8 ||
3877 			option == DITHER_OPTION_SPATIAL8_FM6        ||
3878 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
3879 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
3880 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
3881 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
3882 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
3883 		fmt_bit_depth->flags.RGB_RANDOM =
3884 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
3885 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
3886 			option == DITHER_OPTION_SPATIAL10 ||
3887 			option == DITHER_OPTION_SPATIAL10_FM8 ||
3888 			option == DITHER_OPTION_SPATIAL10_FM6) {
3889 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
3890 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
3891 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
3892 		fmt_bit_depth->flags.RGB_RANDOM =
3893 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
3894 	}
3895 
3896 	if (option == DITHER_OPTION_SPATIAL6 ||
3897 			option == DITHER_OPTION_SPATIAL8 ||
3898 			option == DITHER_OPTION_SPATIAL10) {
3899 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
3900 	} else {
3901 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
3902 	}
3903 
3904 	//////////////////////
3905 	//// temporal dither
3906 	//////////////////////
3907 	if (option == DITHER_OPTION_FM6           ||
3908 			option == DITHER_OPTION_SPATIAL8_FM6     ||
3909 			option == DITHER_OPTION_SPATIAL10_FM6     ||
3910 			option == DITHER_OPTION_TRUN10_FM6     ||
3911 			option == DITHER_OPTION_TRUN8_FM6      ||
3912 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
3913 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
3914 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
3915 	} else if (option == DITHER_OPTION_FM8        ||
3916 			option == DITHER_OPTION_SPATIAL10_FM8  ||
3917 			option == DITHER_OPTION_TRUN10_FM8) {
3918 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
3919 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
3920 	} else if (option == DITHER_OPTION_FM10) {
3921 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
3922 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
3923 	}
3924 
3925 	fmt_bit_depth->pixel_encoding = pixel_encoding;
3926 }
3927 
dc_validate_stream(struct dc * dc,struct dc_stream_state * stream)3928 enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
3929 {
3930 	if (dc == NULL || stream == NULL)
3931 		return DC_ERROR_UNEXPECTED;
3932 
3933 	struct dc_link *link = stream->link;
3934 	struct timing_generator *tg = dc->res_pool->timing_generators[0];
3935 	enum dc_status res = DC_OK;
3936 
3937 	calculate_phy_pix_clks(stream);
3938 
3939 	if (!tg->funcs->validate_timing(tg, &stream->timing))
3940 		res = DC_FAIL_CONTROLLER_VALIDATE;
3941 
3942 	if (res == DC_OK) {
3943 		if (link->ep_type == DISPLAY_ENDPOINT_PHY &&
3944 				!link->link_enc->funcs->validate_output_with_stream(
3945 						link->link_enc, stream))
3946 			res = DC_FAIL_ENC_VALIDATE;
3947 	}
3948 
3949 	/* TODO: validate audio ASIC caps, encoder */
3950 
3951 	if (res == DC_OK)
3952 		res = dc->link_srv->validate_mode_timing(stream,
3953 		      link,
3954 		      &stream->timing);
3955 
3956 	return res;
3957 }
3958 
dc_validate_plane(struct dc * dc,const struct dc_plane_state * plane_state)3959 enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
3960 {
3961 	enum dc_status res = DC_OK;
3962 
3963 	/* check if surface has invalid dimensions */
3964 	if (plane_state->src_rect.width == 0 || plane_state->src_rect.height == 0 ||
3965 		plane_state->dst_rect.width == 0 || plane_state->dst_rect.height == 0)
3966 		return DC_FAIL_SURFACE_VALIDATE;
3967 
3968 	/* TODO For now validates pixel format only */
3969 	if (dc->res_pool->funcs->validate_plane)
3970 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
3971 
3972 	return res;
3973 }
3974 
resource_pixel_format_to_bpp(enum surface_pixel_format format)3975 unsigned int resource_pixel_format_to_bpp(enum surface_pixel_format format)
3976 {
3977 	switch (format) {
3978 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
3979 		return 8;
3980 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
3981 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
3982 		return 12;
3983 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
3984 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
3985 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
3986 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
3987 		return 16;
3988 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
3989 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
3990 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
3991 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
3992 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
3993 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE:
3994 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE_ALPHA:
3995 		return 32;
3996 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
3997 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
3998 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
3999 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
4000 		return 64;
4001 	default:
4002 		ASSERT_CRITICAL(false);
4003 		return -1;
4004 	}
4005 }
get_max_audio_sample_rate(struct audio_mode * modes)4006 static unsigned int get_max_audio_sample_rate(struct audio_mode *modes)
4007 {
4008 	if (modes) {
4009 		if (modes->sample_rates.rate.RATE_192)
4010 			return 192000;
4011 		if (modes->sample_rates.rate.RATE_176_4)
4012 			return 176400;
4013 		if (modes->sample_rates.rate.RATE_96)
4014 			return 96000;
4015 		if (modes->sample_rates.rate.RATE_88_2)
4016 			return 88200;
4017 		if (modes->sample_rates.rate.RATE_48)
4018 			return 48000;
4019 		if (modes->sample_rates.rate.RATE_44_1)
4020 			return 44100;
4021 		if (modes->sample_rates.rate.RATE_32)
4022 			return 32000;
4023 	}
4024 	/*original logic when no audio info*/
4025 	return 441000;
4026 }
4027 
get_audio_check(struct audio_info * aud_modes,struct audio_check * audio_chk)4028 void get_audio_check(struct audio_info *aud_modes,
4029 	struct audio_check *audio_chk)
4030 {
4031 	unsigned int i;
4032 	unsigned int max_sample_rate = 0;
4033 
4034 	if (aud_modes) {
4035 		audio_chk->audio_packet_type = 0x2;/*audio sample packet AP = .25 for layout0, 1 for layout1*/
4036 
4037 		audio_chk->max_audiosample_rate = 0;
4038 		for (i = 0; i < aud_modes->mode_count; i++) {
4039 			max_sample_rate = get_max_audio_sample_rate(&aud_modes->modes[i]);
4040 			if (audio_chk->max_audiosample_rate < max_sample_rate)
4041 				audio_chk->max_audiosample_rate = max_sample_rate;
4042 			/*dts takes the same as type 2: AP = 0.25*/
4043 		}
4044 		/*check which one take more bandwidth*/
4045 		if (audio_chk->max_audiosample_rate > 192000)
4046 			audio_chk->audio_packet_type = 0x9;/*AP =1*/
4047 		audio_chk->acat = 0;/*not support*/
4048 	}
4049 }
4050 
get_temp_hpo_dp_link_enc(const struct resource_context * res_ctx,const struct resource_pool * const pool,const struct dc_link * link)4051 static struct hpo_dp_link_encoder *get_temp_hpo_dp_link_enc(
4052 		const struct resource_context *res_ctx,
4053 		const struct resource_pool *const pool,
4054 		const struct dc_link *link)
4055 {
4056 	struct hpo_dp_link_encoder *hpo_dp_link_enc = NULL;
4057 	int enc_index;
4058 
4059 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, link);
4060 
4061 	if (enc_index < 0)
4062 		enc_index = find_free_hpo_dp_link_enc(res_ctx, pool);
4063 
4064 	if (enc_index >= 0)
4065 		hpo_dp_link_enc = pool->hpo_dp_link_enc[enc_index];
4066 
4067 	return hpo_dp_link_enc;
4068 }
4069 
get_temp_dp_link_res(struct dc_link * link,struct link_resource * link_res,struct dc_link_settings * link_settings)4070 bool get_temp_dp_link_res(struct dc_link *link,
4071 		struct link_resource *link_res,
4072 		struct dc_link_settings *link_settings)
4073 {
4074 	const struct dc *dc  = link->dc;
4075 	const struct resource_context *res_ctx = &dc->current_state->res_ctx;
4076 
4077 	memset(link_res, 0, sizeof(*link_res));
4078 
4079 	if (dc->link_srv->dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING) {
4080 		link_res->hpo_dp_link_enc = get_temp_hpo_dp_link_enc(res_ctx,
4081 				dc->res_pool, link);
4082 		if (!link_res->hpo_dp_link_enc)
4083 			return false;
4084 	}
4085 	return true;
4086 }
4087 
reset_syncd_pipes_from_disabled_pipes(struct dc * dc,struct dc_state * context)4088 void reset_syncd_pipes_from_disabled_pipes(struct dc *dc,
4089 		struct dc_state *context)
4090 {
4091 	int i, j;
4092 	struct pipe_ctx *pipe_ctx_old, *pipe_ctx, *pipe_ctx_syncd;
4093 
4094 	/* If pipe backend is reset, need to reset pipe syncd status */
4095 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4096 		pipe_ctx_old =	&dc->current_state->res_ctx.pipe_ctx[i];
4097 		pipe_ctx = &context->res_ctx.pipe_ctx[i];
4098 
4099 		if (!resource_is_pipe_type(pipe_ctx_old, OTG_MASTER))
4100 			continue;
4101 
4102 		if (!pipe_ctx->stream ||
4103 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
4104 
4105 			/* Reset all the syncd pipes from the disabled pipe */
4106 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
4107 				pipe_ctx_syncd = &context->res_ctx.pipe_ctx[j];
4108 				if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_syncd) == pipe_ctx_old->pipe_idx) ||
4109 					!IS_PIPE_SYNCD_VALID(pipe_ctx_syncd))
4110 					SET_PIPE_SYNCD_TO_PIPE(pipe_ctx_syncd, j);
4111 			}
4112 		}
4113 	}
4114 }
4115 
check_syncd_pipes_for_disabled_master_pipe(struct dc * dc,struct dc_state * context,uint8_t disabled_master_pipe_idx)4116 void check_syncd_pipes_for_disabled_master_pipe(struct dc *dc,
4117 	struct dc_state *context,
4118 	uint8_t disabled_master_pipe_idx)
4119 {
4120 	int i;
4121 	struct pipe_ctx *pipe_ctx, *pipe_ctx_check;
4122 
4123 	pipe_ctx = &context->res_ctx.pipe_ctx[disabled_master_pipe_idx];
4124 	if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx) != disabled_master_pipe_idx) ||
4125 		!IS_PIPE_SYNCD_VALID(pipe_ctx))
4126 		SET_PIPE_SYNCD_TO_PIPE(pipe_ctx, disabled_master_pipe_idx);
4127 
4128 	/* for the pipe disabled, check if any slave pipe exists and assert */
4129 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4130 		pipe_ctx_check = &context->res_ctx.pipe_ctx[i];
4131 
4132 		if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_check) == disabled_master_pipe_idx) &&
4133 		    IS_PIPE_SYNCD_VALID(pipe_ctx_check) && (i != disabled_master_pipe_idx)) {
4134 			struct pipe_ctx *first_pipe = pipe_ctx_check;
4135 
4136 			while (first_pipe->prev_odm_pipe)
4137 				first_pipe = first_pipe->prev_odm_pipe;
4138 			/* When ODM combine is enabled, this case is expected. If the disabled pipe
4139 			 * is part of the ODM tree, then we should not print an error.
4140 			 * */
4141 			if (first_pipe->pipe_idx == disabled_master_pipe_idx)
4142 				continue;
4143 
4144 			DC_ERR("DC: Failure: pipe_idx[%d] syncd with disabled master pipe_idx[%d]\n",
4145 				   i, disabled_master_pipe_idx);
4146 		}
4147 	}
4148 }
4149 
reset_sync_context_for_pipe(const struct dc * dc,struct dc_state * context,uint8_t pipe_idx)4150 void reset_sync_context_for_pipe(const struct dc *dc,
4151 	struct dc_state *context,
4152 	uint8_t pipe_idx)
4153 {
4154 	int i;
4155 	struct pipe_ctx *pipe_ctx_reset;
4156 
4157 	/* reset the otg sync context for the pipe and its slave pipes if any */
4158 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4159 		pipe_ctx_reset = &context->res_ctx.pipe_ctx[i];
4160 
4161 		if (((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_reset) == pipe_idx) &&
4162 			IS_PIPE_SYNCD_VALID(pipe_ctx_reset)) || (i == pipe_idx))
4163 			SET_PIPE_SYNCD_TO_PIPE(pipe_ctx_reset, i);
4164 	}
4165 }
4166 
resource_transmitter_to_phy_idx(const struct dc * dc,enum transmitter transmitter)4167 uint8_t resource_transmitter_to_phy_idx(const struct dc *dc, enum transmitter transmitter)
4168 {
4169 	/* TODO - get transmitter to phy idx mapping from DMUB */
4170 	uint8_t phy_idx = transmitter - TRANSMITTER_UNIPHY_A;
4171 
4172 	if (dc->ctx->dce_version == DCN_VERSION_3_1 &&
4173 			dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0) {
4174 		switch (transmitter) {
4175 		case TRANSMITTER_UNIPHY_A:
4176 			phy_idx = 0;
4177 			break;
4178 		case TRANSMITTER_UNIPHY_B:
4179 			phy_idx = 1;
4180 			break;
4181 		case TRANSMITTER_UNIPHY_C:
4182 			phy_idx = 5;
4183 			break;
4184 		case TRANSMITTER_UNIPHY_D:
4185 			phy_idx = 6;
4186 			break;
4187 		case TRANSMITTER_UNIPHY_E:
4188 			phy_idx = 4;
4189 			break;
4190 		default:
4191 			phy_idx = 0;
4192 			break;
4193 		}
4194 	}
4195 
4196 	return phy_idx;
4197 }
4198 
get_link_hwss(const struct dc_link * link,const struct link_resource * link_res)4199 const struct link_hwss *get_link_hwss(const struct dc_link *link,
4200 		const struct link_resource *link_res)
4201 {
4202 	/* Link_hwss is only accessible by getter function instead of accessing
4203 	 * by pointers in dc with the intent to protect against breaking polymorphism.
4204 	 */
4205 	if (can_use_hpo_dp_link_hwss(link, link_res))
4206 		/* TODO: some assumes that if decided link settings is 128b/132b
4207 		 * channel coding format hpo_dp_link_enc should be used.
4208 		 * Others believe that if hpo_dp_link_enc is available in link
4209 		 * resource then hpo_dp_link_enc must be used. This bound between
4210 		 * hpo_dp_link_enc != NULL and decided link settings is loosely coupled
4211 		 * with a premise that both hpo_dp_link_enc pointer and decided link
4212 		 * settings are determined based on single policy function like
4213 		 * "decide_link_settings" from upper layer. This "convention"
4214 		 * cannot be maintained and enforced at current level.
4215 		 * Therefore a refactor is due so we can enforce a strong bound
4216 		 * between those two parameters at this level.
4217 		 *
4218 		 * To put it simple, we want to make enforcement at low level so that
4219 		 * we will not return link hwss if caller plans to do 8b/10b
4220 		 * with an hpo encoder. Or we can return a very dummy one that doesn't
4221 		 * do work for all functions
4222 		 */
4223 		return (requires_fixed_vs_pe_retimer_hpo_link_hwss(link) ?
4224 				get_hpo_fixed_vs_pe_retimer_dp_link_hwss() : get_hpo_dp_link_hwss());
4225 	else if (can_use_dpia_link_hwss(link, link_res))
4226 		return get_dpia_link_hwss();
4227 	else if (can_use_dio_link_hwss(link, link_res))
4228 		return (requires_fixed_vs_pe_retimer_dio_link_hwss(link)) ?
4229 				get_dio_fixed_vs_pe_retimer_link_hwss() : get_dio_link_hwss();
4230 	else
4231 		return get_virtual_link_hwss();
4232 }
4233 
is_h_timing_divisible_by_2(struct dc_stream_state * stream)4234 bool is_h_timing_divisible_by_2(struct dc_stream_state *stream)
4235 {
4236 	bool divisible = false;
4237 	uint16_t h_blank_start = 0;
4238 	uint16_t h_blank_end = 0;
4239 
4240 	if (stream) {
4241 		h_blank_start = stream->timing.h_total - stream->timing.h_front_porch;
4242 		h_blank_end = h_blank_start - stream->timing.h_addressable;
4243 
4244 		/* HTOTAL, Hblank start/end, and Hsync start/end all must be
4245 		 * divisible by 2 in order for the horizontal timing params
4246 		 * to be considered divisible by 2. Hsync start is always 0.
4247 		 */
4248 		divisible = (stream->timing.h_total % 2 == 0) &&
4249 				(h_blank_start % 2 == 0) &&
4250 				(h_blank_end % 2 == 0) &&
4251 				(stream->timing.h_sync_width % 2 == 0);
4252 	}
4253 	return divisible;
4254 }
4255 
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)4256 bool dc_resource_acquire_secondary_pipe_for_mpc_odm(
4257 		const struct dc *dc,
4258 		struct dc_state *state,
4259 		struct pipe_ctx *pri_pipe,
4260 		struct pipe_ctx *sec_pipe,
4261 		bool odm)
4262 {
4263 	int pipe_idx = sec_pipe->pipe_idx;
4264 	struct pipe_ctx *sec_top, *sec_bottom, *sec_next, *sec_prev;
4265 	const struct resource_pool *pool = dc->res_pool;
4266 
4267 	sec_top = sec_pipe->top_pipe;
4268 	sec_bottom = sec_pipe->bottom_pipe;
4269 	sec_next = sec_pipe->next_odm_pipe;
4270 	sec_prev = sec_pipe->prev_odm_pipe;
4271 
4272 	*sec_pipe = *pri_pipe;
4273 
4274 	sec_pipe->top_pipe = sec_top;
4275 	sec_pipe->bottom_pipe = sec_bottom;
4276 	sec_pipe->next_odm_pipe = sec_next;
4277 	sec_pipe->prev_odm_pipe = sec_prev;
4278 
4279 	sec_pipe->pipe_idx = pipe_idx;
4280 	sec_pipe->plane_res.mi = pool->mis[pipe_idx];
4281 	sec_pipe->plane_res.hubp = pool->hubps[pipe_idx];
4282 	sec_pipe->plane_res.ipp = pool->ipps[pipe_idx];
4283 	sec_pipe->plane_res.xfm = pool->transforms[pipe_idx];
4284 	sec_pipe->plane_res.dpp = pool->dpps[pipe_idx];
4285 	sec_pipe->plane_res.mpcc_inst = pool->dpps[pipe_idx]->inst;
4286 	sec_pipe->stream_res.dsc = NULL;
4287 	if (odm) {
4288 		if (!sec_pipe->top_pipe)
4289 			sec_pipe->stream_res.opp = pool->opps[pipe_idx];
4290 		else
4291 			sec_pipe->stream_res.opp = sec_pipe->top_pipe->stream_res.opp;
4292 		if (sec_pipe->stream->timing.flags.DSC == 1) {
4293 #if defined(CONFIG_DRM_AMD_DC_FP)
4294 			dcn20_acquire_dsc(dc, &state->res_ctx, &sec_pipe->stream_res.dsc, pipe_idx);
4295 #endif
4296 			ASSERT(sec_pipe->stream_res.dsc);
4297 			if (sec_pipe->stream_res.dsc == NULL)
4298 				return false;
4299 		}
4300 #if defined(CONFIG_DRM_AMD_DC_FP)
4301 		dcn20_build_mapped_resource(dc, state, sec_pipe->stream);
4302 #endif
4303 	}
4304 
4305 	return true;
4306 }
4307 
update_dp_encoder_resources_for_test_harness(const struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe_ctx)4308 enum dc_status update_dp_encoder_resources_for_test_harness(const struct dc *dc,
4309 		struct dc_state *context,
4310 		struct pipe_ctx *pipe_ctx)
4311 {
4312 	if (dc->link_srv->dp_get_encoding_format(&pipe_ctx->link_config.dp_link_settings) == DP_128b_132b_ENCODING) {
4313 		if (pipe_ctx->stream_res.hpo_dp_stream_enc == NULL) {
4314 			pipe_ctx->stream_res.hpo_dp_stream_enc =
4315 					find_first_free_match_hpo_dp_stream_enc_for_link(
4316 							&context->res_ctx, dc->res_pool, pipe_ctx->stream);
4317 
4318 			if (!pipe_ctx->stream_res.hpo_dp_stream_enc)
4319 				return DC_NO_STREAM_ENC_RESOURCE;
4320 
4321 			update_hpo_dp_stream_engine_usage(
4322 					&context->res_ctx, dc->res_pool,
4323 					pipe_ctx->stream_res.hpo_dp_stream_enc,
4324 					true);
4325 		}
4326 
4327 		if (pipe_ctx->link_res.hpo_dp_link_enc == NULL) {
4328 			if (!add_hpo_dp_link_enc_to_ctx(&context->res_ctx, dc->res_pool, pipe_ctx, pipe_ctx->stream))
4329 				return DC_NO_LINK_ENC_RESOURCE;
4330 		}
4331 	} else {
4332 		if (pipe_ctx->stream_res.hpo_dp_stream_enc) {
4333 			update_hpo_dp_stream_engine_usage(
4334 					&context->res_ctx, dc->res_pool,
4335 					pipe_ctx->stream_res.hpo_dp_stream_enc,
4336 					false);
4337 			pipe_ctx->stream_res.hpo_dp_stream_enc = NULL;
4338 		}
4339 		if (pipe_ctx->link_res.hpo_dp_link_enc)
4340 			remove_hpo_dp_link_enc_from_ctx(&context->res_ctx, pipe_ctx, pipe_ctx->stream);
4341 	}
4342 
4343 	return DC_OK;
4344 }
4345 
4346