xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc_resource.c (revision 7e3c6322940e13f6630d7cfdc5834f4e69cff4f7)
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 "dio/virtual/virtual_stream_encoder.h"
41 #include "dpcd_defs.h"
42 #include "link_enc_cfg.h"
43 #include "link_service.h"
44 #include "clk_mgr.h"
45 #include "dc_state_priv.h"
46 #include "dc_stream_priv.h"
47 
48 #include "link/hwss/link_hwss_virtual.h"
49 #include "link/hwss/link_hwss_dio.h"
50 #include "link/hwss/link_hwss_dpia.h"
51 #include "link/hwss/link_hwss_hpo_dp.h"
52 #include "link/hwss/link_hwss_dio_fixed_vs_pe_retimer.h"
53 #include "link/hwss/link_hwss_hpo_fixed_vs_pe_retimer_dp.h"
54 
55 #if defined(CONFIG_DRM_AMD_DC_SI)
56 #include "dce60/dce60_resource.h"
57 #endif
58 #if defined(CONFIG_DRM_AMD_DC_DCE)
59 #include "dce80/dce80_resource.h"
60 #include "dce100/dce100_resource.h"
61 #include "dce110/dce110_resource.h"
62 #include "dce112/dce112_resource.h"
63 #include "dce120/dce120_resource.h"
64 #endif
65 #include "dcn10/dcn10_resource.h"
66 #include "dcn20/dcn20_resource.h"
67 #include "dcn21/dcn21_resource.h"
68 #include "dcn201/dcn201_resource.h"
69 #include "dcn30/dcn30_resource.h"
70 #include "dcn301/dcn301_resource.h"
71 #include "dcn302/dcn302_resource.h"
72 #include "dcn303/dcn303_resource.h"
73 #include "dcn31/dcn31_resource.h"
74 #include "dcn314/dcn314_resource.h"
75 #include "dcn315/dcn315_resource.h"
76 #include "dcn316/dcn316_resource.h"
77 #include "dcn32/dcn32_resource.h"
78 #include "dcn321/dcn321_resource.h"
79 #include "dcn35/dcn35_resource.h"
80 #include "dcn351/dcn351_resource.h"
81 #include "dcn36/dcn36_resource.h"
82 #include "dcn401/dcn401_resource.h"
83 #include "dcn42/dcn42_resource.h"
84 #include "dcn42b/dcn42b_resource.h"
85 #include "dcn60/dcn60_resource.h"
86 #if defined(CONFIG_DRM_AMD_DC_FP)
87 #include "dc_spl_translate.h"
88 #endif
89 
90 #define VISUAL_CONFIRM_BASE_DEFAULT 3
91 #define VISUAL_CONFIRM_BASE_MIN 1
92 #define VISUAL_CONFIRM_BASE_MAX 10
93 /* we choose 240 because it is a common denominator of common v addressable
94  * such as 2160, 1440, 1200, 960. So we take 1/240 portion of v addressable as
95  * the visual confirm dpp offset height. So visual confirm height can stay
96  * relatively the same independent from timing used.
97  */
98 #define VISUAL_CONFIRM_DPP_OFFSET_DENO 240
99 
100 #define DC_LOGGER \
101 	dc->ctx->logger
102 #define DC_LOGGER_INIT(logger)
103 #include "link/hwss/link_hwss_hpo_frl.h"
104 #include "dml/dml1_frl_cap_chk.h"
105 #include "dml2_wrapper/dml2_wrapper.h"
106 
107 #define UNABLE_TO_SPLIT -1
108 
109 static void capture_pipe_topology_data(struct dc *dc, int plane_idx, int slice_idx, int stream_idx,
110 									   int dpp_inst, int opp_inst, int tg_inst, bool is_phantom_pipe)
111 {
112 	struct pipe_topology_snapshot *current_snapshot = &dc->debug_data.topology_history.snapshots[dc->debug_data.topology_history.current_snapshot_index];
113 
114 	if (current_snapshot->line_count >= MAX_PIPES)
115 		return;
116 
117 	current_snapshot->pipe_log_lines[current_snapshot->line_count].is_phantom_pipe = is_phantom_pipe;
118 	current_snapshot->pipe_log_lines[current_snapshot->line_count].plane_idx = plane_idx;
119 	current_snapshot->pipe_log_lines[current_snapshot->line_count].slice_idx = slice_idx;
120 	current_snapshot->pipe_log_lines[current_snapshot->line_count].stream_idx = stream_idx;
121 	current_snapshot->pipe_log_lines[current_snapshot->line_count].dpp_inst = dpp_inst;
122 	current_snapshot->pipe_log_lines[current_snapshot->line_count].opp_inst = opp_inst;
123 	current_snapshot->pipe_log_lines[current_snapshot->line_count].tg_inst = tg_inst;
124 
125 	current_snapshot->line_count++;
126 }
127 
128 static void start_new_topology_snapshot(struct dc *dc, struct dc_state *state)
129 {
130 	// Move to next snapshot slot (circular buffer)
131 	dc->debug_data.topology_history.current_snapshot_index = (dc->debug_data.topology_history.current_snapshot_index + 1) % MAX_TOPOLOGY_SNAPSHOTS;
132 
133 	// Clear the new snapshot
134 	struct pipe_topology_snapshot *current_snapshot = &dc->debug_data.topology_history.snapshots[dc->debug_data.topology_history.current_snapshot_index];
135 	memset(current_snapshot, 0, sizeof(*current_snapshot));
136 
137 	// Set metadata
138 	current_snapshot->timestamp_us = dm_get_timestamp(dc->ctx);
139 	current_snapshot->stream_count = state->stream_count;
140 	current_snapshot->phantom_stream_count = state->phantom_stream_count;
141 }
142 
143 enum dce_version resource_parse_asic_id(struct hw_asic_id asic_id)
144 {
145 	enum dce_version dc_version = DCE_VERSION_UNKNOWN;
146 
147 	switch (asic_id.chip_family) {
148 
149 #if defined(CONFIG_DRM_AMD_DC_SI)
150 	case FAMILY_SI:
151 		if (ASIC_REV_IS_TAHITI_P(asic_id.hw_internal_rev) ||
152 		    ASIC_REV_IS_PITCAIRN_PM(asic_id.hw_internal_rev) ||
153 		    ASIC_REV_IS_CAPEVERDE_M(asic_id.hw_internal_rev))
154 			dc_version = DCE_VERSION_6_0;
155 		else if (ASIC_REV_IS_OLAND_M(asic_id.hw_internal_rev))
156 			dc_version = DCE_VERSION_6_4;
157 		else
158 			dc_version = DCE_VERSION_6_1;
159 		break;
160 #endif
161 	case FAMILY_CI:
162 		dc_version = DCE_VERSION_8_0;
163 		break;
164 	case FAMILY_KV:
165 		if (ASIC_REV_IS_KALINDI(asic_id.hw_internal_rev) ||
166 		    ASIC_REV_IS_BHAVANI(asic_id.hw_internal_rev) ||
167 		    ASIC_REV_IS_GODAVARI(asic_id.hw_internal_rev))
168 			dc_version = DCE_VERSION_8_3;
169 		else
170 			dc_version = DCE_VERSION_8_1;
171 		break;
172 	case FAMILY_CZ:
173 		dc_version = DCE_VERSION_11_0;
174 		break;
175 
176 	case FAMILY_VI:
177 		if (ASIC_REV_IS_TONGA_P(asic_id.hw_internal_rev) ||
178 				ASIC_REV_IS_FIJI_P(asic_id.hw_internal_rev)) {
179 			dc_version = DCE_VERSION_10_0;
180 			break;
181 		}
182 		if (ASIC_REV_IS_POLARIS10_P(asic_id.hw_internal_rev) ||
183 				ASIC_REV_IS_POLARIS11_M(asic_id.hw_internal_rev) ||
184 				ASIC_REV_IS_POLARIS12_V(asic_id.hw_internal_rev)) {
185 			dc_version = DCE_VERSION_11_2;
186 		}
187 		if (ASIC_REV_IS_VEGAM(asic_id.hw_internal_rev))
188 			dc_version = DCE_VERSION_11_22;
189 		break;
190 	case FAMILY_AI:
191 		if (ASICREV_IS_VEGA20_P(asic_id.hw_internal_rev))
192 			dc_version = DCE_VERSION_12_1;
193 		else
194 			dc_version = DCE_VERSION_12_0;
195 		break;
196 	case FAMILY_RV:
197 		dc_version = DCN_VERSION_1_0;
198 		if (ASICREV_IS_RAVEN2(asic_id.hw_internal_rev))
199 			dc_version = DCN_VERSION_1_01;
200 		if (ASICREV_IS_RENOIR(asic_id.hw_internal_rev))
201 			dc_version = DCN_VERSION_2_1;
202 		if (ASICREV_IS_GREEN_SARDINE(asic_id.hw_internal_rev))
203 			dc_version = DCN_VERSION_2_1;
204 		break;
205 
206 	case FAMILY_NV:
207 		dc_version = DCN_VERSION_2_0;
208 		if (asic_id.chip_id == DEVICE_ID_NV_13FE ||
209 		    asic_id.chip_id == DEVICE_ID_NV_143F ||
210 		    asic_id.chip_id == DEVICE_ID_NV_13F9 ||
211 		    asic_id.chip_id == DEVICE_ID_NV_13FA ||
212 		    asic_id.chip_id == DEVICE_ID_NV_13FB ||
213 		    asic_id.chip_id == DEVICE_ID_NV_13FC ||
214 		    asic_id.chip_id == DEVICE_ID_NV_13DB) {
215 			dc_version = DCN_VERSION_2_01;
216 			break;
217 		}
218 		if (ASICREV_IS_SIENNA_CICHLID_P(asic_id.hw_internal_rev))
219 			dc_version = DCN_VERSION_3_0;
220 		if (ASICREV_IS_DIMGREY_CAVEFISH_P(asic_id.hw_internal_rev))
221 			dc_version = DCN_VERSION_3_02;
222 		if (ASICREV_IS_BEIGE_GOBY_P(asic_id.hw_internal_rev))
223 			dc_version = DCN_VERSION_3_03;
224 		break;
225 
226 	case FAMILY_VGH:
227 		dc_version = DCN_VERSION_3_01;
228 		break;
229 
230 	case FAMILY_YELLOW_CARP:
231 		if (ASICREV_IS_YELLOW_CARP(asic_id.hw_internal_rev))
232 			dc_version = DCN_VERSION_3_1;
233 		break;
234 	case AMDGPU_FAMILY_GC_10_3_6:
235 		if (ASICREV_IS_GC_10_3_6(asic_id.hw_internal_rev))
236 			dc_version = DCN_VERSION_3_15;
237 		break;
238 	case AMDGPU_FAMILY_GC_10_3_7:
239 		if (ASICREV_IS_GC_10_3_7(asic_id.hw_internal_rev))
240 			dc_version = DCN_VERSION_3_16;
241 		break;
242 	case AMDGPU_FAMILY_GC_11_0_0:
243 		dc_version = DCN_VERSION_3_2;
244 		if (ASICREV_IS_GC_11_0_2(asic_id.hw_internal_rev))
245 			dc_version = DCN_VERSION_3_21;
246 		break;
247 	case AMDGPU_FAMILY_GC_11_0_1:
248 		dc_version = DCN_VERSION_3_14;
249 		break;
250 	case AMDGPU_FAMILY_GC_11_5_0:
251 		dc_version = DCN_VERSION_3_5;
252 		if (ASICREV_IS_DCN4A_SOC_VAR_B(asic_id.hw_internal_rev))
253 			dc_version = DCN_VERSION_4_2B;
254 		if (ASICREV_IS_GC_11_0_4(asic_id.hw_internal_rev))
255 			dc_version = DCN_VERSION_3_51;
256 		if (ASICREV_IS_DCN36(asic_id.hw_internal_rev))
257 			dc_version = DCN_VERSION_3_6;
258 		break;
259 	case AMDGPU_FAMILY_GC_12_0_0:
260 		if (ASICREV_IS_GC_12_0_1_A0(asic_id.hw_internal_rev) ||
261 			ASICREV_IS_GC_12_0_0_A0(asic_id.hw_internal_rev))
262 			dc_version = DCN_VERSION_4_01;
263 		break;
264 	case AMDGPU_FAMILY_GC_11_5_4:
265 			dc_version = DCN_VERSION_4_2;
266 	break;
267 	case AMDGPU_FAMILY_GC_13_0_1:
268 			dc_version = DCN_VERSION_6_0;
269 		break;
270 	default:
271 		dc_version = DCE_VERSION_UNKNOWN;
272 		break;
273 	}
274 	return dc_version;
275 }
276 
277 struct resource_pool *dc_create_resource_pool(struct dc  *dc,
278 					      const struct dc_init_data *init_data,
279 					      enum dce_version dc_version)
280 {
281 	struct resource_pool *res_pool = NULL;
282 
283 	switch (dc_version) {
284 #if defined(CONFIG_DRM_AMD_DC_SI)
285 	case DCE_VERSION_6_0:
286 		res_pool = dce60_create_resource_pool(
287 			init_data->num_virtual_links, dc);
288 		break;
289 	case DCE_VERSION_6_1:
290 		res_pool = dce61_create_resource_pool(
291 			init_data->num_virtual_links, dc);
292 		break;
293 	case DCE_VERSION_6_4:
294 		res_pool = dce64_create_resource_pool(
295 			init_data->num_virtual_links, dc);
296 		break;
297 #endif
298 #if defined(CONFIG_DRM_AMD_DC_DCE)
299 	case DCE_VERSION_8_0:
300 		res_pool = dce80_create_resource_pool(
301 				(uint8_t)init_data->num_virtual_links, dc);
302 		break;
303 	case DCE_VERSION_8_1:
304 		res_pool = dce81_create_resource_pool(
305 				(uint8_t)init_data->num_virtual_links, dc);
306 		break;
307 	case DCE_VERSION_8_3:
308 		res_pool = dce83_create_resource_pool(
309 				(uint8_t)init_data->num_virtual_links, dc);
310 		break;
311 	case DCE_VERSION_10_0:
312 		res_pool = dce100_create_resource_pool(
313 				(uint8_t)init_data->num_virtual_links, dc);
314 		break;
315 	case DCE_VERSION_11_0:
316 		res_pool = dce110_create_resource_pool(
317 				(uint8_t)init_data->num_virtual_links, dc,
318 				init_data->asic_id);
319 		break;
320 	case DCE_VERSION_11_2:
321 	case DCE_VERSION_11_22:
322 		res_pool = dce112_create_resource_pool(
323 				(uint8_t)init_data->num_virtual_links, dc);
324 		break;
325 	case DCE_VERSION_12_0:
326 	case DCE_VERSION_12_1:
327 		res_pool = dce120_create_resource_pool(
328 				(uint8_t)init_data->num_virtual_links, dc);
329 		break;
330 #endif
331 
332 #if defined(CONFIG_DRM_AMD_DC_FP)
333 	case DCN_VERSION_1_0:
334 	case DCN_VERSION_1_01:
335 		res_pool = dcn10_create_resource_pool(init_data, dc);
336 		break;
337 	case DCN_VERSION_2_0:
338 		res_pool = dcn20_create_resource_pool(init_data, dc);
339 		break;
340 	case DCN_VERSION_2_1:
341 		res_pool = dcn21_create_resource_pool(init_data, dc);
342 		break;
343 	case DCN_VERSION_2_01:
344 		res_pool = dcn201_create_resource_pool(init_data, dc);
345 		break;
346 	case DCN_VERSION_3_0:
347 		res_pool = dcn30_create_resource_pool(init_data, dc);
348 		break;
349 	case DCN_VERSION_3_01:
350 		res_pool = dcn301_create_resource_pool(init_data, dc);
351 		break;
352 	case DCN_VERSION_3_02:
353 		res_pool = dcn302_create_resource_pool(init_data, dc);
354 		break;
355 	case DCN_VERSION_3_03:
356 		res_pool = dcn303_create_resource_pool(init_data, dc);
357 		break;
358 	case DCN_VERSION_3_1:
359 		res_pool = dcn31_create_resource_pool(init_data, dc);
360 		break;
361 	case DCN_VERSION_3_14:
362 		res_pool = dcn314_create_resource_pool(init_data, dc);
363 		break;
364 	case DCN_VERSION_3_15:
365 		res_pool = dcn315_create_resource_pool(init_data, dc);
366 		break;
367 	case DCN_VERSION_3_16:
368 		res_pool = dcn316_create_resource_pool(init_data, dc);
369 		break;
370 	case DCN_VERSION_3_2:
371 		res_pool = dcn32_create_resource_pool(init_data, dc);
372 		break;
373 	case DCN_VERSION_3_21:
374 		res_pool = dcn321_create_resource_pool(init_data, dc);
375 		break;
376 	case DCN_VERSION_3_5:
377 		res_pool = dcn35_create_resource_pool(init_data, dc);
378 		break;
379 	case DCN_VERSION_3_51:
380 		res_pool = dcn351_create_resource_pool(init_data, dc);
381 		break;
382 	case DCN_VERSION_3_6:
383 		res_pool = dcn36_create_resource_pool(init_data, dc);
384 		break;
385 	case DCN_VERSION_4_01:
386 		res_pool = dcn401_create_resource_pool(init_data, dc);
387 		break;
388 	case DCN_VERSION_4_2:
389 		res_pool = dcn42_create_resource_pool(init_data, dc);
390 		break;
391 	case DCN_VERSION_4_2B:
392 		res_pool = dcn42b_create_resource_pool(init_data, dc);
393 		break;
394 	case DCN_VERSION_6_0:
395 		res_pool = dcn60_create_resource_pool(init_data, dc);
396 		break;
397 #endif /* CONFIG_DRM_AMD_DC_FP */
398 	default:
399 		break;
400 	}
401 
402 	if (res_pool != NULL) {
403 		if (dc->ctx->dc_bios->fw_info_valid) {
404 			res_pool->ref_clocks.xtalin_clock_inKhz =
405 				dc->ctx->dc_bios->fw_info.pll_info.crystal_frequency;
406 			/* initialize with firmware data first, no all
407 			 * ASIC have DCCG SW component. FPGA or
408 			 * simulation need initialization of
409 			 * dccg_ref_clock_inKhz, dchub_ref_clock_inKhz
410 			 * with xtalin_clock_inKhz
411 			 */
412 			res_pool->ref_clocks.dccg_ref_clock_inKhz =
413 				res_pool->ref_clocks.xtalin_clock_inKhz;
414 			res_pool->ref_clocks.dchub_ref_clock_inKhz =
415 				res_pool->ref_clocks.xtalin_clock_inKhz;
416 		} else
417 			ASSERT_CRITICAL(false);
418 	}
419 
420 	return res_pool;
421 }
422 
423 void dc_destroy_resource_pool(struct dc *dc)
424 {
425 	if (dc) {
426 		if (dc->res_pool)
427 			dc->res_pool->funcs->destroy(&dc->res_pool);
428 
429 		kfree(dc->hwseq);
430 	}
431 }
432 
433 static void update_num_audio(
434 	const struct resource_straps *straps,
435 	unsigned int *num_audio,
436 	struct audio_support *aud_support)
437 {
438 	aud_support->dp_audio = true;
439 	aud_support->hdmi_audio_native = false;
440 	aud_support->hdmi_audio_on_dongle = false;
441 
442 	if (straps->hdmi_disable == 0) {
443 		if (straps->dc_pinstraps_audio & 0x2) {
444 			aud_support->hdmi_audio_on_dongle = true;
445 			aud_support->hdmi_audio_native = true;
446 		}
447 	}
448 
449 	switch (straps->audio_stream_number) {
450 	case 0: /* multi streams supported */
451 		break;
452 	case 1: /* multi streams not supported */
453 		*num_audio = 1;
454 		break;
455 	default:
456 		DC_ERR("DC: unexpected audio fuse!\n");
457 	}
458 }
459 
460 bool resource_construct(
461 	unsigned int num_virtual_links,
462 	struct dc  *dc,
463 	struct resource_pool *pool,
464 	const struct resource_create_funcs *create_funcs)
465 {
466 	struct dc_context *ctx = dc->ctx;
467 	const struct resource_caps *caps = pool->res_cap;
468 	unsigned int i;
469 	unsigned int num_audio = caps->num_audio;
470 	struct resource_straps straps = {0};
471 
472 	if (create_funcs->read_dce_straps)
473 		create_funcs->read_dce_straps(dc->ctx, &straps);
474 
475 	pool->audio_count = 0;
476 	if (create_funcs->create_audio) {
477 		/* find the total number of streams available via the
478 		 * AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT
479 		 * registers (one for each pin) starting from pin 1
480 		 * up to the max number of audio pins.
481 		 * We stop on the first pin where
482 		 * PORT_CONNECTIVITY == 1 (as instructed by HW team).
483 		 */
484 		update_num_audio(&straps, &num_audio, &pool->audio_support);
485 		for (i = 0; i < (unsigned int)caps->num_audio; i++) {
486 			struct audio *aud = create_funcs->create_audio(ctx, i);
487 
488 			if (aud == NULL) {
489 				DC_ERR("DC: failed to create audio!\n");
490 				return false;
491 			}
492 			if (!aud->funcs->endpoint_valid(aud)) {
493 				aud->funcs->destroy(&aud);
494 				break;
495 			}
496 			pool->audios[i] = aud;
497 			pool->audio_count++;
498 		}
499 	}
500 
501 	pool->stream_enc_count = 0;
502 	if (create_funcs->create_stream_encoder) {
503 		for (i = 0; i < (unsigned int)caps->num_stream_encoder; i++) {
504 			pool->stream_enc[i] = create_funcs->create_stream_encoder(i, ctx);
505 			if (pool->stream_enc[i] == NULL)
506 				DC_ERR("DC: failed to create stream_encoder!\n");
507 			pool->stream_enc_count++;
508 		}
509 
510 		for (i = 0; i < (unsigned int)caps->num_analog_stream_encoder; i++) {
511 			pool->stream_enc[caps->num_stream_encoder + i] =
512 				create_funcs->create_stream_encoder(ENGINE_ID_DACA + i, ctx);
513 			if (pool->stream_enc[caps->num_stream_encoder + i] == NULL)
514 				DC_ERR("DC: failed to create analog stream_encoder %d!\n", i);
515 			pool->stream_enc_count++;
516 		}
517 	}
518 
519 	pool->hpo_frl_stream_enc_count = 0;
520 	if (create_funcs->create_hpo_frl_stream_encoder) {
521 		for (i = 0; i < (unsigned int)caps->num_hpo_frl; i++) {
522 			pool->hpo_frl_stream_enc[i] = create_funcs->create_hpo_frl_stream_encoder(i+ENGINE_ID_HPO_0, ctx);
523 			if (pool->hpo_frl_stream_enc[i] == NULL)
524 				DC_ERR("DC: failed to create HPO FRL stream encoder!\n");
525 			pool->hpo_frl_stream_enc_count++;
526 
527 		}
528 	}
529 	pool->hpo_frl_link_enc_count = 0;
530 	if (create_funcs->create_hpo_frl_link_encoder) {
531 		for (i = 0; i < (unsigned int)caps->num_hpo_frl; i++) {
532 			pool->hpo_frl_link_enc[i] = create_funcs->create_hpo_frl_link_encoder(i+ENGINE_ID_HPO_0, ctx);
533 			if (pool->hpo_frl_link_enc[i] == NULL)
534 				DC_ERR("DC: failed to create HPO FRL link encoder!\n");
535 			pool->hpo_frl_link_enc_count++;
536 		}
537 	}
538 	pool->hpo_dp_stream_enc_count = 0;
539 	if (create_funcs->create_hpo_dp_stream_encoder) {
540 		for (i = 0; i < (unsigned int)caps->num_hpo_dp_stream_encoder; i++) {
541 			pool->hpo_dp_stream_enc[i] = create_funcs->create_hpo_dp_stream_encoder(i+ENGINE_ID_HPO_DP_0, ctx);
542 			if (pool->hpo_dp_stream_enc[i] == NULL)
543 				DC_ERR("DC: failed to create HPO DP stream encoder!\n");
544 			pool->hpo_dp_stream_enc_count++;
545 
546 		}
547 	}
548 
549 	pool->hpo_dp_link_enc_count = 0;
550 	if (create_funcs->create_hpo_dp_link_encoder) {
551 		for (i = 0; i < (unsigned int)caps->num_hpo_dp_link_encoder; i++) {
552 			pool->hpo_dp_link_enc[i] = create_funcs->create_hpo_dp_link_encoder((uint8_t)i, ctx);
553 			if (pool->hpo_dp_link_enc[i] == NULL)
554 				DC_ERR("DC: failed to create HPO DP link encoder!\n");
555 			pool->hpo_dp_link_enc_count++;
556 		}
557 	}
558 
559 	for (i = 0; i < (unsigned int)caps->num_mpc_3dlut; i++) {
560 		pool->mpc_lut[i] = dc_create_3dlut_func();
561 		if (pool->mpc_lut[i] == NULL)
562 			DC_ERR("DC: failed to create MPC 3dlut!\n");
563 		pool->mpc_shaper[i] = dc_create_transfer_func();
564 		if (pool->mpc_shaper[i] == NULL)
565 			DC_ERR("DC: failed to create MPC shaper!\n");
566 	}
567 
568 	dc->caps.dynamic_audio = false;
569 	if (pool->audio_count < pool->stream_enc_count) {
570 		dc->caps.dynamic_audio = true;
571 	}
572 	for (i = 0; i < num_virtual_links; i++) {
573 		pool->stream_enc[pool->stream_enc_count] =
574 			virtual_stream_encoder_create(
575 					ctx, ctx->dc_bios);
576 		if (pool->stream_enc[pool->stream_enc_count] == NULL) {
577 			DC_ERR("DC: failed to create stream_encoder!\n");
578 			return false;
579 		}
580 		pool->stream_enc_count++;
581 	}
582 
583 	dc->hwseq = create_funcs->create_hwseq(ctx);
584 
585 	return true;
586 }
587 static int find_matching_clock_source(
588 		const struct resource_pool *pool,
589 		struct clock_source *clock_source)
590 {
591 
592 	unsigned int i;
593 
594 	for (i = 0; i < pool->clk_src_count; i++) {
595 		if (pool->clock_sources[i] == clock_source)
596 			return (int)i;
597 	}
598 	return -1;
599 }
600 
601 void resource_unreference_clock_source(
602 		struct resource_context *res_ctx,
603 		const struct resource_pool *pool,
604 		struct clock_source *clock_source)
605 {
606 	int i = find_matching_clock_source(pool, clock_source);
607 
608 	if (i > -1)
609 		res_ctx->clock_source_ref_count[i]--;
610 
611 	if (pool->dp_clock_source == clock_source)
612 		res_ctx->dp_clock_source_ref_count--;
613 }
614 
615 void resource_reference_clock_source(
616 		struct resource_context *res_ctx,
617 		const struct resource_pool *pool,
618 		struct clock_source *clock_source)
619 {
620 	int i = find_matching_clock_source(pool, clock_source);
621 
622 	if (i > -1)
623 		res_ctx->clock_source_ref_count[i]++;
624 
625 	if (pool->dp_clock_source == clock_source)
626 		res_ctx->dp_clock_source_ref_count++;
627 }
628 
629 int resource_get_clock_source_reference(
630 		struct resource_context *res_ctx,
631 		const struct resource_pool *pool,
632 		struct clock_source *clock_source)
633 {
634 	int i = find_matching_clock_source(pool, clock_source);
635 
636 	if (i > -1)
637 		return res_ctx->clock_source_ref_count[i];
638 
639 	if (pool->dp_clock_source == clock_source)
640 		return res_ctx->dp_clock_source_ref_count;
641 
642 	return -1;
643 }
644 
645 bool resource_are_vblanks_synchronizable(
646 	struct dc_stream_state *stream1,
647 	struct dc_stream_state *stream2)
648 {
649 	uint32_t base60_refresh_rates[] = {10, 20, 5};
650 	uint8_t i;
651 	uint8_t rr_count = (uint8_t)ARRAY_SIZE(base60_refresh_rates);
652 	uint64_t frame_time_diff;
653 
654 	if (stream1->ctx->dc->config.vblank_alignment_dto_params &&
655 		stream1->ctx->dc->config.vblank_alignment_max_frame_time_diff > 0 &&
656 		dc_is_dp_signal(stream1->signal) &&
657 		dc_is_dp_signal(stream2->signal) &&
658 		false == stream1->has_non_synchronizable_pclk &&
659 		false == stream2->has_non_synchronizable_pclk &&
660 		stream1->timing.flags.VBLANK_SYNCHRONIZABLE &&
661 		stream2->timing.flags.VBLANK_SYNCHRONIZABLE) {
662 		/* disable refresh rates higher than 60Hz for now */
663 		if (stream1->timing.pix_clk_100hz*100/stream1->timing.h_total/
664 				stream1->timing.v_total > 60)
665 			return false;
666 		if (stream2->timing.pix_clk_100hz*100/stream2->timing.h_total/
667 				stream2->timing.v_total > 60)
668 			return false;
669 		frame_time_diff = (uint64_t)10000 *
670 			stream1->timing.h_total *
671 			stream1->timing.v_total *
672 			stream2->timing.pix_clk_100hz;
673 		frame_time_diff = div_u64(frame_time_diff, stream1->timing.pix_clk_100hz);
674 		frame_time_diff = div_u64(frame_time_diff, stream2->timing.h_total);
675 		frame_time_diff = div_u64(frame_time_diff, stream2->timing.v_total);
676 		for (i = 0; i < rr_count; i++) {
677 			int64_t diff = (int64_t)div_u64(frame_time_diff * base60_refresh_rates[i], 10) - 10000;
678 
679 			if (diff < 0)
680 				diff = -diff;
681 			if (diff < stream1->ctx->dc->config.vblank_alignment_max_frame_time_diff)
682 				return true;
683 		}
684 	}
685 	return false;
686 }
687 
688 bool resource_are_streams_timing_synchronizable(
689 	struct dc_stream_state *stream1,
690 	struct dc_stream_state *stream2)
691 {
692 	if (stream1->timing.h_total != stream2->timing.h_total)
693 		return false;
694 
695 	if (stream1->timing.v_total != stream2->timing.v_total)
696 		return false;
697 
698 	if (stream1->timing.h_addressable
699 				!= stream2->timing.h_addressable)
700 		return false;
701 
702 	if (stream1->timing.v_addressable
703 				!= stream2->timing.v_addressable)
704 		return false;
705 
706 	if (stream1->timing.v_front_porch
707 				!= stream2->timing.v_front_porch)
708 		return false;
709 
710 	if (stream1->timing.pix_clk_100hz
711 				!= stream2->timing.pix_clk_100hz)
712 		return false;
713 
714 	if (stream1->clamping.c_depth != stream2->clamping.c_depth)
715 		return false;
716 
717 	if (stream1->phy_pix_clk != stream2->phy_pix_clk
718 			&& (!dc_is_dp_signal(stream1->signal)
719 			|| !dc_is_dp_signal(stream2->signal)))
720 		return false;
721 
722 	if (stream1->view_format != stream2->view_format)
723 		return false;
724 
725 	if (stream1->ignore_msa_timing_param || stream2->ignore_msa_timing_param)
726 		return false;
727 
728 	return true;
729 }
730 static bool is_dp_and_hdmi_sharable(
731 		struct dc_stream_state *stream1,
732 		struct dc_stream_state *stream2)
733 {
734 	if (stream1->ctx->dc->caps.disable_dp_clk_share)
735 		return false;
736 
737 	if (stream1->clamping.c_depth != COLOR_DEPTH_888 ||
738 		stream2->clamping.c_depth != COLOR_DEPTH_888)
739 		return false;
740 
741 	return true;
742 
743 }
744 
745 static bool is_sharable_clk_src(
746 	const struct pipe_ctx *pipe_with_clk_src,
747 	const struct pipe_ctx *pipe)
748 {
749 	if (pipe_with_clk_src->clock_source == NULL)
750 		return false;
751 
752 	if (pipe_with_clk_src->stream->signal == SIGNAL_TYPE_VIRTUAL)
753 		return false;
754 
755 	if (dc_is_dp_signal(pipe_with_clk_src->stream->signal) ||
756 		(dc_is_dp_signal(pipe->stream->signal) &&
757 		!is_dp_and_hdmi_sharable(pipe_with_clk_src->stream,
758 				     pipe->stream)))
759 		return false;
760 
761 	if (dc_is_hdmi_signal(pipe_with_clk_src->stream->signal)
762 			&& dc_is_dual_link_signal(pipe->stream->signal))
763 		return false;
764 
765 	if (dc_is_hdmi_signal(pipe->stream->signal)
766 			&& dc_is_dual_link_signal(pipe_with_clk_src->stream->signal))
767 		return false;
768 
769 	if (!resource_are_streams_timing_synchronizable(
770 			pipe_with_clk_src->stream, pipe->stream))
771 		return false;
772 
773 	return true;
774 }
775 
776 struct clock_source *resource_find_used_clk_src_for_sharing(
777 					struct resource_context *res_ctx,
778 					struct pipe_ctx *pipe_ctx)
779 {
780 	int i;
781 
782 	for (i = 0; i < MAX_PIPES; i++) {
783 		if (is_sharable_clk_src(&res_ctx->pipe_ctx[i], pipe_ctx))
784 			return res_ctx->pipe_ctx[i].clock_source;
785 	}
786 
787 	return NULL;
788 }
789 
790 static enum dc_pixel_format convert_pixel_format_to_dalsurface(
791 		enum surface_pixel_format surface_pixel_format)
792 {
793 	enum dc_pixel_format dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
794 
795 	switch (surface_pixel_format) {
796 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
797 		dal_pixel_format = PIXEL_FORMAT_INDEX8;
798 		break;
799 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
800 		dal_pixel_format = PIXEL_FORMAT_RGB565;
801 		break;
802 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
803 		dal_pixel_format = PIXEL_FORMAT_RGB565;
804 		break;
805 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
806 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
807 		break;
808 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
809 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
810 		break;
811 	case SURFACE_PIXEL_FORMAT_GRPH_RGBA8888:
812 		dal_pixel_format = PIXEL_FORMAT_ARGB8888;
813 		break;
814 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
815 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
816 		break;
817 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
818 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010;
819 		break;
820 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
821 		dal_pixel_format = PIXEL_FORMAT_ARGB2101010_XRBIAS;
822 		break;
823 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
824 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
825 		dal_pixel_format = PIXEL_FORMAT_FP16;
826 		break;
827 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
828 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
829 		dal_pixel_format = PIXEL_FORMAT_420BPP8;
830 		break;
831 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
832 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
833 		dal_pixel_format = PIXEL_FORMAT_420BPP10;
834 		break;
835 	case SURFACE_PIXEL_FORMAT_VIDEO_422_CrCb_P208:
836 	case SURFACE_PIXEL_FORMAT_VIDEO_422_CbCr_P208:
837 		dal_pixel_format = PIXEL_FORMAT_422BPP8;
838 		break;
839 	case SURFACE_PIXEL_FORMAT_VIDEO_422_CrCb_P210:
840 	case SURFACE_PIXEL_FORMAT_VIDEO_422_CbCr_P210:
841 		dal_pixel_format = PIXEL_FORMAT_422BPP10;
842 		break;
843 	case SURFACE_PIXEL_FORMAT_VIDEO_422_CrCb_P212:
844 	case SURFACE_PIXEL_FORMAT_VIDEO_422_CbCr_P212:
845 		dal_pixel_format = PIXEL_FORMAT_422BPP12;
846 		break;
847 	case SURFACE_PIXEL_FORMAT_VIDEO_422_YCrYCb:
848 	case SURFACE_PIXEL_FORMAT_VIDEO_422_YCbYCr:
849 	case SURFACE_PIXEL_FORMAT_VIDEO_422_CrYCbY:
850 	case SURFACE_PIXEL_FORMAT_VIDEO_422_CbYCrY:
851 		dal_pixel_format = PIXEL_FORMAT_422BPP8;
852 		break;
853 	case SURFACE_PIXEL_FORMAT_VIDEO_422_10bpc_YCrYCb:
854 	case SURFACE_PIXEL_FORMAT_VIDEO_422_10bpc_YCbYCr:
855 	case SURFACE_PIXEL_FORMAT_VIDEO_422_10bpc_CrYCbY:
856 	case SURFACE_PIXEL_FORMAT_VIDEO_422_10bpc_CbYCrY:
857 		dal_pixel_format = PIXEL_FORMAT_422BPP10;
858 		break;
859 	case SURFACE_PIXEL_FORMAT_VIDEO_422_12bpc_YCrYCb:
860 	case SURFACE_PIXEL_FORMAT_VIDEO_422_12bpc_YCbYCr:
861 	case SURFACE_PIXEL_FORMAT_VIDEO_422_12bpc_CrYCbY:
862 	case SURFACE_PIXEL_FORMAT_VIDEO_422_12bpc_CbYCrY:
863 		dal_pixel_format = PIXEL_FORMAT_422BPP12;
864 		break;
865 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
866 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
867 	default:
868 		dal_pixel_format = PIXEL_FORMAT_UNKNOWN;
869 		break;
870 	}
871 	return dal_pixel_format;
872 }
873 
874 static inline void get_vp_scan_direction(
875 	enum dc_rotation_angle rotation,
876 	bool horizontal_mirror,
877 	bool *orthogonal_rotation,
878 	bool *flip_vert_scan_dir,
879 	bool *flip_horz_scan_dir)
880 {
881 	*orthogonal_rotation = false;
882 	*flip_vert_scan_dir = false;
883 	*flip_horz_scan_dir = false;
884 	if (rotation == ROTATION_ANGLE_180) {
885 		*flip_vert_scan_dir = true;
886 		*flip_horz_scan_dir = true;
887 	} else if (rotation == ROTATION_ANGLE_90) {
888 		*orthogonal_rotation = true;
889 		*flip_horz_scan_dir = true;
890 	} else if (rotation == ROTATION_ANGLE_270) {
891 		*orthogonal_rotation = true;
892 		*flip_vert_scan_dir = true;
893 	}
894 
895 	if (horizontal_mirror)
896 		*flip_horz_scan_dir = !*flip_horz_scan_dir;
897 }
898 
899 static struct rect intersect_rec(const struct rect *r0, const struct rect *r1)
900 {
901 	struct rect rec;
902 	int r0_x_end = r0->x + r0->width;
903 	int r1_x_end = r1->x + r1->width;
904 	int r0_y_end = r0->y + r0->height;
905 	int r1_y_end = r1->y + r1->height;
906 
907 	rec.x = r0->x > r1->x ? r0->x : r1->x;
908 	rec.width = r0_x_end > r1_x_end ? r1_x_end - rec.x : r0_x_end - rec.x;
909 	rec.y = r0->y > r1->y ? r0->y : r1->y;
910 	rec.height = r0_y_end > r1_y_end ? r1_y_end - rec.y : r0_y_end - rec.y;
911 
912 	/* in case that there is no intersection */
913 	if (rec.width < 0 || rec.height < 0)
914 		memset(&rec, 0, sizeof(rec));
915 
916 	return rec;
917 }
918 
919 static struct rect shift_rec(const struct rect *rec_in, int x, int y)
920 {
921 	struct rect rec_out = *rec_in;
922 
923 	rec_out.x += x;
924 	rec_out.y += y;
925 
926 	return rec_out;
927 }
928 
929 static struct rect calculate_plane_rec_in_timing_active(
930 		struct pipe_ctx *pipe_ctx,
931 		const struct rect *rec_in)
932 {
933 	/*
934 	 * The following diagram shows an example where we map a 1920x1200
935 	 * desktop to a 2560x1440 timing with a plane rect in the middle
936 	 * of the screen. To map a plane rect from Stream Source to Timing
937 	 * Active space, we first multiply stream scaling ratios (i.e 2304/1920
938 	 * horizontal and 1440/1200 vertical) to the plane's x and y, then
939 	 * we add stream destination offsets (i.e 128 horizontal, 0 vertical).
940 	 * This will give us a plane rect's position in Timing Active. However
941 	 * we have to remove the fractional. The rule is that we find left/right
942 	 * and top/bottom positions and round the value to the adjacent integer.
943 	 *
944 	 * Stream Source Space
945 	 * ------------
946 	 *        __________________________________________________
947 	 *       |Stream Source (1920 x 1200) ^                     |
948 	 *       |                            y                     |
949 	 *       |         <------- w --------|>                    |
950 	 *       |          __________________V                     |
951 	 *       |<-- x -->|Plane//////////////| ^                  |
952 	 *       |         |(pre scale)////////| |                  |
953 	 *       |         |///////////////////| |                  |
954 	 *       |         |///////////////////| h                  |
955 	 *       |         |///////////////////| |                  |
956 	 *       |         |///////////////////| |                  |
957 	 *       |         |///////////////////| V                  |
958 	 *       |                                                  |
959 	 *       |                                                  |
960 	 *       |__________________________________________________|
961 	 *
962 	 *
963 	 * Timing Active Space
964 	 * ---------------------------------
965 	 *
966 	 *       Timing Active (2560 x 1440)
967 	 *        __________________________________________________
968 	 *       |*****|  Stteam Destination (2304 x 1440)    |*****|
969 	 *       |*****|                                      |*****|
970 	 *       |<128>|                                      |*****|
971 	 *       |*****|     __________________               |*****|
972 	 *       |*****|    |Plane/////////////|              |*****|
973 	 *       |*****|    |(post scale)//////|              |*****|
974 	 *       |*****|    |//////////////////|              |*****|
975 	 *       |*****|    |//////////////////|              |*****|
976 	 *       |*****|    |//////////////////|              |*****|
977 	 *       |*****|    |//////////////////|              |*****|
978 	 *       |*****|                                      |*****|
979 	 *       |*****|                                      |*****|
980 	 *       |*****|                                      |*****|
981 	 *       |*****|______________________________________|*****|
982 	 *
983 	 * So the resulting formulas are shown below:
984 	 *
985 	 * recout_x = 128 + round(plane_x * 2304 / 1920)
986 	 * recout_w = 128 + round((plane_x + plane_w) * 2304 / 1920) - recout_x
987 	 * recout_y = 0 + round(plane_y * 1440 / 1280)
988 	 * recout_h = 0 + round((plane_y + plane_h) * 1440 / 1200) - recout_y
989 	 *
990 	 * NOTE: fixed point division is not error free. To reduce errors
991 	 * introduced by fixed point division, we divide only after
992 	 * multiplication is complete.
993 	 */
994 	const struct dc_stream_state *stream = pipe_ctx->stream;
995 	struct rect rec_out = {0};
996 	struct fixed31_32 temp;
997 
998 	temp = dc_fixpt_from_fraction(rec_in->x * (long long)stream->dst.width,
999 			stream->src.width);
1000 	rec_out.x = stream->dst.x + dc_fixpt_round(temp);
1001 
1002 	temp = dc_fixpt_from_fraction(
1003 			(rec_in->x + rec_in->width) * (long long)stream->dst.width,
1004 			stream->src.width);
1005 	rec_out.width = stream->dst.x + dc_fixpt_round(temp) - rec_out.x;
1006 
1007 	temp = dc_fixpt_from_fraction(rec_in->y * (long long)stream->dst.height,
1008 			stream->src.height);
1009 	rec_out.y = stream->dst.y + dc_fixpt_round(temp);
1010 
1011 	temp = dc_fixpt_from_fraction(
1012 			(rec_in->y + rec_in->height) * (long long)stream->dst.height,
1013 			stream->src.height);
1014 	rec_out.height = stream->dst.y + dc_fixpt_round(temp) - rec_out.y;
1015 
1016 	return rec_out;
1017 }
1018 
1019 static struct rect calculate_mpc_slice_in_timing_active(
1020 		struct pipe_ctx *pipe_ctx,
1021 		struct rect *plane_clip_rec)
1022 {
1023 	const struct dc_stream_state *stream = pipe_ctx->stream;
1024 	int mpc_slice_count = resource_get_mpc_slice_count(pipe_ctx);
1025 	int mpc_slice_idx = resource_get_mpc_slice_index(pipe_ctx);
1026 	int epimo = mpc_slice_count - plane_clip_rec->width % mpc_slice_count - 1;
1027 	struct rect mpc_rec;
1028 
1029 	mpc_rec.width = plane_clip_rec->width / mpc_slice_count;
1030 	mpc_rec.x = plane_clip_rec->x + mpc_rec.width * mpc_slice_idx;
1031 	mpc_rec.height = plane_clip_rec->height;
1032 	mpc_rec.y = plane_clip_rec->y;
1033 	ASSERT(mpc_slice_count == 1 ||
1034 			stream->view_format != VIEW_3D_FORMAT_SIDE_BY_SIDE ||
1035 			mpc_rec.width % 2 == 0);
1036 
1037 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
1038 		mpc_rec.x -= (mpc_rec.width * mpc_slice_idx);
1039 
1040 	/* extra pixels in the division remainder need to go to pipes after
1041 	 * the extra pixel index minus one(epimo) defined here as:
1042 	 */
1043 	if (mpc_slice_idx > epimo) {
1044 		mpc_rec.x += mpc_slice_idx - epimo - 1;
1045 		mpc_rec.width += 1;
1046 	}
1047 
1048 	if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM) {
1049 		ASSERT(mpc_rec.height % 2 == 0);
1050 		mpc_rec.height /= 2;
1051 	}
1052 	return mpc_rec;
1053 }
1054 
1055 static void calculate_adjust_recout_for_visual_confirm(const struct pipe_ctx *pipe_ctx,
1056 	unsigned int *base_offset, unsigned int *dpp_offset)
1057 {
1058 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1059 	*base_offset = 0;
1060 	*dpp_offset = 0;
1061 
1062 	if (dc->debug.visual_confirm == VISUAL_CONFIRM_DISABLE || !pipe_ctx->plane_res.dpp)
1063 		return;
1064 
1065 	*dpp_offset = pipe_ctx->stream->timing.v_addressable / VISUAL_CONFIRM_DPP_OFFSET_DENO;
1066 	*dpp_offset *= pipe_ctx->plane_res.dpp->inst;
1067 
1068 	if ((dc->debug.visual_confirm_rect_height >= VISUAL_CONFIRM_BASE_MIN) &&
1069 			dc->debug.visual_confirm_rect_height <= VISUAL_CONFIRM_BASE_MAX)
1070 		*base_offset = dc->debug.visual_confirm_rect_height;
1071 	else
1072 		*base_offset = VISUAL_CONFIRM_BASE_DEFAULT;
1073 }
1074 
1075 static void reverse_adjust_recout_for_visual_confirm(struct rect *recout,
1076 		const struct pipe_ctx *pipe_ctx)
1077 {
1078 	unsigned int dpp_offset, base_offset;
1079 
1080 	calculate_adjust_recout_for_visual_confirm(pipe_ctx, &base_offset,
1081 		&dpp_offset);
1082 	recout->height += base_offset;
1083 	recout->height += dpp_offset;
1084 }
1085 
1086 static void adjust_recout_for_visual_confirm(struct rect *recout,
1087 		struct pipe_ctx *pipe_ctx)
1088 {
1089 	unsigned int dpp_offset, base_offset;
1090 
1091 	calculate_adjust_recout_for_visual_confirm(pipe_ctx, &base_offset,
1092 		&dpp_offset);
1093 	recout->height -= base_offset;
1094 	recout->height -= dpp_offset;
1095 }
1096 
1097 /*
1098  * The function maps a plane clip from Stream Source Space to ODM Slice Space
1099  * and calculates the rec of the overlapping area of MPC slice of the plane
1100  * clip, ODM slice associated with the pipe context and stream destination rec.
1101  */
1102 static void calculate_recout(struct pipe_ctx *pipe_ctx)
1103 {
1104 	/*
1105 	 * A plane clip represents the desired plane size and position in Stream
1106 	 * Source Space. Stream Source is the destination where all planes are
1107 	 * blended (i.e. positioned, scaled and overlaid). It is a canvas where
1108 	 * all planes associated with the current stream are drawn together.
1109 	 * After Stream Source is completed, we will further scale and
1110 	 * reposition the entire canvas of the stream source to Stream
1111 	 * Destination in Timing Active Space. This could be due to display
1112 	 * overscan adjustment where we will need to rescale and reposition all
1113 	 * the planes so they can fit into a TV with overscan or downscale
1114 	 * upscale features such as GPU scaling or VSR.
1115 	 *
1116 	 * This two step blending is a virtual procedure in software. In
1117 	 * hardware there is no such thing as Stream Source. all planes are
1118 	 * blended once in Timing Active Space. Software virtualizes a Stream
1119 	 * Source space to decouple the math complicity so scaling param
1120 	 * calculation focuses on one step at a time.
1121 	 *
1122 	 * In the following two diagrams, user applied 10% overscan adjustment
1123 	 * so the Stream Source needs to be scaled down a little before mapping
1124 	 * to Timing Active Space. As a result the Plane Clip is also scaled
1125 	 * down by the same ratio, Plane Clip position (i.e. x and y) with
1126 	 * respect to Stream Source is also scaled down. To map it in Timing
1127 	 * Active Space additional x and y offsets from Stream Destination are
1128 	 * added to Plane Clip as well.
1129 	 *
1130 	 * Stream Source Space
1131 	 * ------------
1132 	 *        __________________________________________________
1133 	 *       |Stream Source (3840 x 2160) ^                     |
1134 	 *       |                            y                     |
1135 	 *       |                            |                     |
1136 	 *       |          __________________V                     |
1137 	 *       |<-- x -->|Plane Clip/////////|                    |
1138 	 *       |         |(pre scale)////////|                    |
1139 	 *       |         |///////////////////|                    |
1140 	 *       |         |///////////////////|                    |
1141 	 *       |         |///////////////////|                    |
1142 	 *       |         |///////////////////|                    |
1143 	 *       |         |///////////////////|                    |
1144 	 *       |                                                  |
1145 	 *       |                                                  |
1146 	 *       |__________________________________________________|
1147 	 *
1148 	 *
1149 	 * Timing Active Space (3840 x 2160)
1150 	 * ---------------------------------
1151 	 *
1152 	 *       Timing Active
1153 	 *        __________________________________________________
1154 	 *       | y_____________________________________________   |
1155 	 *       |x |Stream Destination (3456 x 1944)            |  |
1156 	 *       |  |                                            |  |
1157 	 *       |  |        __________________                  |  |
1158 	 *       |  |       |Plane Clip////////|                 |  |
1159 	 *       |  |       |(post scale)//////|                 |  |
1160 	 *       |  |       |//////////////////|                 |  |
1161 	 *       |  |       |//////////////////|                 |  |
1162 	 *       |  |       |//////////////////|                 |  |
1163 	 *       |  |       |//////////////////|                 |  |
1164 	 *       |  |                                            |  |
1165 	 *       |  |                                            |  |
1166 	 *       |  |____________________________________________|  |
1167 	 *       |__________________________________________________|
1168 	 *
1169 	 *
1170 	 * In Timing Active Space a plane clip could be further sliced into
1171 	 * pieces called MPC slices. Each Pipe Context is responsible for
1172 	 * processing only one MPC slice so the plane processing workload can be
1173 	 * distributed to multiple DPP Pipes. MPC slices could be blended
1174 	 * together to a single ODM slice. Each ODM slice is responsible for
1175 	 * processing a portion of Timing Active divided horizontally so the
1176 	 * output pixel processing workload can be distributed to multiple OPP
1177 	 * pipes. All ODM slices are mapped together in ODM block so all MPC
1178 	 * slices belong to different ODM slices could be pieced together to
1179 	 * form a single image in Timing Active. MPC slices must belong to
1180 	 * single ODM slice. If an MPC slice goes across ODM slice boundary, it
1181 	 * needs to be divided into two MPC slices one for each ODM slice.
1182 	 *
1183 	 * In the following diagram the output pixel processing workload is
1184 	 * divided horizontally into two ODM slices one for each OPP blend tree.
1185 	 * OPP0 blend tree is responsible for processing left half of Timing
1186 	 * Active, while OPP2 blend tree is responsible for processing right
1187 	 * half.
1188 	 *
1189 	 * The plane has two MPC slices. However since the right MPC slice goes
1190 	 * across ODM boundary, two DPP pipes are needed one for each OPP blend
1191 	 * tree. (i.e. DPP1 for OPP0 blend tree and DPP2 for OPP2 blend tree).
1192 	 *
1193 	 * Assuming that we have a Pipe Context associated with OPP0 and DPP1
1194 	 * working on processing the plane in the diagram. We want to know the
1195 	 * width and height of the shaded rectangle and its relative position
1196 	 * with respect to the ODM slice0. This is called the recout of the pipe
1197 	 * context.
1198 	 *
1199 	 * Planes can be at arbitrary size and position and there could be an
1200 	 * arbitrary number of MPC and ODM slices. The algorithm needs to take
1201 	 * all scenarios into account.
1202 	 *
1203 	 * Timing Active Space (3840 x 2160)
1204 	 * ---------------------------------
1205 	 *
1206 	 *       Timing Active
1207 	 *        __________________________________________________
1208 	 *       |OPP0(ODM slice0)^        |OPP2(ODM slice1)        |
1209 	 *       |                y        |                        |
1210 	 *       |                |  <- w ->                        |
1211 	 *       |           _____V________|____                    |
1212 	 *       |          |DPP0 ^  |DPP1 |DPP2|                   |
1213 	 *       |<------ x |-----|->|/////|    |                   |
1214 	 *       |          |     |  |/////|    |                   |
1215 	 *       |          |     h  |/////|    |                   |
1216 	 *       |          |     |  |/////|    |                   |
1217 	 *       |          |_____V__|/////|____|                   |
1218 	 *       |                         |                        |
1219 	 *       |                         |                        |
1220 	 *       |                         |                        |
1221 	 *       |_________________________|________________________|
1222 	 *
1223 	 *
1224 	 */
1225 	struct rect plane_clip;
1226 	struct rect mpc_slice_of_plane_clip;
1227 	struct rect odm_slice_src;
1228 	struct rect overlapping_area;
1229 
1230 	plane_clip = calculate_plane_rec_in_timing_active(pipe_ctx,
1231 			&pipe_ctx->plane_state->clip_rect);
1232 	/* guard plane clip from drawing beyond stream dst here */
1233 	plane_clip = intersect_rec(&plane_clip,
1234 				&pipe_ctx->stream->dst);
1235 	mpc_slice_of_plane_clip = calculate_mpc_slice_in_timing_active(
1236 			pipe_ctx, &plane_clip);
1237 	odm_slice_src = resource_get_odm_slice_src_rect(pipe_ctx);
1238 	overlapping_area = intersect_rec(&mpc_slice_of_plane_clip, &odm_slice_src);
1239 	if (overlapping_area.height > 0 &&
1240 			overlapping_area.width > 0) {
1241 		/* shift the overlapping area so it is with respect to current
1242 		 * ODM slice source's position
1243 		 */
1244 		pipe_ctx->plane_res.scl_data.recout = shift_rec(
1245 				&overlapping_area,
1246 				-odm_slice_src.x, -odm_slice_src.y);
1247 		adjust_recout_for_visual_confirm(
1248 				&pipe_ctx->plane_res.scl_data.recout,
1249 				pipe_ctx);
1250 	} else {
1251 		/* if there is no overlap, zero recout */
1252 		memset(&pipe_ctx->plane_res.scl_data.recout, 0,
1253 				sizeof(struct rect));
1254 	}
1255 
1256 }
1257 
1258 static void calculate_scaling_ratios(struct pipe_ctx *pipe_ctx)
1259 {
1260 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1261 	const struct dc_stream_state *stream = pipe_ctx->stream;
1262 	struct rect surf_src = plane_state->src_rect;
1263 	const int in_w = stream->src.width;
1264 	const int in_h = stream->src.height;
1265 	const int out_w = stream->dst.width;
1266 	const int out_h = stream->dst.height;
1267 
1268 	/*Swap surf_src height and width since scaling ratios are in recout rotation*/
1269 	if (pipe_ctx->plane_state->rotation == ROTATION_ANGLE_90 ||
1270 			pipe_ctx->plane_state->rotation == ROTATION_ANGLE_270)
1271 		swap(surf_src.height, surf_src.width);
1272 
1273 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_from_fraction(
1274 					surf_src.width,
1275 					plane_state->dst_rect.width);
1276 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_from_fraction(
1277 					surf_src.height,
1278 					plane_state->dst_rect.height);
1279 
1280 	if (stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
1281 		pipe_ctx->plane_res.scl_data.ratios.horz.value *= 2;
1282 	else if (stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
1283 		pipe_ctx->plane_res.scl_data.ratios.vert.value *= 2;
1284 
1285 	pipe_ctx->plane_res.scl_data.ratios.vert.value = div64_s64(
1286 		pipe_ctx->plane_res.scl_data.ratios.vert.value * in_h, out_h);
1287 	pipe_ctx->plane_res.scl_data.ratios.horz.value = div64_s64(
1288 		pipe_ctx->plane_res.scl_data.ratios.horz.value * in_w, out_w);
1289 
1290 	pipe_ctx->plane_res.scl_data.ratios.horz_c = pipe_ctx->plane_res.scl_data.ratios.horz;
1291 	pipe_ctx->plane_res.scl_data.ratios.vert_c = pipe_ctx->plane_res.scl_data.ratios.vert;
1292 
1293 	if (pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP8
1294 			|| pipe_ctx->plane_res.scl_data.format == PIXEL_FORMAT_420BPP10) {
1295 		pipe_ctx->plane_res.scl_data.ratios.horz_c.value /= 2;
1296 		pipe_ctx->plane_res.scl_data.ratios.vert_c.value /= 2;
1297 	}
1298 	pipe_ctx->plane_res.scl_data.ratios.horz = dc_fixpt_truncate(
1299 			pipe_ctx->plane_res.scl_data.ratios.horz, 19);
1300 	pipe_ctx->plane_res.scl_data.ratios.vert = dc_fixpt_truncate(
1301 			pipe_ctx->plane_res.scl_data.ratios.vert, 19);
1302 	pipe_ctx->plane_res.scl_data.ratios.horz_c = dc_fixpt_truncate(
1303 			pipe_ctx->plane_res.scl_data.ratios.horz_c, 19);
1304 	pipe_ctx->plane_res.scl_data.ratios.vert_c = dc_fixpt_truncate(
1305 			pipe_ctx->plane_res.scl_data.ratios.vert_c, 19);
1306 }
1307 
1308 
1309 /*
1310  * We completely calculate vp offset, size and inits here based entirely on scaling
1311  * ratios and recout for pixel perfect pipe combine.
1312  */
1313 static void calculate_init_and_vp(
1314 		bool flip_scan_dir,
1315 		int recout_offset_within_recout_full,
1316 		int recout_size,
1317 		int src_size,
1318 		int taps,
1319 		struct fixed31_32 ratio,
1320 		struct fixed31_32 *init,
1321 		int *vp_offset,
1322 		int *vp_size)
1323 {
1324 	struct fixed31_32 temp;
1325 	int int_part;
1326 
1327 	/*
1328 	 * First of the taps starts sampling pixel number <init_int_part> corresponding to recout
1329 	 * pixel 1. Next recout pixel samples int part of <init + scaling ratio> and so on.
1330 	 * All following calculations are based on this logic.
1331 	 *
1332 	 * Init calculated according to formula:
1333 	 * 	init = (scaling_ratio + number_of_taps + 1) / 2
1334 	 * 	init_bot = init + scaling_ratio
1335 	 * 	to get pixel perfect combine add the fraction from calculating vp offset
1336 	 */
1337 	temp = dc_fixpt_mul_int(ratio, recout_offset_within_recout_full);
1338 	*vp_offset = dc_fixpt_floor(temp);
1339 	temp.value &= 0xffffffff;
1340 	*init = dc_fixpt_truncate(dc_fixpt_add(dc_fixpt_div_int(
1341 			dc_fixpt_add_int(ratio, taps + 1), 2), temp), 19);
1342 	/*
1343 	 * If viewport has non 0 offset and there are more taps than covered by init then
1344 	 * we should decrease the offset and increase init so we are never sampling
1345 	 * outside of viewport.
1346 	 */
1347 	int_part = dc_fixpt_floor(*init);
1348 	if (int_part < taps) {
1349 		int_part = taps - int_part;
1350 		if (int_part > *vp_offset)
1351 			int_part = *vp_offset;
1352 		*vp_offset -= int_part;
1353 		*init = dc_fixpt_add_int(*init, int_part);
1354 	}
1355 	/*
1356 	 * If taps are sampling outside of viewport at end of recout and there are more pixels
1357 	 * available in the surface we should increase the viewport size, regardless set vp to
1358 	 * only what is used.
1359 	 */
1360 	temp = dc_fixpt_add(*init, dc_fixpt_mul_int(ratio, recout_size - 1));
1361 	*vp_size = dc_fixpt_floor(temp);
1362 	if (*vp_size + *vp_offset > src_size)
1363 		*vp_size = src_size - *vp_offset;
1364 
1365 	/* We did all the math assuming we are scanning same direction as display does,
1366 	 * however mirror/rotation changes how vp scans vs how it is offset. If scan direction
1367 	 * is flipped we simply need to calculate offset from the other side of plane.
1368 	 * Note that outside of viewport all scaling hardware works in recout space.
1369 	 */
1370 	if (flip_scan_dir)
1371 		*vp_offset = src_size - *vp_offset - *vp_size;
1372 }
1373 
1374 static void calculate_inits_and_viewports(struct pipe_ctx *pipe_ctx)
1375 {
1376 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1377 	struct scaler_data *data = &pipe_ctx->plane_res.scl_data;
1378 	struct rect src = plane_state->src_rect;
1379 	struct rect recout_dst_in_active_timing;
1380 	struct rect recout_clip_in_active_timing;
1381 	struct rect recout_clip_in_recout_dst;
1382 	struct rect overlap_in_active_timing;
1383 	struct rect odm_slice_src = resource_get_odm_slice_src_rect(pipe_ctx);
1384 	int vpc_div = (data->format == PIXEL_FORMAT_420BPP8
1385 				|| data->format == PIXEL_FORMAT_420BPP10) ? 2 : 1;
1386 	bool orthogonal_rotation, flip_vert_scan_dir, flip_horz_scan_dir;
1387 
1388 	recout_clip_in_active_timing = shift_rec(
1389 			&data->recout, odm_slice_src.x, odm_slice_src.y);
1390 	recout_dst_in_active_timing = calculate_plane_rec_in_timing_active(
1391 			pipe_ctx, &plane_state->dst_rect);
1392 	overlap_in_active_timing = intersect_rec(&recout_clip_in_active_timing,
1393 			&recout_dst_in_active_timing);
1394 	if (overlap_in_active_timing.width > 0 &&
1395 			overlap_in_active_timing.height > 0)
1396 		recout_clip_in_recout_dst = shift_rec(&overlap_in_active_timing,
1397 				-recout_dst_in_active_timing.x,
1398 				-recout_dst_in_active_timing.y);
1399 	else
1400 		memset(&recout_clip_in_recout_dst, 0, sizeof(struct rect));
1401 
1402 	/*
1403 	 * Work in recout rotation since that requires less transformations
1404 	 */
1405 	get_vp_scan_direction(
1406 			plane_state->rotation,
1407 			plane_state->horizontal_mirror,
1408 			&orthogonal_rotation,
1409 			&flip_vert_scan_dir,
1410 			&flip_horz_scan_dir);
1411 
1412 	if (orthogonal_rotation) {
1413 		swap(src.width, src.height);
1414 		swap(flip_vert_scan_dir, flip_horz_scan_dir);
1415 	}
1416 
1417 	calculate_init_and_vp(
1418 			flip_horz_scan_dir,
1419 			recout_clip_in_recout_dst.x,
1420 			data->recout.width,
1421 			src.width,
1422 			data->taps.h_taps,
1423 			data->ratios.horz,
1424 			&data->inits.h,
1425 			&data->viewport.x,
1426 			&data->viewport.width);
1427 	calculate_init_and_vp(
1428 			flip_horz_scan_dir,
1429 			recout_clip_in_recout_dst.x,
1430 			data->recout.width,
1431 			src.width / vpc_div,
1432 			data->taps.h_taps_c,
1433 			data->ratios.horz_c,
1434 			&data->inits.h_c,
1435 			&data->viewport_c.x,
1436 			&data->viewport_c.width);
1437 	calculate_init_and_vp(
1438 			flip_vert_scan_dir,
1439 			recout_clip_in_recout_dst.y,
1440 			data->recout.height,
1441 			src.height,
1442 			data->taps.v_taps,
1443 			data->ratios.vert,
1444 			&data->inits.v,
1445 			&data->viewport.y,
1446 			&data->viewport.height);
1447 	calculate_init_and_vp(
1448 			flip_vert_scan_dir,
1449 			recout_clip_in_recout_dst.y,
1450 			data->recout.height,
1451 			src.height / vpc_div,
1452 			data->taps.v_taps_c,
1453 			data->ratios.vert_c,
1454 			&data->inits.v_c,
1455 			&data->viewport_c.y,
1456 			&data->viewport_c.height);
1457 	if (orthogonal_rotation) {
1458 		swap(data->viewport.x, data->viewport.y);
1459 		swap(data->viewport.width, data->viewport.height);
1460 		swap(data->viewport_c.x, data->viewport_c.y);
1461 		swap(data->viewport_c.width, data->viewport_c.height);
1462 	}
1463 	data->viewport.x += src.x;
1464 	data->viewport.y += src.y;
1465 	ASSERT(src.x % vpc_div == 0 && src.y % vpc_div == 0);
1466 	data->viewport_c.x += src.x / vpc_div;
1467 	data->viewport_c.y += src.y / vpc_div;
1468 }
1469 
1470 static enum controller_dp_test_pattern convert_dp_to_controller_test_pattern(
1471 				enum dp_test_pattern test_pattern)
1472 {
1473 	enum controller_dp_test_pattern controller_test_pattern;
1474 
1475 	switch (test_pattern) {
1476 	case DP_TEST_PATTERN_COLOR_SQUARES:
1477 		controller_test_pattern =
1478 				CONTROLLER_DP_TEST_PATTERN_COLORSQUARES;
1479 	break;
1480 	case DP_TEST_PATTERN_COLOR_SQUARES_CEA:
1481 		controller_test_pattern =
1482 				CONTROLLER_DP_TEST_PATTERN_COLORSQUARES_CEA;
1483 	break;
1484 	case DP_TEST_PATTERN_VERTICAL_BARS:
1485 		controller_test_pattern =
1486 				CONTROLLER_DP_TEST_PATTERN_VERTICALBARS;
1487 	break;
1488 	case DP_TEST_PATTERN_HORIZONTAL_BARS:
1489 		controller_test_pattern =
1490 				CONTROLLER_DP_TEST_PATTERN_HORIZONTALBARS;
1491 	break;
1492 	case DP_TEST_PATTERN_COLOR_RAMP:
1493 		controller_test_pattern =
1494 				CONTROLLER_DP_TEST_PATTERN_COLORRAMP;
1495 	break;
1496 	default:
1497 		controller_test_pattern =
1498 				CONTROLLER_DP_TEST_PATTERN_VIDEOMODE;
1499 	break;
1500 	}
1501 
1502 	return controller_test_pattern;
1503 }
1504 
1505 static enum controller_dp_color_space convert_dp_to_controller_color_space(
1506 		enum dp_test_pattern_color_space color_space)
1507 {
1508 	enum controller_dp_color_space controller_color_space;
1509 
1510 	switch (color_space) {
1511 	case DP_TEST_PATTERN_COLOR_SPACE_RGB:
1512 		controller_color_space = CONTROLLER_DP_COLOR_SPACE_RGB;
1513 		break;
1514 	case DP_TEST_PATTERN_COLOR_SPACE_YCBCR601:
1515 		controller_color_space = CONTROLLER_DP_COLOR_SPACE_YCBCR601;
1516 		break;
1517 	case DP_TEST_PATTERN_COLOR_SPACE_YCBCR709:
1518 		controller_color_space = CONTROLLER_DP_COLOR_SPACE_YCBCR709;
1519 		break;
1520 	case DP_TEST_PATTERN_COLOR_SPACE_UNDEFINED:
1521 	default:
1522 		controller_color_space = CONTROLLER_DP_COLOR_SPACE_UDEFINED;
1523 		break;
1524 	}
1525 
1526 	return controller_color_space;
1527 }
1528 
1529 void resource_build_test_pattern_params(struct resource_context *res_ctx,
1530 				struct pipe_ctx *otg_master)
1531 {
1532 	struct pipe_ctx *opp_heads[MAX_PIPES];
1533 	struct test_pattern_params *params;
1534 	int odm_cnt;
1535 	enum controller_dp_test_pattern controller_test_pattern;
1536 	enum controller_dp_color_space controller_color_space;
1537 	enum dc_color_depth color_depth = otg_master->stream->timing.display_color_depth;
1538 	struct rect odm_slice_src;
1539 	int i;
1540 
1541 	controller_test_pattern = convert_dp_to_controller_test_pattern(
1542 			otg_master->stream->test_pattern.type);
1543 	controller_color_space = convert_dp_to_controller_color_space(
1544 			otg_master->stream->test_pattern.color_space);
1545 
1546 	if (controller_test_pattern == CONTROLLER_DP_TEST_PATTERN_VIDEOMODE)
1547 		return;
1548 
1549 	odm_cnt = resource_get_opp_heads_for_otg_master(otg_master, res_ctx, opp_heads);
1550 
1551 	for (i = 0; i < odm_cnt; i++) {
1552 		odm_slice_src = resource_get_odm_slice_src_rect(opp_heads[i]);
1553 		params = &opp_heads[i]->stream_res.test_pattern_params;
1554 		params->test_pattern = controller_test_pattern;
1555 		params->color_space = controller_color_space;
1556 		params->color_depth = color_depth;
1557 		params->height = odm_slice_src.height;
1558 		params->offset = odm_slice_src.x;
1559 		params->width = odm_slice_src.width;
1560 	}
1561 }
1562 
1563 enum upsp_mode resource_is_upsp_required(enum surface_pixel_format format,
1564 		enum dc_scaling_linearity scaling_linearity)
1565 {
1566 	bool scaling_in_linear = (scaling_linearity == DC_SCALING_LINEARITY_LINEAR);
1567 	bool is_420_format = (format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN &&
1568 			format <= SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb);
1569 	bool is_422_format = (format > SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb &&
1570 			format < SURFACE_PIXEL_FORMAT_SUBSAMPLE_END);
1571 
1572 	/* UPSP (chroma upsampling) is only needed when subsampled YUV is scaled in
1573 	 * linear space.
1574 	 */
1575 	if (scaling_in_linear && is_420_format)
1576 		return UPSP_HORIZONTAL_VERTICAL_UPSAMPLING;
1577 	if (scaling_in_linear && is_422_format)
1578 		return UPSP_HORIZONTAL_UPSAMPLING_ONLY;
1579 	return UPSP_BYPASS;
1580 }
1581 
1582 bool resource_build_scaling_params(struct pipe_ctx *pipe_ctx)
1583 {
1584 	const struct dc_plane_state *plane_state = pipe_ctx->plane_state;
1585 	struct dc_crtc_timing *timing = &pipe_ctx->stream->timing;
1586 	const struct rect odm_slice_src = resource_get_odm_slice_src_rect(pipe_ctx);
1587 	struct scaling_taps temp = {0};
1588 	bool res = false;
1589 
1590 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1591 
1592 	/* Invalid input */
1593 	if (!plane_state ||
1594 			!plane_state->dst_rect.width ||
1595 			!plane_state->dst_rect.height ||
1596 			!plane_state->src_rect.width ||
1597 			!plane_state->src_rect.height) {
1598 		ASSERT(0);
1599 		return false;
1600 	}
1601 
1602 	/* Timing borders are part of vactive that we are also supposed to skip in addition
1603 	 * to any stream dst offset. Since dm logic assumes dst is in addressable
1604 	 * space we need to add the left and top borders to dst offsets temporarily.
1605 	 * TODO: fix in DM, stream dst is supposed to be in vactive
1606 	 */
1607 	pipe_ctx->stream->dst.x += timing->h_border_left;
1608 	pipe_ctx->stream->dst.y += timing->v_border_top;
1609 
1610 	/* Calculate H and V active size */
1611 	pipe_ctx->plane_res.scl_data.h_active = odm_slice_src.width;
1612 	pipe_ctx->plane_res.scl_data.v_active = odm_slice_src.height;
1613 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
1614 			pipe_ctx->plane_state->format);
1615 
1616 #if defined(CONFIG_DRM_AMD_DC_FP)
1617 	if ((pipe_ctx->stream->ctx->dc->config.use_spl)	&& (!pipe_ctx->stream->ctx->dc->debug.disable_spl)) {
1618 		struct spl_in *spl_in = &pipe_ctx->plane_res.spl_in;
1619 		struct spl_out *spl_out = &pipe_ctx->plane_res.spl_out;
1620 
1621 		if (plane_state->ctx->dce_version > DCE_VERSION_MAX)
1622 			pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_36BPP;
1623 		else
1624 			pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
1625 
1626 		pipe_ctx->plane_res.scl_data.lb_params.alpha_en = plane_state->per_pixel_alpha;
1627 		pipe_ctx->plane_res.scl_data.upsp = resource_is_upsp_required(plane_state->format, plane_state->scaling_linearity);
1628 
1629 		// Convert pipe_ctx to respective input params for SPL
1630 		translate_SPL_in_params_from_pipe_ctx(pipe_ctx, spl_in);
1631 		/* Pass visual confirm debug information */
1632 		calculate_adjust_recout_for_visual_confirm(pipe_ctx,
1633 			&spl_in->debug.visual_confirm_base_offset,
1634 			&spl_in->debug.visual_confirm_dpp_offset);
1635 		// Set SPL output parameters to dscl_prog_data to be used for hw registers
1636 		spl_out->dscl_prog_data = resource_get_dscl_prog_data(pipe_ctx);
1637 		// Calculate scaler parameters from SPL
1638 		res = spl_calculate_scaler_params(spl_in, spl_out);
1639 		// Convert respective out params from SPL to scaler data
1640 		translate_SPL_out_params_to_pipe_ctx(pipe_ctx, spl_out);
1641 
1642 		/* Ignore scaler failure if pipe context plane is phantom plane */
1643 		if (!res && plane_state->is_phantom)
1644 			res = true;
1645 	} else {
1646 #endif
1647 	/* depends on h_active */
1648 	calculate_recout(pipe_ctx);
1649 	/* depends on pixel format */
1650 	calculate_scaling_ratios(pipe_ctx);
1651 
1652 	/*
1653 	 * LB calculations depend on vp size, h/v_active and scaling ratios
1654 	 * Setting line buffer pixel depth to 24bpp yields banding
1655 	 * on certain displays, such as the Sharp 4k. 36bpp is needed
1656 	 * to support SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616 and
1657 	 * SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616 with actual > 10 bpc
1658 	 * precision on DCN display engines, but apparently not for DCE, as
1659 	 * far as testing on DCE-11.2 and DCE-8 showed. Various DCE parts have
1660 	 * problems: Carrizo with DCE_VERSION_11_0 does not like 36 bpp lb depth,
1661 	 * neither do DCE-8 at 4k resolution, or DCE-11.2 (broken identify pixel
1662 	 * passthrough). Therefore only use 36 bpp on DCN where it is actually needed.
1663 	 */
1664 	if (plane_state->ctx->dce_version > DCE_VERSION_MAX)
1665 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_36BPP;
1666 	else
1667 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
1668 
1669 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = plane_state->per_pixel_alpha;
1670 
1671 	// get TAP value with 100x100 dummy data for max scaling qualify, override
1672 	// if a new scaling quality required
1673 	pipe_ctx->plane_res.scl_data.viewport.width = 100;
1674 	pipe_ctx->plane_res.scl_data.viewport.height = 100;
1675 	pipe_ctx->plane_res.scl_data.viewport_c.width = 100;
1676 	pipe_ctx->plane_res.scl_data.viewport_c.height = 100;
1677 	if (pipe_ctx->plane_res.xfm != NULL)
1678 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1679 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1680 
1681 	if (pipe_ctx->plane_res.dpp != NULL)
1682 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1683 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1684 
1685 	temp = pipe_ctx->plane_res.scl_data.taps;
1686 
1687 	calculate_inits_and_viewports(pipe_ctx);
1688 
1689 	if (pipe_ctx->plane_res.xfm != NULL)
1690 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1691 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1692 
1693 	if (pipe_ctx->plane_res.dpp != NULL)
1694 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1695 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1696 
1697 
1698 	if (!res) {
1699 		/* Try 24 bpp linebuffer */
1700 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
1701 
1702 		if (pipe_ctx->plane_res.xfm != NULL)
1703 			res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1704 					pipe_ctx->plane_res.xfm,
1705 					&pipe_ctx->plane_res.scl_data,
1706 					&plane_state->scaling_quality);
1707 
1708 		if (pipe_ctx->plane_res.dpp != NULL)
1709 			res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1710 					pipe_ctx->plane_res.dpp,
1711 					&pipe_ctx->plane_res.scl_data,
1712 					&plane_state->scaling_quality);
1713 	}
1714 
1715 	/* Ignore scaler failure if pipe context plane is phantom plane */
1716 	if (!res && plane_state->is_phantom)
1717 		res = true;
1718 
1719 	if (res && (pipe_ctx->plane_res.scl_data.taps.v_taps != temp.v_taps ||
1720 		pipe_ctx->plane_res.scl_data.taps.h_taps != temp.h_taps ||
1721 		pipe_ctx->plane_res.scl_data.taps.v_taps_c != temp.v_taps_c ||
1722 		pipe_ctx->plane_res.scl_data.taps.h_taps_c != temp.h_taps_c))
1723 		calculate_inits_and_viewports(pipe_ctx);
1724 
1725 	/*
1726 	 * Handle side by side and top bottom 3d recout offsets after vp calculation
1727 	 * since 3d is special and needs to calculate vp as if there is no recout offset
1728 	 * This may break with rotation, good thing we aren't mixing hw rotation and 3d
1729 	 */
1730 	if (pipe_ctx->top_pipe && pipe_ctx->top_pipe->plane_state == plane_state) {
1731 		ASSERT(plane_state->rotation == ROTATION_ANGLE_0 ||
1732 			(pipe_ctx->stream->view_format != VIEW_3D_FORMAT_TOP_AND_BOTTOM &&
1733 				pipe_ctx->stream->view_format != VIEW_3D_FORMAT_SIDE_BY_SIDE));
1734 		if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
1735 			pipe_ctx->plane_res.scl_data.recout.y += pipe_ctx->plane_res.scl_data.recout.height;
1736 		else if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
1737 			pipe_ctx->plane_res.scl_data.recout.x += pipe_ctx->plane_res.scl_data.recout.width;
1738 	}
1739 
1740 	/* Clamp minimum viewport size */
1741 	if (pipe_ctx->plane_res.scl_data.viewport.height < MIN_VIEWPORT_SIZE)
1742 		pipe_ctx->plane_res.scl_data.viewport.height = MIN_VIEWPORT_SIZE;
1743 	if (pipe_ctx->plane_res.scl_data.viewport.width < MIN_VIEWPORT_SIZE)
1744 		pipe_ctx->plane_res.scl_data.viewport.width = MIN_VIEWPORT_SIZE;
1745 #ifdef CONFIG_DRM_AMD_DC_FP
1746 	}
1747 #endif
1748 	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"
1749 			"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",
1750 			__func__,
1751 			pipe_ctx->pipe_idx,
1752 			pipe_ctx->plane_res.scl_data.viewport.height,
1753 			pipe_ctx->plane_res.scl_data.viewport.width,
1754 			pipe_ctx->plane_res.scl_data.viewport.x,
1755 			pipe_ctx->plane_res.scl_data.viewport.y,
1756 			pipe_ctx->plane_res.scl_data.recout.height,
1757 			pipe_ctx->plane_res.scl_data.recout.width,
1758 			pipe_ctx->plane_res.scl_data.recout.x,
1759 			pipe_ctx->plane_res.scl_data.recout.y,
1760 			pipe_ctx->plane_res.scl_data.h_active,
1761 			pipe_ctx->plane_res.scl_data.v_active,
1762 			plane_state->src_rect.height,
1763 			plane_state->src_rect.width,
1764 			plane_state->src_rect.x,
1765 			plane_state->src_rect.y,
1766 			plane_state->dst_rect.height,
1767 			plane_state->dst_rect.width,
1768 			plane_state->dst_rect.x,
1769 			plane_state->dst_rect.y,
1770 			plane_state->clip_rect.height,
1771 			plane_state->clip_rect.width,
1772 			plane_state->clip_rect.x,
1773 			plane_state->clip_rect.y);
1774 
1775 	pipe_ctx->stream->dst.x -= timing->h_border_left;
1776 	pipe_ctx->stream->dst.y -= timing->v_border_top;
1777 
1778 	return res;
1779 }
1780 
1781 bool resource_can_pipe_disable_cursor(const struct pipe_ctx *pipe_ctx)
1782 {
1783 	struct pipe_ctx *test_pipe, *split_pipe;
1784 	struct rect r1 = pipe_ctx->plane_res.scl_data.recout;
1785 	int r1_right, r1_bottom;
1786 	unsigned int cur_layer = pipe_ctx->plane_state->layer_index;
1787 
1788 	reverse_adjust_recout_for_visual_confirm(&r1, pipe_ctx);
1789 	r1_right = r1.x + r1.width;
1790 	r1_bottom = r1.y + r1.height;
1791 
1792 	/**
1793 	 * Disable the cursor if there's another pipe above this with a
1794 	 * plane that contains this pipe's viewport to prevent double cursor
1795 	 * and incorrect scaling artifacts.
1796 	 */
1797 	for (test_pipe = pipe_ctx->top_pipe; test_pipe;
1798 	     test_pipe = test_pipe->top_pipe) {
1799 		struct rect r2;
1800 		int r2_right, r2_bottom;
1801 		// Skip invisible layer and pipe-split plane on same layer
1802 		if (!test_pipe->plane_state ||
1803 		    !test_pipe->plane_state->visible ||
1804 		    test_pipe->plane_state->layer_index == cur_layer)
1805 			continue;
1806 
1807 		r2 = test_pipe->plane_res.scl_data.recout;
1808 		reverse_adjust_recout_for_visual_confirm(&r2, test_pipe);
1809 		r2_right = r2.x + r2.width;
1810 		r2_bottom = r2.y + r2.height;
1811 
1812 		/**
1813 		 * There is another half plane on same layer because of
1814 		 * pipe-split, merge together per same height.
1815 		 */
1816 		for (split_pipe = pipe_ctx->top_pipe; split_pipe;
1817 		     split_pipe = split_pipe->top_pipe) {
1818 
1819 			if (split_pipe == test_pipe)
1820 				continue;
1821 
1822 			if (split_pipe->plane_state->layer_index == test_pipe->plane_state->layer_index) {
1823 				struct rect r2_half;
1824 
1825 				r2_half = split_pipe->plane_res.scl_data.recout;
1826 				reverse_adjust_recout_for_visual_confirm(&r2_half, split_pipe);
1827 				r2.x = min(r2_half.x, r2.x);
1828 				r2.width = r2.width + r2_half.width;
1829 				r2_right = r2.x + r2.width;
1830 				r2_bottom = min(r2_bottom, r2_half.y + r2_half.height);
1831 				break;
1832 			}
1833 		}
1834 
1835 		if (r1.x >= r2.x && r1.y >= r2.y && r1_right <= r2_right && r1_bottom <= r2_bottom)
1836 			return true;
1837 	}
1838 
1839 	return false;
1840 }
1841 
1842 
1843 enum dc_status resource_build_scaling_params_for_context(
1844 	const struct dc  *dc,
1845 	struct dc_state *context)
1846 {
1847 	(void)dc;
1848 	int i;
1849 
1850 	for (i = 0; i < MAX_PIPES; i++) {
1851 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
1852 				context->res_ctx.pipe_ctx[i].stream != NULL)
1853 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1854 				return DC_FAIL_SCALING;
1855 	}
1856 
1857 	return DC_OK;
1858 }
1859 
1860 struct pipe_ctx *resource_find_free_secondary_pipe_legacy(
1861 		struct resource_context *res_ctx,
1862 		const struct resource_pool *pool,
1863 		const struct pipe_ctx *primary_pipe)
1864 {
1865 	int i;
1866 	struct pipe_ctx *secondary_pipe = NULL;
1867 
1868 	/*
1869 	 * We add a preferred pipe mapping to avoid the chance that
1870 	 * MPCCs already in use will need to be reassigned to other trees.
1871 	 * For example, if we went with the strict, assign backwards logic:
1872 	 *
1873 	 * (State 1)
1874 	 * Display A on, no surface, top pipe = 0
1875 	 * Display B on, no surface, top pipe = 1
1876 	 *
1877 	 * (State 2)
1878 	 * Display A on, no surface, top pipe = 0
1879 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 5
1880 	 *
1881 	 * (State 3)
1882 	 * Display A on, surface enable, top pipe = 0, bottom pipe = 5
1883 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1884 	 *
1885 	 * The state 2->3 transition requires remapping MPCC 5 from display B
1886 	 * to display A.
1887 	 *
1888 	 * However, with the preferred pipe logic, state 2 would look like:
1889 	 *
1890 	 * (State 2)
1891 	 * Display A on, no surface, top pipe = 0
1892 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1893 	 *
1894 	 * This would then cause 2->3 to not require remapping any MPCCs.
1895 	 */
1896 	if (primary_pipe) {
1897 		int preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
1898 		if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1899 			secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1900 			secondary_pipe->pipe_idx = (uint8_t)preferred_pipe_idx;
1901 		}
1902 	}
1903 
1904 	/*
1905 	 * search backwards for the second pipe to keep pipe
1906 	 * assignment more consistent
1907 	 */
1908 	if (!secondary_pipe)
1909 		for (i = pool->pipe_count - 1; i >= 0; i--) {
1910 			if (res_ctx->pipe_ctx[i].stream == NULL) {
1911 				secondary_pipe = &res_ctx->pipe_ctx[i];
1912 				secondary_pipe->pipe_idx = (uint8_t)i;
1913 				break;
1914 			}
1915 		}
1916 
1917 	return secondary_pipe;
1918 }
1919 
1920 int resource_find_free_pipe_used_as_sec_opp_head_by_cur_otg_master(
1921 		const struct resource_context *cur_res_ctx,
1922 		struct resource_context *new_res_ctx,
1923 		const struct pipe_ctx *cur_otg_master)
1924 {
1925 	(void)cur_res_ctx;
1926 	const struct pipe_ctx *cur_sec_opp_head = cur_otg_master->next_odm_pipe;
1927 	struct pipe_ctx *new_pipe;
1928 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1929 
1930 	while (cur_sec_opp_head) {
1931 		new_pipe = &new_res_ctx->pipe_ctx[cur_sec_opp_head->pipe_idx];
1932 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1933 			free_pipe_idx = cur_sec_opp_head->pipe_idx;
1934 			break;
1935 		}
1936 		cur_sec_opp_head = cur_sec_opp_head->next_odm_pipe;
1937 	}
1938 
1939 	return free_pipe_idx;
1940 }
1941 
1942 int resource_find_free_pipe_used_in_cur_mpc_blending_tree(
1943 		const struct resource_context *cur_res_ctx,
1944 		struct resource_context *new_res_ctx,
1945 		const struct pipe_ctx *cur_opp_head)
1946 {
1947 	(void)cur_res_ctx;
1948 	const struct pipe_ctx *cur_sec_dpp = cur_opp_head->bottom_pipe;
1949 	struct pipe_ctx *new_pipe;
1950 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1951 
1952 	while (cur_sec_dpp) {
1953 		/* find a free pipe used in current opp blend tree,
1954 		 * this is to avoid MPO pipe switching to different opp blending
1955 		 * tree
1956 		 */
1957 		new_pipe = &new_res_ctx->pipe_ctx[cur_sec_dpp->pipe_idx];
1958 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1959 			free_pipe_idx = cur_sec_dpp->pipe_idx;
1960 			break;
1961 		}
1962 		cur_sec_dpp = cur_sec_dpp->bottom_pipe;
1963 	}
1964 
1965 	return free_pipe_idx;
1966 }
1967 
1968 int recource_find_free_pipe_not_used_in_cur_res_ctx(
1969 		const struct resource_context *cur_res_ctx,
1970 		struct resource_context *new_res_ctx,
1971 		const struct resource_pool *pool)
1972 {
1973 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1974 	const struct pipe_ctx *new_pipe, *cur_pipe;
1975 	unsigned int i;
1976 
1977 	for (i = 0; i < pool->pipe_count; i++) {
1978 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1979 		new_pipe = &new_res_ctx->pipe_ctx[i];
1980 
1981 		if (resource_is_pipe_type(cur_pipe, FREE_PIPE) &&
1982 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1983 			free_pipe_idx = i;
1984 			break;
1985 		}
1986 	}
1987 
1988 	return free_pipe_idx;
1989 }
1990 
1991 int recource_find_free_pipe_used_as_otg_master_in_cur_res_ctx(
1992 		const struct resource_context *cur_res_ctx,
1993 		struct resource_context *new_res_ctx,
1994 		const struct resource_pool *pool)
1995 {
1996 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1997 	const struct pipe_ctx *new_pipe, *cur_pipe;
1998 	unsigned int i;
1999 
2000 	for (i = 0; i < pool->pipe_count; i++) {
2001 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
2002 		new_pipe = &new_res_ctx->pipe_ctx[i];
2003 
2004 		if (resource_is_pipe_type(cur_pipe, OTG_MASTER) &&
2005 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
2006 			free_pipe_idx = i;
2007 			break;
2008 		}
2009 	}
2010 
2011 	return free_pipe_idx;
2012 }
2013 
2014 int resource_find_free_pipe_used_as_cur_sec_dpp(
2015 		const struct resource_context *cur_res_ctx,
2016 		struct resource_context *new_res_ctx,
2017 		const struct resource_pool *pool)
2018 {
2019 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
2020 	const struct pipe_ctx *new_pipe, *cur_pipe;
2021 	unsigned int i;
2022 
2023 	for (i = 0; i < pool->pipe_count; i++) {
2024 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
2025 		new_pipe = &new_res_ctx->pipe_ctx[i];
2026 
2027 		if (resource_is_pipe_type(cur_pipe, DPP_PIPE) &&
2028 				!resource_is_pipe_type(cur_pipe, OPP_HEAD) &&
2029 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
2030 			free_pipe_idx = i;
2031 			break;
2032 		}
2033 	}
2034 
2035 	return free_pipe_idx;
2036 }
2037 
2038 int resource_find_free_pipe_used_as_cur_sec_dpp_in_mpcc_combine(
2039 		const struct resource_context *cur_res_ctx,
2040 		struct resource_context *new_res_ctx,
2041 		const struct resource_pool *pool)
2042 {
2043 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
2044 	const struct pipe_ctx *new_pipe, *cur_pipe;
2045 	unsigned int i;
2046 
2047 	for (i = 0; i < pool->pipe_count; i++) {
2048 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
2049 		new_pipe = &new_res_ctx->pipe_ctx[i];
2050 
2051 		if (resource_is_pipe_type(cur_pipe, DPP_PIPE) &&
2052 				!resource_is_pipe_type(cur_pipe, OPP_HEAD) &&
2053 				resource_get_mpc_slice_index(cur_pipe) > 0 &&
2054 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
2055 			free_pipe_idx = i;
2056 			break;
2057 		}
2058 	}
2059 
2060 	return free_pipe_idx;
2061 }
2062 
2063 int resource_find_any_free_pipe(struct resource_context *new_res_ctx,
2064 		const struct resource_pool *pool)
2065 {
2066 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
2067 	const struct pipe_ctx *new_pipe;
2068 	unsigned int i;
2069 
2070 	for (i = 0; i < pool->pipe_count; i++) {
2071 		new_pipe = &new_res_ctx->pipe_ctx[i];
2072 
2073 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
2074 			free_pipe_idx = i;
2075 			break;
2076 		}
2077 	}
2078 
2079 	return free_pipe_idx;
2080 }
2081 
2082 bool resource_is_pipe_type(const struct pipe_ctx *pipe_ctx, enum pipe_type type)
2083 {
2084 	switch (type) {
2085 	case OTG_MASTER:
2086 		return !pipe_ctx->prev_odm_pipe &&
2087 				!pipe_ctx->top_pipe &&
2088 				pipe_ctx->stream;
2089 	case OPP_HEAD:
2090 		return !pipe_ctx->top_pipe && pipe_ctx->stream;
2091 	case DPP_PIPE:
2092 		return pipe_ctx->plane_state && pipe_ctx->stream;
2093 	case FREE_PIPE:
2094 		return !pipe_ctx->plane_state && !pipe_ctx->stream;
2095 	default:
2096 		return false;
2097 	}
2098 }
2099 
2100 struct pipe_ctx *resource_get_otg_master_for_stream(
2101 		struct resource_context *res_ctx,
2102 		const struct dc_stream_state *stream)
2103 {
2104 	int i;
2105 
2106 	for (i = 0; i < MAX_PIPES; i++) {
2107 		if (res_ctx->pipe_ctx[i].stream == stream &&
2108 				resource_is_pipe_type(&res_ctx->pipe_ctx[i], OTG_MASTER))
2109 			return &res_ctx->pipe_ctx[i];
2110 	}
2111 	return NULL;
2112 }
2113 
2114 int resource_get_opp_heads_for_otg_master(const struct pipe_ctx *otg_master,
2115 		struct resource_context *res_ctx,
2116 		struct pipe_ctx *opp_heads[MAX_PIPES])
2117 {
2118 	struct pipe_ctx *opp_head = &res_ctx->pipe_ctx[otg_master->pipe_idx];
2119 	struct dc *dc = otg_master->stream->ctx->dc;
2120 	int i = 0;
2121 
2122 	DC_LOGGER_INIT(dc->ctx->logger);
2123 
2124 	if (!resource_is_pipe_type(otg_master, OTG_MASTER)) {
2125 		DC_LOG_WARNING("%s called from a non OTG master, something "
2126 			       "is wrong in the pipe configuration",
2127 			       __func__);
2128 		ASSERT(0);
2129 		return 0;
2130 	}
2131 	while (opp_head) {
2132 		ASSERT(i < MAX_PIPES);
2133 		opp_heads[i++] = opp_head;
2134 		opp_head = opp_head->next_odm_pipe;
2135 	}
2136 	return i;
2137 }
2138 
2139 int resource_get_dpp_pipes_for_opp_head(const struct pipe_ctx *opp_head,
2140 		struct resource_context *res_ctx,
2141 		struct pipe_ctx *dpp_pipes[MAX_PIPES])
2142 {
2143 	struct pipe_ctx *pipe = &res_ctx->pipe_ctx[opp_head->pipe_idx];
2144 	int i = 0;
2145 
2146 	if (!resource_is_pipe_type(opp_head, OPP_HEAD)) {
2147 		ASSERT(0);
2148 		return 0;
2149 	}
2150 	while (pipe && resource_is_pipe_type(pipe, DPP_PIPE)) {
2151 		ASSERT(i < MAX_PIPES);
2152 		dpp_pipes[i++] = pipe;
2153 		pipe = pipe->bottom_pipe;
2154 	}
2155 	return i;
2156 }
2157 
2158 int resource_get_dpp_pipes_for_plane(const struct dc_plane_state *plane,
2159 		struct resource_context *res_ctx,
2160 		struct pipe_ctx *dpp_pipes[MAX_PIPES])
2161 {
2162 	int i = 0, j;
2163 	struct pipe_ctx *pipe;
2164 
2165 	for (j = 0; j < MAX_PIPES; j++) {
2166 		pipe = &res_ctx->pipe_ctx[j];
2167 		if (pipe->plane_state == plane && pipe->prev_odm_pipe == NULL) {
2168 			if (resource_is_pipe_type(pipe, OPP_HEAD) ||
2169 					pipe->top_pipe->plane_state != plane)
2170 				break;
2171 		}
2172 	}
2173 
2174 	if (j < MAX_PIPES) {
2175 		if (pipe->next_odm_pipe)
2176 			while (pipe) {
2177 				dpp_pipes[i++] = pipe;
2178 				pipe = pipe->next_odm_pipe;
2179 			}
2180 		else
2181 			while (pipe && pipe->plane_state == plane) {
2182 				dpp_pipes[i++] = pipe;
2183 				pipe = pipe->bottom_pipe;
2184 			}
2185 	}
2186 	return i;
2187 }
2188 
2189 struct pipe_ctx *resource_get_otg_master(const struct pipe_ctx *pipe_ctx)
2190 {
2191 	struct pipe_ctx *otg_master = resource_get_opp_head(pipe_ctx);
2192 
2193 	while (otg_master->prev_odm_pipe)
2194 		otg_master = otg_master->prev_odm_pipe;
2195 	return otg_master;
2196 }
2197 
2198 struct pipe_ctx *resource_get_opp_head(const struct pipe_ctx *pipe_ctx)
2199 {
2200 	struct pipe_ctx *opp_head = (struct pipe_ctx *) pipe_ctx;
2201 
2202 	ASSERT(!resource_is_pipe_type(opp_head, FREE_PIPE));
2203 	while (opp_head->top_pipe)
2204 		opp_head = opp_head->top_pipe;
2205 	return opp_head;
2206 }
2207 
2208 struct pipe_ctx *resource_get_primary_dpp_pipe(const struct pipe_ctx *dpp_pipe)
2209 {
2210 	struct pipe_ctx *pri_dpp_pipe = (struct pipe_ctx *) dpp_pipe;
2211 
2212 	ASSERT(resource_is_pipe_type(dpp_pipe, DPP_PIPE));
2213 	while (pri_dpp_pipe->prev_odm_pipe)
2214 		pri_dpp_pipe = pri_dpp_pipe->prev_odm_pipe;
2215 	while (pri_dpp_pipe->top_pipe &&
2216 			pri_dpp_pipe->top_pipe->plane_state == pri_dpp_pipe->plane_state)
2217 		pri_dpp_pipe = pri_dpp_pipe->top_pipe;
2218 	return pri_dpp_pipe;
2219 }
2220 
2221 
2222 int resource_get_mpc_slice_index(const struct pipe_ctx *pipe_ctx)
2223 {
2224 	struct pipe_ctx *split_pipe = pipe_ctx->top_pipe;
2225 	int index = 0;
2226 
2227 	while (split_pipe && split_pipe->plane_state == pipe_ctx->plane_state) {
2228 		index++;
2229 		split_pipe = split_pipe->top_pipe;
2230 	}
2231 
2232 	return index;
2233 }
2234 
2235 int resource_get_mpc_slice_count(const struct pipe_ctx *pipe)
2236 {
2237 	int mpc_split_count = 1;
2238 	const struct pipe_ctx *other_pipe = pipe->bottom_pipe;
2239 
2240 	while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
2241 		mpc_split_count++;
2242 		other_pipe = other_pipe->bottom_pipe;
2243 	}
2244 	other_pipe = pipe->top_pipe;
2245 	while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
2246 		mpc_split_count++;
2247 		other_pipe = other_pipe->top_pipe;
2248 	}
2249 
2250 	return mpc_split_count;
2251 }
2252 
2253 int resource_get_odm_slice_count(const struct pipe_ctx *pipe)
2254 {
2255 	int odm_split_count = 1;
2256 
2257 	pipe = resource_get_otg_master(pipe);
2258 
2259 	while (pipe->next_odm_pipe) {
2260 		odm_split_count++;
2261 		pipe = pipe->next_odm_pipe;
2262 	}
2263 	return odm_split_count;
2264 }
2265 
2266 int resource_get_odm_slice_index(const struct pipe_ctx *pipe_ctx)
2267 {
2268 	int index = 0;
2269 
2270 	pipe_ctx = resource_get_opp_head(pipe_ctx);
2271 	if (!pipe_ctx)
2272 		return 0;
2273 
2274 	while (pipe_ctx->prev_odm_pipe) {
2275 		index++;
2276 		pipe_ctx = pipe_ctx->prev_odm_pipe;
2277 	}
2278 
2279 	return index;
2280 }
2281 
2282 int resource_get_odm_slice_dst_width(struct pipe_ctx *otg_master,
2283 		bool is_last_segment)
2284 {
2285 	const struct dc_crtc_timing *timing;
2286 	int count;
2287 	int h_active;
2288 	int width;
2289 	bool two_pixel_alignment_required = false;
2290 
2291 	if (!otg_master || !otg_master->stream)
2292 		return 0;
2293 
2294 	timing = &otg_master->stream->timing;
2295 	count = resource_get_odm_slice_count(otg_master);
2296 	h_active = timing->h_addressable +
2297 			timing->h_border_left +
2298 			timing->h_border_right +
2299 			otg_master->dsc_padding_params.dsc_hactive_padding;
2300 	width = h_active / count;
2301 
2302 	if (otg_master->stream_res.tg)
2303 		two_pixel_alignment_required =
2304 				otg_master->stream_res.tg->funcs->is_two_pixels_per_container(timing) ||
2305 				/*
2306 				 * 422 is sub-sampled horizontally. 1 set of chromas
2307 				 * (Cb/Cr) is shared for 2 lumas (i.e 2 Y values).
2308 				 * Therefore even if 422 is still 1 pixel per container,
2309 				 * ODM segment width still needs to be 2 pixel aligned.
2310 				 */
2311 				timing->pixel_encoding == PIXEL_ENCODING_YCBCR422;
2312 	if ((width % 2) && two_pixel_alignment_required)
2313 		width++;
2314 
2315 	return is_last_segment ?
2316 			h_active - width * (count - 1) :
2317 			width;
2318 }
2319 
2320 struct rect resource_get_odm_slice_dst_rect(struct pipe_ctx *pipe_ctx)
2321 {
2322 	const struct dc_stream_state *stream = pipe_ctx->stream;
2323 	bool is_last_odm_slice = pipe_ctx->next_odm_pipe == NULL;
2324 	struct pipe_ctx *otg_master = resource_get_otg_master(pipe_ctx);
2325 	int odm_slice_idx = resource_get_odm_slice_index(pipe_ctx);
2326 	int odm_segment_offset = resource_get_odm_slice_dst_width(otg_master, false);
2327 	struct rect odm_slice_dst;
2328 
2329 	odm_slice_dst.x = odm_segment_offset * odm_slice_idx;
2330 	odm_slice_dst.width = resource_get_odm_slice_dst_width(otg_master, is_last_odm_slice);
2331 	odm_slice_dst.y = 0;
2332 	odm_slice_dst.height = stream->timing.v_addressable +
2333 			stream->timing.v_border_bottom +
2334 			stream->timing.v_border_top;
2335 
2336 	return odm_slice_dst;
2337 }
2338 
2339 struct rect resource_get_odm_slice_src_rect(struct pipe_ctx *pipe_ctx)
2340 {
2341 	struct rect odm_slice_dst;
2342 	struct rect odm_slice_src;
2343 	struct pipe_ctx *opp_head = resource_get_opp_head(pipe_ctx);
2344 	struct output_pixel_processor *opp = opp_head->stream_res.opp;
2345 	uint32_t left_edge_extra_pixel_count;
2346 
2347 	odm_slice_dst = resource_get_odm_slice_dst_rect(opp_head);
2348 	odm_slice_src = odm_slice_dst;
2349 
2350 	if (opp && opp->funcs->opp_get_left_edge_extra_pixel_count)
2351 		left_edge_extra_pixel_count =
2352 				opp->funcs->opp_get_left_edge_extra_pixel_count(
2353 						opp, pipe_ctx->stream->timing.pixel_encoding,
2354 						resource_is_pipe_type(opp_head, OTG_MASTER));
2355 	else
2356 		left_edge_extra_pixel_count = 0;
2357 
2358 	odm_slice_src.x -= left_edge_extra_pixel_count;
2359 	odm_slice_src.width += left_edge_extra_pixel_count;
2360 
2361 	return odm_slice_src;
2362 }
2363 
2364 bool resource_is_pipe_topology_changed(const struct dc_state *state_a,
2365 		const struct dc_state *state_b)
2366 {
2367 	int i;
2368 	const struct pipe_ctx *pipe_a, *pipe_b;
2369 
2370 	if (state_a->stream_count != state_b->stream_count)
2371 		return true;
2372 
2373 	for (i = 0; i < MAX_PIPES; i++) {
2374 		pipe_a = &state_a->res_ctx.pipe_ctx[i];
2375 		pipe_b = &state_b->res_ctx.pipe_ctx[i];
2376 
2377 		if (pipe_a->stream && !pipe_b->stream)
2378 			return true;
2379 		else if (!pipe_a->stream && pipe_b->stream)
2380 			return true;
2381 
2382 		if (pipe_a->plane_state && !pipe_b->plane_state)
2383 			return true;
2384 		else if (!pipe_a->plane_state && pipe_b->plane_state)
2385 			return true;
2386 
2387 		if (pipe_a->bottom_pipe && pipe_b->bottom_pipe) {
2388 			if (pipe_a->bottom_pipe->pipe_idx != pipe_b->bottom_pipe->pipe_idx)
2389 				return true;
2390 			if ((pipe_a->bottom_pipe->plane_state == pipe_a->plane_state) &&
2391 					(pipe_b->bottom_pipe->plane_state != pipe_b->plane_state))
2392 				return true;
2393 			else if ((pipe_a->bottom_pipe->plane_state != pipe_a->plane_state) &&
2394 					(pipe_b->bottom_pipe->plane_state == pipe_b->plane_state))
2395 				return true;
2396 		} else if (pipe_a->bottom_pipe || pipe_b->bottom_pipe) {
2397 			return true;
2398 		}
2399 
2400 		if (pipe_a->next_odm_pipe && pipe_b->next_odm_pipe) {
2401 			if (pipe_a->next_odm_pipe->pipe_idx != pipe_b->next_odm_pipe->pipe_idx)
2402 				return true;
2403 		} else if (pipe_a->next_odm_pipe || pipe_b->next_odm_pipe) {
2404 			return true;
2405 		}
2406 	}
2407 	return false;
2408 }
2409 
2410 bool resource_is_odm_topology_changed(const struct pipe_ctx *otg_master_a,
2411 		const struct pipe_ctx *otg_master_b)
2412 {
2413 	const struct pipe_ctx *opp_head_a = otg_master_a;
2414 	const struct pipe_ctx *opp_head_b = otg_master_b;
2415 
2416 	if (!resource_is_pipe_type(otg_master_a, OTG_MASTER) ||
2417 			!resource_is_pipe_type(otg_master_b, OTG_MASTER))
2418 		return true;
2419 
2420 	while (opp_head_a && opp_head_b) {
2421 		if (opp_head_a->stream_res.opp != opp_head_b->stream_res.opp)
2422 			return true;
2423 		if ((opp_head_a->next_odm_pipe && !opp_head_b->next_odm_pipe) ||
2424 				(!opp_head_a->next_odm_pipe && opp_head_b->next_odm_pipe))
2425 			return true;
2426 		opp_head_a = opp_head_a->next_odm_pipe;
2427 		opp_head_b = opp_head_b->next_odm_pipe;
2428 	}
2429 
2430 	return false;
2431 }
2432 
2433 /*
2434  * Sample log:
2435  *    pipe topology update
2436  *  ________________________
2437  * | plane0  slice0  stream0|
2438  * |DPP0----OPP0----OTG0----| <--- case 0 (OTG master pipe with plane)
2439  * | plane1 |       |       |
2440  * |DPP1----|       |       | <--- case 5 (DPP pipe not in last slice)
2441  * | plane0  slice1 |       |
2442  * |DPP2----OPP2----|       | <--- case 2 (OPP head pipe with plane)
2443  * | plane1 |               |
2444  * |DPP3----|               | <--- case 4 (DPP pipe in last slice)
2445  * |         slice0  stream1|
2446  * |DPG4----OPP4----OTG4----| <--- case 1 (OTG master pipe without plane)
2447  * |         slice1 |       |
2448  * |DPG5----OPP5----|       | <--- case 3 (OPP head pipe without plane)
2449  * |________________________|
2450  */
2451 
2452 static void resource_log_pipe(struct dc *dc, struct pipe_ctx *pipe,
2453 		int stream_idx, int slice_idx, int plane_idx, int slice_count,
2454 		bool is_primary, bool is_phantom_pipe)
2455 {
2456 	DC_LOGGER_INIT(dc->ctx->logger);
2457 
2458 	// new format for logging: bit storing code
2459 	if (slice_idx == 0 && plane_idx == 0 && is_primary) {
2460 		/* case 0 (OTG master pipe with plane) */
2461 		DC_LOG_DC(" | plane%d  slice%d  stream%d|",
2462 				plane_idx, slice_idx, stream_idx);
2463 		DC_LOG_DC(" |DPP%d----OPP%d----OTG%d----|",
2464 				pipe->plane_res.dpp->inst,
2465 				pipe->stream_res.opp->inst,
2466 				pipe->stream_res.tg->inst);
2467 		capture_pipe_topology_data(dc, plane_idx, slice_idx, stream_idx,
2468 				pipe->plane_res.dpp->inst,
2469 				pipe->stream_res.opp->inst,
2470 				pipe->stream_res.tg->inst, is_phantom_pipe);
2471 	} else if (slice_idx == 0 && plane_idx == -1) {
2472 		/* case 1 (OTG master pipe without plane) */
2473 		DC_LOG_DC(" |         slice%d  stream%d|",
2474 				slice_idx, stream_idx);
2475 		DC_LOG_DC(" |DPG%d----OPP%d----OTG%d----|",
2476 				pipe->stream_res.opp->inst,
2477 				pipe->stream_res.opp->inst,
2478 				pipe->stream_res.tg->inst);
2479 		capture_pipe_topology_data(dc, 0xF, slice_idx, stream_idx,
2480 				pipe->plane_res.dpp->inst,
2481 				pipe->stream_res.opp->inst,
2482 				pipe->stream_res.tg->inst, is_phantom_pipe);
2483 	} else if (slice_idx != 0 && plane_idx == 0 && is_primary) {
2484 		/* case 2 (OPP head pipe with plane) */
2485 		DC_LOG_DC(" | plane%d  slice%d |       |",
2486 				plane_idx, slice_idx);
2487 		DC_LOG_DC(" |DPP%d----OPP%d----|       |",
2488 				pipe->plane_res.dpp->inst,
2489 				pipe->stream_res.opp->inst);
2490 		capture_pipe_topology_data(dc, plane_idx, slice_idx, stream_idx,
2491 				pipe->plane_res.dpp->inst,
2492 				pipe->stream_res.opp->inst,
2493 				pipe->stream_res.tg->inst, is_phantom_pipe);
2494 	} else if (slice_idx != 0 && plane_idx == -1) {
2495 		/* case 3 (OPP head pipe without plane) */
2496 		DC_LOG_DC(" |         slice%d |       |", slice_idx);
2497 		DC_LOG_DC(" |DPG%d----OPP%d----|       |",
2498 				pipe->plane_res.dpp->inst,
2499 				pipe->stream_res.opp->inst);
2500 		capture_pipe_topology_data(dc, 0xF, slice_idx, stream_idx,
2501 				pipe->plane_res.dpp->inst,
2502 				pipe->stream_res.opp->inst,
2503 				pipe->stream_res.tg->inst, is_phantom_pipe);
2504 	} else if (slice_idx == slice_count - 1) {
2505 		/* case 4 (DPP pipe in last slice) */
2506 		DC_LOG_DC(" | plane%d |               |", plane_idx);
2507 		DC_LOG_DC(" |DPP%d----|               |",
2508 				pipe->plane_res.dpp->inst);
2509 		capture_pipe_topology_data(dc, plane_idx, slice_idx, stream_idx,
2510 				pipe->plane_res.dpp->inst,
2511 				pipe->stream_res.opp->inst,
2512 				pipe->stream_res.tg->inst, is_phantom_pipe);
2513 	} else {
2514 		/* case 5 (DPP pipe not in last slice) */
2515 		DC_LOG_DC(" | plane%d |       |       |", plane_idx);
2516 		DC_LOG_DC(" |DPP%d----|       |       |",
2517 				pipe->plane_res.dpp->inst);
2518 		capture_pipe_topology_data(dc, plane_idx, slice_idx, stream_idx,
2519 				pipe->plane_res.dpp->inst,
2520 				pipe->stream_res.opp->inst,
2521 				pipe->stream_res.tg->inst, is_phantom_pipe);
2522 	}
2523 }
2524 
2525 static void resource_log_pipe_for_stream(struct dc *dc, struct dc_state *state,
2526 		struct pipe_ctx *otg_master, int stream_idx, bool is_phantom_pipe)
2527 {
2528 	struct pipe_ctx *opp_heads[MAX_PIPES];
2529 	struct pipe_ctx *dpp_pipes[MAX_PIPES];
2530 
2531 	int slice_idx, dpp_idx, plane_idx, slice_count, dpp_count;
2532 	bool is_primary;
2533 
2534 	slice_count = resource_get_opp_heads_for_otg_master(otg_master,
2535 			&state->res_ctx, opp_heads);
2536 	for (slice_idx = 0; slice_idx < slice_count; slice_idx++) {
2537 		plane_idx = -1;
2538 		if (opp_heads[slice_idx]->plane_state) {
2539 			dpp_count = resource_get_dpp_pipes_for_opp_head(
2540 					opp_heads[slice_idx],
2541 					&state->res_ctx,
2542 					dpp_pipes);
2543 			for (dpp_idx = 0; dpp_idx < dpp_count; dpp_idx++) {
2544 				is_primary = !dpp_pipes[dpp_idx]->top_pipe ||
2545 						dpp_pipes[dpp_idx]->top_pipe->plane_state != dpp_pipes[dpp_idx]->plane_state;
2546 				if (is_primary)
2547 					plane_idx++;
2548 				resource_log_pipe(dc, dpp_pipes[dpp_idx],
2549 						stream_idx, slice_idx,
2550 						plane_idx, slice_count,
2551 						is_primary, is_phantom_pipe);
2552 			}
2553 		} else {
2554 			resource_log_pipe(dc, opp_heads[slice_idx],
2555 					stream_idx, slice_idx, plane_idx,
2556 					slice_count, true, is_phantom_pipe);
2557 		}
2558 
2559 	}
2560 }
2561 
2562 static int resource_stream_to_stream_idx(struct dc_state *state,
2563 		struct dc_stream_state *stream)
2564 {
2565 	int i, stream_idx = -1;
2566 
2567 	for (i = 0; i < state->stream_count; i++)
2568 		if (state->streams[i] == stream) {
2569 			stream_idx = i;
2570 			break;
2571 		}
2572 
2573 	/* never return negative array index */
2574 	if (stream_idx == -1) {
2575 		ASSERT(0);
2576 		return 0;
2577 	}
2578 
2579 	return stream_idx;
2580 }
2581 
2582 void resource_log_pipe_topology_update(struct dc *dc, struct dc_state *state)
2583 {
2584 	struct pipe_ctx *otg_master;
2585 	int stream_idx, phantom_stream_idx;
2586 	DC_LOGGER_INIT(dc->ctx->logger);
2587 	bool is_phantom_pipe = false;
2588 
2589 	// Start a new snapshot for this topology update
2590 	start_new_topology_snapshot(dc, state);
2591 
2592 	DC_LOG_DC("    pipe topology update");
2593 	DC_LOG_DC("  ________________________");
2594 	for (stream_idx = 0; stream_idx < state->stream_count; stream_idx++) {
2595 		if (state->streams[stream_idx]->is_phantom)
2596 			continue;
2597 
2598 		otg_master = resource_get_otg_master_for_stream(
2599 				&state->res_ctx, state->streams[stream_idx]);
2600 
2601 		if (!otg_master)
2602 			continue;
2603 
2604 		resource_log_pipe_for_stream(dc, state, otg_master, stream_idx, is_phantom_pipe);
2605 	}
2606 	if (state->phantom_stream_count > 0) {
2607 		is_phantom_pipe = true;
2608 		DC_LOG_DC(" |    (phantom pipes)     |");
2609 		for (stream_idx = 0; stream_idx < state->stream_count; stream_idx++) {
2610 			if (state->stream_status[stream_idx].mall_stream_config.type != SUBVP_MAIN)
2611 				continue;
2612 
2613 			phantom_stream_idx = resource_stream_to_stream_idx(state,
2614 					state->stream_status[stream_idx].mall_stream_config.paired_stream);
2615 			otg_master = resource_get_otg_master_for_stream(
2616 					&state->res_ctx, state->streams[phantom_stream_idx]);
2617 			if (!otg_master)
2618 				continue;
2619 
2620 			resource_log_pipe_for_stream(dc, state, otg_master, stream_idx, is_phantom_pipe);
2621 		}
2622 	}
2623 	DC_LOG_DC(" |________________________|\n");
2624 }
2625 
2626 static struct pipe_ctx *get_tail_pipe(
2627 		struct pipe_ctx *head_pipe)
2628 {
2629 	struct pipe_ctx *tail_pipe = head_pipe->bottom_pipe;
2630 
2631 	while (tail_pipe) {
2632 		head_pipe = tail_pipe;
2633 		tail_pipe = tail_pipe->bottom_pipe;
2634 	}
2635 
2636 	return head_pipe;
2637 }
2638 
2639 static struct pipe_ctx *get_last_opp_head(
2640 		struct pipe_ctx *opp_head)
2641 {
2642 	ASSERT(resource_is_pipe_type(opp_head, OPP_HEAD));
2643 	while (opp_head->next_odm_pipe)
2644 		opp_head = opp_head->next_odm_pipe;
2645 	return opp_head;
2646 }
2647 
2648 static struct pipe_ctx *get_last_dpp_pipe_in_mpcc_combine(
2649 		struct pipe_ctx *dpp_pipe)
2650 {
2651 	ASSERT(resource_is_pipe_type(dpp_pipe, DPP_PIPE));
2652 	while (dpp_pipe->bottom_pipe &&
2653 			dpp_pipe->plane_state == dpp_pipe->bottom_pipe->plane_state)
2654 		dpp_pipe = dpp_pipe->bottom_pipe;
2655 	return dpp_pipe;
2656 }
2657 
2658 static bool update_pipe_params_after_odm_slice_count_change(
2659 		struct pipe_ctx *otg_master,
2660 		struct dc_state *context,
2661 		const struct resource_pool *pool)
2662 {
2663 	unsigned int i;
2664 	struct pipe_ctx *pipe;
2665 	bool result = true;
2666 
2667 	for (i = 0; i < pool->pipe_count && result; i++) {
2668 		pipe = &context->res_ctx.pipe_ctx[i];
2669 		if (pipe->stream == otg_master->stream && pipe->plane_state)
2670 			result = resource_build_scaling_params(pipe);
2671 	}
2672 
2673 	if (pool->funcs->build_pipe_pix_clk_params)
2674 		pool->funcs->build_pipe_pix_clk_params(otg_master);
2675 
2676 	resource_build_test_pattern_params(&context->res_ctx, otg_master);
2677 
2678 	return result;
2679 }
2680 
2681 static bool update_pipe_params_after_mpc_slice_count_change(
2682 		const struct dc_plane_state *plane,
2683 		struct dc_state *context,
2684 		const struct resource_pool *pool)
2685 {
2686 	unsigned int i;
2687 	struct pipe_ctx *pipe;
2688 	bool result = true;
2689 
2690 	for (i = 0; i < pool->pipe_count && result; i++) {
2691 		pipe = &context->res_ctx.pipe_ctx[i];
2692 		if (pipe->plane_state == plane)
2693 			result = resource_build_scaling_params(pipe);
2694 	}
2695 	return result;
2696 }
2697 
2698 static int acquire_first_split_pipe(
2699 		struct resource_context *res_ctx,
2700 		const struct resource_pool *pool,
2701 		struct dc_stream_state *stream)
2702 {
2703 	unsigned int i;
2704 
2705 	for (i = 0; i < pool->pipe_count; i++) {
2706 		struct pipe_ctx *split_pipe = &res_ctx->pipe_ctx[i];
2707 
2708 		if (split_pipe->top_pipe &&
2709 				split_pipe->top_pipe->plane_state == split_pipe->plane_state) {
2710 			split_pipe->top_pipe->bottom_pipe = split_pipe->bottom_pipe;
2711 			if (split_pipe->bottom_pipe)
2712 				split_pipe->bottom_pipe->top_pipe = split_pipe->top_pipe;
2713 
2714 			if (split_pipe->top_pipe->plane_state)
2715 				resource_build_scaling_params(split_pipe->top_pipe);
2716 
2717 			memset(split_pipe, 0, sizeof(*split_pipe));
2718 			split_pipe->stream_res.tg = pool->timing_generators[i];
2719 			split_pipe->plane_res.hubp = pool->hubps[i];
2720 			split_pipe->plane_res.ipp = pool->ipps[i];
2721 			split_pipe->plane_res.dpp = pool->dpps[i];
2722 			split_pipe->stream_res.opp = pool->opps[i];
2723 			split_pipe->plane_res.mpcc_inst = (uint8_t)pool->dpps[i]->inst;
2724 			split_pipe->pipe_idx = (uint8_t)i;
2725 
2726 			split_pipe->stream = stream;
2727 			return (int)i;
2728 		}
2729 	}
2730 	return FREE_PIPE_INDEX_NOT_FOUND;
2731 }
2732 
2733 static void update_stream_engine_usage(
2734 		struct resource_context *res_ctx,
2735 		const struct resource_pool *pool,
2736 		struct stream_encoder *stream_enc,
2737 		bool acquired)
2738 {
2739 	unsigned int i;
2740 
2741 	for (i = 0; i < pool->stream_enc_count; i++) {
2742 		if (pool->stream_enc[i] == stream_enc)
2743 			res_ctx->is_stream_enc_acquired[i] = acquired;
2744 	}
2745 }
2746 
2747 static void update_hpo_frl_stream_engine_usage(
2748 		struct resource_context *res_ctx,
2749 		const struct resource_pool *pool,
2750 		struct hpo_frl_stream_encoder *hpo_frl_stream_enc,
2751 		bool acquired)
2752 {
2753 	unsigned int i;
2754 
2755 	for (i = 0; i < pool->hpo_frl_stream_enc_count; i++) {
2756 		if (pool->hpo_frl_stream_enc[i] == hpo_frl_stream_enc)
2757 			res_ctx->is_hpo_frl_stream_enc_acquired[i] = acquired;
2758 	}
2759 }
2760 
2761 static struct hpo_frl_stream_encoder *find_first_free_match_hpo_frl_stream_enc_for_link(
2762 		struct resource_context *res_ctx,
2763 		const struct resource_pool *pool,
2764 		struct dc_stream_state *stream)
2765 {
2766 	(void)stream;
2767 	unsigned int i;
2768 
2769 	for (i = 0; i < pool->hpo_frl_stream_enc_count; i++) {
2770 		if (!res_ctx->is_hpo_frl_stream_enc_acquired[i] &&
2771 				pool->hpo_frl_stream_enc[i]) {
2772 
2773 			return pool->hpo_frl_stream_enc[i];
2774 		}
2775 	}
2776 
2777 	return NULL;
2778 }
2779 
2780 static inline int find_acquired_hpo_frl_link_enc_for_link(
2781 		const struct resource_context *res_ctx,
2782 		const struct dc_link *link)
2783 {
2784 	int i;
2785 
2786 	for (i = 0; i < ARRAY_SIZE(res_ctx->hpo_frl_link_enc_to_link_idx); i++)
2787 		if (res_ctx->hpo_frl_link_enc_ref_cnts[i] > 0 &&
2788 				res_ctx->hpo_frl_link_enc_to_link_idx[i] == link->link_index)
2789 			return i;
2790 
2791 	return -1;
2792 }
2793 
2794 static inline int find_free_hpo_frl_link_enc(const struct resource_context *res_ctx,
2795 		const struct resource_pool *pool)
2796 {
2797 	unsigned int i;
2798 
2799 	for (i = 0; i < ARRAY_SIZE(res_ctx->hpo_frl_link_enc_ref_cnts); i++)
2800 		if (res_ctx->hpo_frl_link_enc_ref_cnts[i] == 0)
2801 			break;
2802 
2803 	return (i < ARRAY_SIZE(res_ctx->hpo_frl_link_enc_ref_cnts) &&
2804 			i < pool->hpo_frl_link_enc_count) ? (int)i : -1;
2805 }
2806 
2807 static inline void acquire_hpo_frl_link_enc(
2808 		struct resource_context *res_ctx,
2809 		unsigned int link_index,
2810 		int enc_index)
2811 {
2812 	res_ctx->hpo_frl_link_enc_to_link_idx[enc_index] = link_index;
2813 	res_ctx->hpo_frl_link_enc_ref_cnts[enc_index] = 1;
2814 }
2815 
2816 static inline void retain_hpo_frl_link_enc(
2817 		struct resource_context *res_ctx,
2818 		int enc_index)
2819 {
2820 	res_ctx->hpo_frl_link_enc_ref_cnts[enc_index]++;
2821 }
2822 
2823 static inline void release_hpo_frl_link_enc(
2824 		struct resource_context *res_ctx,
2825 		int enc_index)
2826 {
2827 	ASSERT(res_ctx->hpo_frl_link_enc_ref_cnts[enc_index] > 0);
2828 	res_ctx->hpo_frl_link_enc_ref_cnts[enc_index]--;
2829 }
2830 
2831 static bool add_hpo_frl_link_enc_to_ctx(struct resource_context *res_ctx,
2832 		const struct resource_pool *pool,
2833 		struct pipe_ctx *pipe_ctx,
2834 		struct dc_stream_state *stream)
2835 {
2836 	int enc_index;
2837 
2838 	enc_index = find_acquired_hpo_frl_link_enc_for_link(res_ctx, stream->link);
2839 
2840 	if (enc_index >= 0) {
2841 		retain_hpo_frl_link_enc(res_ctx, enc_index);
2842 	} else {
2843 		enc_index = find_free_hpo_frl_link_enc(res_ctx, pool);
2844 		if (enc_index >= 0)
2845 			acquire_hpo_frl_link_enc(res_ctx, stream->link->link_index, enc_index);
2846 	}
2847 
2848 	if (enc_index >= 0)
2849 		pipe_ctx->link_res.hpo_frl_link_enc = pool->hpo_frl_link_enc[enc_index];
2850 
2851 	return pipe_ctx->link_res.hpo_frl_link_enc != NULL;
2852 }
2853 
2854 static void remove_hpo_frl_link_enc_from_ctx(struct resource_context *res_ctx,
2855 		struct pipe_ctx *pipe_ctx,
2856 		struct dc_stream_state *stream)
2857 {
2858 	int enc_index;
2859 
2860 	enc_index = find_acquired_hpo_frl_link_enc_for_link(res_ctx, stream->link);
2861 
2862 	if (enc_index >= 0) {
2863 		release_hpo_frl_link_enc(res_ctx, enc_index);
2864 		pipe_ctx->link_res.hpo_frl_link_enc = NULL;
2865 	}
2866 }
2867 
2868 static struct hpo_frl_link_encoder *get_temp_hpo_frl_link_enc(
2869 		const struct resource_context *res_ctx,
2870 		const struct resource_pool *const pool,
2871 		const struct dc_link *link)
2872 {
2873 	struct hpo_frl_link_encoder *hpo_frl_link_enc = NULL;
2874 	int enc_index;
2875 
2876 	enc_index = find_acquired_hpo_frl_link_enc_for_link(res_ctx, link);
2877 
2878 	if (enc_index < 0)
2879 		enc_index = find_free_hpo_frl_link_enc(res_ctx, pool);
2880 
2881 	if (enc_index >= 0)
2882 		hpo_frl_link_enc = pool->hpo_frl_link_enc[enc_index];
2883 
2884 	return hpo_frl_link_enc;
2885 }
2886 
2887 bool get_temp_frl_link_res(struct dc_link *link,
2888 		struct link_resource *link_res)
2889 {
2890 	const struct dc *dc  = link->dc;
2891 	const struct resource_context *res_ctx = &dc->current_state->res_ctx;
2892 
2893 	memset(link_res, 0, sizeof(*link_res));
2894 	link_res->hpo_frl_link_enc = get_temp_hpo_frl_link_enc(res_ctx, dc->res_pool, link);
2895 	if (!link_res->hpo_frl_link_enc)
2896 		return false;
2897 
2898 	link_res->dio_link_enc = get_temp_dio_link_enc(res_ctx,
2899 			dc->res_pool, link);
2900 	if (!link_res->dio_link_enc)
2901 		return false;
2902 
2903 	return true;
2904 }
2905 static void update_hpo_dp_stream_engine_usage(
2906 		struct resource_context *res_ctx,
2907 		const struct resource_pool *pool,
2908 		struct hpo_dp_stream_encoder *hpo_dp_stream_enc,
2909 		bool acquired)
2910 {
2911 	unsigned int i;
2912 
2913 	for (i = 0; i < pool->hpo_dp_stream_enc_count; i++) {
2914 		if (pool->hpo_dp_stream_enc[i] == hpo_dp_stream_enc)
2915 			res_ctx->is_hpo_dp_stream_enc_acquired[i] = acquired;
2916 	}
2917 }
2918 
2919 static inline int find_acquired_hpo_dp_link_enc_for_link(
2920 		const struct resource_context *res_ctx,
2921 		const struct dc_link *link)
2922 {
2923 	int i;
2924 
2925 	for (i = 0; i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_to_link_idx); i++)
2926 		if (res_ctx->hpo_dp_link_enc_ref_cnts[i] > 0 &&
2927 				res_ctx->hpo_dp_link_enc_to_link_idx[i] == link->link_index)
2928 			return i;
2929 
2930 	return -1;
2931 }
2932 
2933 static inline int find_free_hpo_dp_link_enc(const struct resource_context *res_ctx,
2934 		const struct resource_pool *pool)
2935 {
2936 	unsigned int i;
2937 
2938 	for (i = 0; i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_ref_cnts); i++)
2939 		if (res_ctx->hpo_dp_link_enc_ref_cnts[i] == 0)
2940 			break;
2941 
2942 	return (i < ARRAY_SIZE(res_ctx->hpo_dp_link_enc_ref_cnts) &&
2943 			i < pool->hpo_dp_link_enc_count) ? (int)i : -1;
2944 }
2945 
2946 static inline void acquire_hpo_dp_link_enc(
2947 		struct resource_context *res_ctx,
2948 		unsigned int link_index,
2949 		int enc_index)
2950 {
2951 	res_ctx->hpo_dp_link_enc_to_link_idx[enc_index] = link_index;
2952 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index] = 1;
2953 }
2954 
2955 static inline void retain_hpo_dp_link_enc(
2956 		struct resource_context *res_ctx,
2957 		int enc_index)
2958 {
2959 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index]++;
2960 }
2961 
2962 static inline void release_hpo_dp_link_enc(
2963 		struct resource_context *res_ctx,
2964 		int enc_index)
2965 {
2966 	ASSERT(res_ctx->hpo_dp_link_enc_ref_cnts[enc_index] > 0);
2967 	res_ctx->hpo_dp_link_enc_ref_cnts[enc_index]--;
2968 }
2969 
2970 static bool add_hpo_dp_link_enc_to_ctx(struct resource_context *res_ctx,
2971 		const struct resource_pool *pool,
2972 		struct pipe_ctx *pipe_ctx,
2973 		struct dc_stream_state *stream)
2974 {
2975 	int enc_index;
2976 
2977 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, stream->link);
2978 
2979 	if (enc_index >= 0) {
2980 		retain_hpo_dp_link_enc(res_ctx, enc_index);
2981 	} else {
2982 		enc_index = find_free_hpo_dp_link_enc(res_ctx, pool);
2983 		if (enc_index >= 0)
2984 			acquire_hpo_dp_link_enc(res_ctx, stream->link->link_index, enc_index);
2985 	}
2986 
2987 	if (enc_index >= 0)
2988 		pipe_ctx->link_res.hpo_dp_link_enc = pool->hpo_dp_link_enc[enc_index];
2989 
2990 	return pipe_ctx->link_res.hpo_dp_link_enc != NULL;
2991 }
2992 
2993 static void remove_hpo_dp_link_enc_from_ctx(struct resource_context *res_ctx,
2994 		struct pipe_ctx *pipe_ctx,
2995 		struct dc_stream_state *stream)
2996 {
2997 	int enc_index;
2998 
2999 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, stream->link);
3000 
3001 	if (enc_index >= 0) {
3002 		release_hpo_dp_link_enc(res_ctx, enc_index);
3003 		pipe_ctx->link_res.hpo_dp_link_enc = NULL;
3004 	}
3005 }
3006 
3007 static inline int find_acquired_dio_link_enc_for_link(
3008 		const struct resource_context *res_ctx,
3009 		const struct dc_link *link)
3010 {
3011 	int i;
3012 
3013 	for (i = 0; i < ARRAY_SIZE(res_ctx->dio_link_enc_ref_cnts); i++)
3014 		if (res_ctx->dio_link_enc_ref_cnts[i] > 0 &&
3015 				res_ctx->dio_link_enc_to_link_idx[i] == link->link_index)
3016 			return i;
3017 
3018 	return -1;
3019 }
3020 
3021 static inline int find_fixed_dio_link_enc(const struct dc_link *link)
3022 {
3023 	/* virtual links own their link encoder directly and are never
3024 	 * registered into pool->link_encoders[]. */
3025 	if (link->link_id.id == CONNECTOR_ID_VIRTUAL)
3026 		return -1;
3027 
3028 	/* the 8b10b dp phy can only use fixed link encoder */
3029 	return link->eng_id;
3030 }
3031 
3032 static inline int find_free_dio_link_enc(const struct resource_context *res_ctx,
3033 		const struct dc_link *link, const struct resource_pool *pool, struct dc_stream_state *stream)
3034 {
3035 	unsigned int i;
3036 	int j = -1;
3037 	int stream_enc_inst = -1;
3038 	unsigned int enc_count = pool->dig_link_enc_count;
3039 
3040 	/* Find stream encoder instance for the stream */
3041 	if (stream) {
3042 		for (i = 0; i < pool->pipe_count; i++) {
3043 			if ((res_ctx->pipe_ctx[i].stream == stream) &&
3044 				(res_ctx->pipe_ctx[i].stream_res.stream_enc != NULL)) {
3045 				stream_enc_inst = res_ctx->pipe_ctx[i].stream_res.stream_enc->id;
3046 				break;
3047 			}
3048 		}
3049 	}
3050 
3051 	/* Assign dpia preferred > stream enc instance > available */
3052 	for (i = 0; i < enc_count; i++) {
3053 		if (res_ctx->dio_link_enc_ref_cnts[i] == 0) {
3054 			if (j == -1)
3055 				j = i;
3056 
3057 			if (link->dpia_preferred_eng_id == i) {
3058 				j = i;
3059 				break;
3060 			}
3061 
3062 			if (stream_enc_inst == i) {
3063 				j = stream_enc_inst;
3064 			}
3065 		}
3066 	}
3067 	return j;
3068 }
3069 
3070 static inline void acquire_dio_link_enc(
3071 		struct resource_context *res_ctx,
3072 		unsigned int link_index,
3073 		int enc_index)
3074 {
3075 	res_ctx->dio_link_enc_to_link_idx[enc_index] = link_index;
3076 	res_ctx->dio_link_enc_ref_cnts[enc_index] = 1;
3077 }
3078 
3079 static inline void retain_dio_link_enc(
3080 		struct resource_context *res_ctx,
3081 		int enc_index)
3082 {
3083 	res_ctx->dio_link_enc_ref_cnts[enc_index]++;
3084 }
3085 
3086 static inline void release_dio_link_enc(
3087 		struct resource_context *res_ctx,
3088 		int enc_index)
3089 {
3090 	ASSERT(res_ctx->dio_link_enc_ref_cnts[enc_index] > 0);
3091 	res_ctx->dio_link_enc_ref_cnts[enc_index]--;
3092 }
3093 
3094 static bool is_dio_enc_acquired_by_other_link(const struct dc_link *link,
3095 		int enc_index,
3096 		int *link_index)
3097 {
3098 	const struct dc *dc  = link->dc;
3099 	const struct resource_context *res_ctx = &dc->current_state->res_ctx;
3100 
3101 	/* pass the link_index that acquired the enc_index */
3102 	if (res_ctx->dio_link_enc_ref_cnts[enc_index] > 0 &&
3103 			res_ctx->dio_link_enc_to_link_idx[enc_index] != link->link_index) {
3104 		*link_index = res_ctx->dio_link_enc_to_link_idx[enc_index];
3105 		return true;
3106 	}
3107 
3108 	return false;
3109 }
3110 
3111 static void swap_dio_link_enc_to_muxable_ctx(struct dc_state *context,
3112 		const struct resource_pool *pool,
3113 		int new_encoder,
3114 		int old_encoder)
3115 {
3116 	struct resource_context *res_ctx = &context->res_ctx;
3117 	int stream_count = context->stream_count;
3118 	int i = 0;
3119 
3120 	res_ctx->dio_link_enc_ref_cnts[new_encoder] = res_ctx->dio_link_enc_ref_cnts[old_encoder];
3121 	res_ctx->dio_link_enc_to_link_idx[new_encoder] = res_ctx->dio_link_enc_to_link_idx[old_encoder];
3122 	res_ctx->dio_link_enc_ref_cnts[old_encoder] = 0;
3123 
3124 	for (i = 0; i < stream_count; i++) {
3125 		struct dc_stream_state *stream = context->streams[i];
3126 		struct pipe_ctx *pipe_ctx = resource_get_otg_master_for_stream(&context->res_ctx, stream);
3127 
3128 		if (pipe_ctx && pipe_ctx->link_res.dio_link_enc == pool->link_encoders[old_encoder])
3129 			pipe_ctx->link_res.dio_link_enc = pool->link_encoders[new_encoder];
3130 	}
3131 }
3132 
3133 static bool add_dio_link_enc_to_ctx(const struct dc *dc,
3134 		struct dc_state *context,
3135 		const struct resource_pool *pool,
3136 		struct pipe_ctx *pipe_ctx,
3137 		struct dc_stream_state *stream)
3138 {
3139 	struct resource_context *res_ctx = &context->res_ctx;
3140 	int enc_index;
3141 
3142 	enc_index = find_acquired_dio_link_enc_for_link(res_ctx, stream->link);
3143 
3144 	if (enc_index >= 0) {
3145 		retain_dio_link_enc(res_ctx, enc_index);
3146 	} else {
3147 		if (stream->link->is_dig_mapping_flexible)
3148 			enc_index = find_free_dio_link_enc(res_ctx, stream->link, pool, stream);
3149 		else {
3150 			int link_index = 0;
3151 
3152 			enc_index = find_fixed_dio_link_enc(stream->link);
3153 			/* Fixed mapping link can only use its fixed link encoder.
3154 			 * If the encoder is acquired by other link then get a new free encoder and swap the new
3155 			 * one into the acquiring link.
3156 			 */
3157 			if (enc_index >= 0 && is_dio_enc_acquired_by_other_link(stream->link, enc_index, &link_index)) {
3158 				int new_enc_index = find_free_dio_link_enc(res_ctx, dc->links[link_index], pool, stream);
3159 
3160 				if (new_enc_index >= 0)
3161 					swap_dio_link_enc_to_muxable_ctx(context, pool, new_enc_index, enc_index);
3162 				else
3163 					return false;
3164 			}
3165 		}
3166 
3167 		if (enc_index >= 0)
3168 			acquire_dio_link_enc(res_ctx, stream->link->link_index, enc_index);
3169 	}
3170 
3171 	if (enc_index >= 0)
3172 		pipe_ctx->link_res.dio_link_enc = pool->link_encoders[enc_index];
3173 	else
3174 		pipe_ctx->link_res.dio_link_enc = stream->link->link_enc;
3175 
3176 	return pipe_ctx->link_res.dio_link_enc != NULL;
3177 }
3178 
3179 static void remove_dio_link_enc_from_ctx(struct resource_context *res_ctx,
3180 		struct pipe_ctx *pipe_ctx,
3181 		struct dc_stream_state *stream)
3182 {
3183 	int enc_index = -1;
3184 
3185 	if (stream->link)
3186 		enc_index = find_acquired_dio_link_enc_for_link(res_ctx, stream->link);
3187 
3188 	if (enc_index >= 0) {
3189 		release_dio_link_enc(res_ctx, enc_index);
3190 		pipe_ctx->link_res.dio_link_enc = NULL;
3191 	}
3192 }
3193 
3194 static int get_num_of_free_pipes(const struct resource_pool *pool, const struct dc_state *context)
3195 {
3196 	unsigned int i;
3197 	int count = 0;
3198 
3199 	for (i = 0; i < pool->pipe_count; i++)
3200 		if (resource_is_pipe_type(&context->res_ctx.pipe_ctx[i], FREE_PIPE))
3201 			count++;
3202 	return count;
3203 }
3204 
3205 enum dc_status resource_add_otg_master_for_stream_output(struct dc_state *new_ctx,
3206 		const struct resource_pool *pool,
3207 		struct dc_stream_state *stream)
3208 {
3209 	(void)pool;
3210 	struct dc *dc = stream->ctx->dc;
3211 
3212 	return dc->res_pool->funcs->add_stream_to_ctx(dc, new_ctx, stream);
3213 }
3214 
3215 void resource_remove_otg_master_for_stream_output(struct dc_state *context,
3216 		const struct resource_pool *pool,
3217 		struct dc_stream_state *stream)
3218 {
3219 	struct pipe_ctx *otg_master = resource_get_otg_master_for_stream(
3220 			&context->res_ctx, stream);
3221 
3222 	if (!otg_master)
3223 		return;
3224 
3225 	ASSERT(resource_get_odm_slice_count(otg_master) == 1);
3226 	ASSERT(otg_master->plane_state == NULL);
3227 	ASSERT(otg_master->stream_res.stream_enc);
3228 	update_stream_engine_usage(
3229 			&context->res_ctx,
3230 			pool,
3231 			otg_master->stream_res.stream_enc,
3232 			false);
3233 
3234 	if (dc_is_hdmi_frl_signal(stream->signal)) {
3235 		update_hpo_frl_stream_engine_usage(
3236 			&context->res_ctx, pool,
3237 			otg_master->stream_res.hpo_frl_stream_enc,
3238 			false);
3239 		remove_hpo_frl_link_enc_from_ctx(
3240 			&context->res_ctx, otg_master, stream);
3241 		remove_dio_link_enc_from_ctx(&context->res_ctx, otg_master, stream);
3242 	}
3243 	if (stream->ctx->dc->link_srv->dp_is_128b_132b_signal(otg_master)) {
3244 		update_hpo_dp_stream_engine_usage(
3245 			&context->res_ctx, pool,
3246 			otg_master->stream_res.hpo_dp_stream_enc,
3247 			false);
3248 		remove_hpo_dp_link_enc_from_ctx(
3249 				&context->res_ctx, otg_master, stream);
3250 	}
3251 
3252 	if (stream->ctx->dc->config.unify_link_enc_assignment)
3253 		remove_dio_link_enc_from_ctx(&context->res_ctx, otg_master, stream);
3254 
3255 	if (otg_master->stream_res.audio)
3256 		update_audio_usage(
3257 			&context->res_ctx,
3258 			pool,
3259 			otg_master->stream_res.audio,
3260 			false);
3261 
3262 	resource_unreference_clock_source(&context->res_ctx,
3263 					  pool,
3264 					  otg_master->clock_source);
3265 
3266 	if (pool->funcs->remove_stream_from_ctx)
3267 		pool->funcs->remove_stream_from_ctx(
3268 				stream->ctx->dc, context, stream);
3269 
3270 	memset(otg_master, 0, sizeof(*otg_master));
3271 }
3272 
3273 /* For each OPP head of an OTG master, add top plane at plane index 0.
3274  *
3275  * In the following example, the stream has 2 ODM slices without a top plane.
3276  * By adding a plane 0 to OPP heads, we are configuring our hardware to render
3277  * plane 0 by using each OPP head's DPP.
3278  *
3279  *       Inter-pipe Relation (Before Adding Plane)
3280  *        __________________________________________________
3281  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3282  *       |        |               | slice 0   |             |
3283  *       |   0    |               |blank ----ODM----------- |
3284  *       |        |               | slice 1 | |             |
3285  *       |   1    |               |blank ---- |             |
3286  *       |________|_______________|___________|_____________|
3287  *
3288  *       Inter-pipe Relation (After Adding Plane)
3289  *        __________________________________________________
3290  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3291  *       |        |  plane 0      | slice 0   |             |
3292  *       |   0    | -------------------------ODM----------- |
3293  *       |        |  plane 0      | slice 1 | |             |
3294  *       |   1    | ------------------------- |             |
3295  *       |________|_______________|___________|_____________|
3296  */
3297 static bool add_plane_to_opp_head_pipes(struct pipe_ctx *otg_master_pipe,
3298 		struct dc_plane_state *plane_state,
3299 		struct dc_state *context)
3300 {
3301 	(void)context;
3302 	struct pipe_ctx *opp_head_pipe = otg_master_pipe;
3303 
3304 	while (opp_head_pipe) {
3305 		if (opp_head_pipe->plane_state) {
3306 			ASSERT(0);
3307 			return false;
3308 		}
3309 		opp_head_pipe->plane_state = plane_state;
3310 		opp_head_pipe = opp_head_pipe->next_odm_pipe;
3311 	}
3312 
3313 	return true;
3314 }
3315 
3316 /* For each OPP head of an OTG master, acquire a secondary DPP pipe and add
3317  * the plane. So the plane is added to all ODM slices associated with the OTG
3318  * master pipe in the bottom layer.
3319  *
3320  * In the following example, the stream has 2 ODM slices and a top plane 0.
3321  * By acquiring secondary DPP pipes and adding a plane 1, we are configuring our
3322  * hardware to render the plane 1 by acquiring a new pipe for each ODM slice and
3323  * render plane 1 using new pipes' DPP in the Z axis below plane 0.
3324  *
3325  *       Inter-pipe Relation (Before Adding Plane)
3326  *        __________________________________________________
3327  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3328  *       |        |  plane 0      | slice 0   |             |
3329  *       |   0    | -------------------------ODM----------- |
3330  *       |        |  plane 0      | slice 1 | |             |
3331  *       |   1    | ------------------------- |             |
3332  *       |________|_______________|___________|_____________|
3333  *
3334  *       Inter-pipe Relation (After Acquiring and Adding Plane)
3335  *        __________________________________________________
3336  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3337  *       |        |  plane 0      | slice 0   |             |
3338  *       |   0    | -------------MPC---------ODM----------- |
3339  *       |        |  plane 1    | |         | |             |
3340  *       |   2    | ------------- |         | |             |
3341  *       |        |  plane 0      | slice 1 | |             |
3342  *       |   1    | -------------MPC--------- |             |
3343  *       |        |  plane 1    | |           |             |
3344  *       |   3    | ------------- |           |             |
3345  *       |________|_______________|___________|_____________|
3346  */
3347 static bool acquire_secondary_dpp_pipes_and_add_plane(
3348 		struct pipe_ctx *otg_master_pipe,
3349 		struct dc_plane_state *plane_state,
3350 		struct dc_state *new_ctx,
3351 		struct dc_state *cur_ctx,
3352 		struct resource_pool *pool)
3353 {
3354 	struct pipe_ctx *sec_pipe, *tail_pipe;
3355 	struct pipe_ctx *opp_heads[MAX_PIPES];
3356 	int opp_head_count;
3357 	int i;
3358 
3359 	if (!pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe) {
3360 		ASSERT(0);
3361 		return false;
3362 	}
3363 
3364 	opp_head_count = resource_get_opp_heads_for_otg_master(otg_master_pipe,
3365 			&new_ctx->res_ctx, opp_heads);
3366 	if (get_num_of_free_pipes(pool, new_ctx) < opp_head_count)
3367 		/* not enough free pipes */
3368 		return false;
3369 
3370 	for (i = 0; i < opp_head_count; i++) {
3371 		sec_pipe = pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe(
3372 				cur_ctx,
3373 				new_ctx,
3374 				pool,
3375 				opp_heads[i]);
3376 		ASSERT(sec_pipe);
3377 		sec_pipe->plane_state = plane_state;
3378 
3379 		/* establish pipe relationship */
3380 		tail_pipe = get_tail_pipe(opp_heads[i]);
3381 		tail_pipe->bottom_pipe = sec_pipe;
3382 		sec_pipe->top_pipe = tail_pipe;
3383 		sec_pipe->bottom_pipe = NULL;
3384 		if (tail_pipe->prev_odm_pipe) {
3385 			ASSERT(tail_pipe->prev_odm_pipe->bottom_pipe);
3386 			sec_pipe->prev_odm_pipe = tail_pipe->prev_odm_pipe->bottom_pipe;
3387 			tail_pipe->prev_odm_pipe->bottom_pipe->next_odm_pipe = sec_pipe;
3388 		} else {
3389 			sec_pipe->prev_odm_pipe = NULL;
3390 		}
3391 	}
3392 	return true;
3393 }
3394 
3395 /*
3396  * resource_assign_rmcm() - Assign the RMCM instances for a whole context
3397  *
3398  * RMCM is pre-blend, so every pipe asking for one needs its own instance. Ownership is
3399  * tracked per instance in res_ctx->rmcm_in_use[]; the owning pipe is the one holding the
3400  * matching plane_res.rmcm pointer.
3401  *
3402  * A pipe that already drives an RMCM keeps it, so a plane that is only now asking never
3403  * evicts one that is already using an instance - it simply gets none when the pool is
3404  * exhausted. This has to see the finished context, hence one pass over all pipes rather
3405  * than one per appended plane.
3406  */
3407 void resource_assign_rmcm(
3408 		struct dc_state *new_ctx,
3409 		const struct dc_state *cur_ctx,
3410 		const struct resource_pool *pool)
3411 {
3412 	struct rmcm *held[MAX_PIPES] = { NULL };
3413 	unsigned int p;
3414 	int i;
3415 
3416 	/* new_ctx and cur_ctx can be the same object, so snapshot before clearing */
3417 	for (p = 0; p < pool->pipe_count && p < MAX_PIPES; p++)
3418 		held[p] = cur_ctx ? cur_ctx->res_ctx.pipe_ctx[p].plane_res.rmcm : NULL;
3419 
3420 	for (p = 0; p < pool->pipe_count && p < MAX_PIPES; p++)
3421 		new_ctx->res_ctx.pipe_ctx[p].plane_res.rmcm = NULL;
3422 
3423 	for (i = 0; i < MAX_RMCM_INST; i++)
3424 		new_ctx->res_ctx.rmcm_in_use[i] = false;
3425 
3426 	/* incumbents first, so a later pipe cannot take an instance out from under them */
3427 	for (p = 0; p < pool->pipe_count && p < MAX_PIPES; p++) {
3428 		struct pipe_ctx *pipe = &new_ctx->res_ctx.pipe_ctx[p];
3429 
3430 		if (!pipe->plane_state || !pipe->plane_state->cm.flags.bits.rmcm_enable)
3431 			continue;
3432 
3433 		if (!held[p] || new_ctx->res_ctx.rmcm_in_use[held[p]->inst])
3434 			continue;
3435 
3436 		pipe->plane_res.rmcm = held[p];
3437 		new_ctx->res_ctx.rmcm_in_use[held[p]->inst] = true;
3438 	}
3439 
3440 	for (p = 0; p < pool->pipe_count && p < MAX_PIPES; p++) {
3441 		struct pipe_ctx *pipe = &new_ctx->res_ctx.pipe_ctx[p];
3442 
3443 		if (!pipe->plane_state || !pipe->plane_state->cm.flags.bits.rmcm_enable)
3444 			continue;
3445 
3446 		if (pipe->plane_res.rmcm)
3447 			continue;
3448 
3449 		for (i = 0; i < pool->res_cap->num_rmcm && i < MAX_RMCM_INST; i++) {
3450 			if (!pool->rmcm[i] || new_ctx->res_ctx.rmcm_in_use[i])
3451 				continue;
3452 
3453 			pipe->plane_res.rmcm = pool->rmcm[i];
3454 			new_ctx->res_ctx.rmcm_in_use[i] = true;
3455 			break;
3456 		}
3457 	}
3458 }
3459 
3460 /*
3461  * resource_release_rmcm() - Release an RMCM instance for a pipe.
3462  *
3463  * Marks the instance free and NULLs the caller's pointer.
3464  */
3465 void resource_release_rmcm(
3466 		struct resource_context *res_ctx,
3467 		const struct resource_pool *pool,
3468 		struct rmcm **rmcm)
3469 {
3470 	int i;
3471 
3472 	if (!*rmcm)
3473 		return;
3474 
3475 	for (i = 0; i < pool->res_cap->num_rmcm && i < MAX_RMCM_INST; i++) {
3476 		if (pool->rmcm[i] == *rmcm) {
3477 			res_ctx->rmcm_in_use[i] = false;
3478 			*rmcm = NULL;
3479 			return;
3480 		}
3481 	}
3482 }
3483 
3484 bool resource_append_dpp_pipes_for_plane_composition(
3485 		struct dc_state *new_ctx,
3486 		struct dc_state *cur_ctx,
3487 		struct resource_pool *pool,
3488 		struct pipe_ctx *otg_master_pipe,
3489 		struct dc_plane_state *plane_state)
3490 {
3491 	bool success;
3492 
3493 	if (otg_master_pipe->plane_state == NULL)
3494 		success = add_plane_to_opp_head_pipes(otg_master_pipe,
3495 				plane_state, new_ctx);
3496 	else
3497 		success = acquire_secondary_dpp_pipes_and_add_plane(
3498 				otg_master_pipe, plane_state, new_ctx,
3499 				cur_ctx, pool);
3500 	if (success) {
3501 		/* when appending a plane mpc slice count changes from 0 to 1 */
3502 		success = update_pipe_params_after_mpc_slice_count_change(
3503 				plane_state, new_ctx, pool);
3504 		if (!success)
3505 			resource_remove_dpp_pipes_for_plane_composition(new_ctx,
3506 					pool, plane_state);
3507 	}
3508 
3509 	/* Idempotent, so the last append of a commit leaves the final assignment behind.
3510 	 * Releasing is driven by the MPC_RMCM_CNTL mirror (see dcn42_release_rmcm_from_mpcc).
3511 	 */
3512 	if (success)
3513 		resource_assign_rmcm(new_ctx, cur_ctx, pool);
3514 
3515 	return success;
3516 }
3517 
3518 void resource_remove_dpp_pipes_for_plane_composition(
3519 		struct dc_state *context,
3520 		const struct resource_pool *pool,
3521 		const struct dc_plane_state *plane_state)
3522 {
3523 	int i;
3524 
3525 	for (i = pool->pipe_count - 1; i >= 0; i--) {
3526 		struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
3527 
3528 		if (pipe_ctx->plane_state == plane_state) {
3529 			/* Release RMCM instance for this pipe */
3530 			if (pipe_ctx->plane_res.rmcm)
3531 				resource_release_rmcm(&context->res_ctx,
3532 						pool,
3533 						&pipe_ctx->plane_res.rmcm);
3534 
3535 			if (pipe_ctx->top_pipe)
3536 				pipe_ctx->top_pipe->bottom_pipe = pipe_ctx->bottom_pipe;
3537 
3538 			/* Second condition is to avoid setting NULL to top pipe
3539 			 * of tail pipe making it look like head pipe in subsequent
3540 			 * deletes
3541 			 */
3542 			if (pipe_ctx->bottom_pipe && pipe_ctx->top_pipe)
3543 				pipe_ctx->bottom_pipe->top_pipe = pipe_ctx->top_pipe;
3544 
3545 			/*
3546 			 * For head pipe detach surfaces from pipe for tail
3547 			 * pipe just zero it out
3548 			 */
3549 			if (!pipe_ctx->top_pipe)
3550 				pipe_ctx->plane_state = NULL;
3551 			else
3552 				memset(pipe_ctx, 0, sizeof(*pipe_ctx));
3553 		}
3554 	}
3555 }
3556 
3557 /*
3558  * Increase ODM slice count by 1 by acquiring pipes and adding a new ODM slice
3559  * at the last index.
3560  * return - true if a new ODM slice is added and required pipes are acquired.
3561  * false if new_ctx is no longer a valid state after new ODM slice is added.
3562  *
3563  * This is achieved by duplicating MPC blending tree from previous ODM slice.
3564  * In the following example, we have a single MPC tree and 1 ODM slice 0. We
3565  * want to add a new odm slice by duplicating the MPC blending tree and add
3566  * ODM slice 1.
3567  *
3568  *       Inter-pipe Relation (Before Acquiring and Adding ODM Slice)
3569  *        __________________________________________________
3570  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3571  *       |        |  plane 0      | slice 0   |             |
3572  *       |   0    | -------------MPC---------ODM----------- |
3573  *       |        |  plane 1    | |           |             |
3574  *       |   1    | ------------- |           |             |
3575  *       |________|_______________|___________|_____________|
3576  *
3577  *       Inter-pipe Relation (After Acquiring and Adding ODM Slice)
3578  *        __________________________________________________
3579  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3580  *       |        |  plane 0      | slice 0   |             |
3581  *       |   0    | -------------MPC---------ODM----------- |
3582  *       |        |  plane 1    | |         | |             |
3583  *       |   1    | ------------- |         | |             |
3584  *       |        |  plane 0      | slice 1 | |             |
3585  *       |   2    | -------------MPC--------- |             |
3586  *       |        |  plane 1    | |           |             |
3587  *       |   3    | ------------- |           |             |
3588  *       |________|_______________|___________|_____________|
3589  */
3590 static bool acquire_pipes_and_add_odm_slice(
3591 		struct pipe_ctx *otg_master_pipe,
3592 		struct dc_state *new_ctx,
3593 		const struct dc_state *cur_ctx,
3594 		const struct resource_pool *pool)
3595 {
3596 	struct pipe_ctx *last_opp_head = get_last_opp_head(otg_master_pipe);
3597 	struct pipe_ctx *new_opp_head;
3598 	struct pipe_ctx *last_top_dpp_pipe, *last_bottom_dpp_pipe,
3599 			*new_top_dpp_pipe, *new_bottom_dpp_pipe;
3600 
3601 	if (!pool->funcs->acquire_free_pipe_as_secondary_opp_head) {
3602 		ASSERT(0);
3603 		return false;
3604 	}
3605 	new_opp_head = pool->funcs->acquire_free_pipe_as_secondary_opp_head(
3606 					cur_ctx, new_ctx, pool,
3607 					otg_master_pipe);
3608 	if (!new_opp_head)
3609 		return false;
3610 
3611 	last_opp_head->next_odm_pipe = new_opp_head;
3612 	new_opp_head->prev_odm_pipe = last_opp_head;
3613 	new_opp_head->next_odm_pipe = NULL;
3614 	new_opp_head->plane_state = last_opp_head->plane_state;
3615 	last_top_dpp_pipe = last_opp_head;
3616 	new_top_dpp_pipe = new_opp_head;
3617 
3618 	while (last_top_dpp_pipe->bottom_pipe) {
3619 		last_bottom_dpp_pipe = last_top_dpp_pipe->bottom_pipe;
3620 		new_bottom_dpp_pipe = pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe(
3621 				cur_ctx, new_ctx, pool,
3622 				new_opp_head);
3623 		if (!new_bottom_dpp_pipe)
3624 			return false;
3625 
3626 		new_bottom_dpp_pipe->plane_state = last_bottom_dpp_pipe->plane_state;
3627 		new_top_dpp_pipe->bottom_pipe = new_bottom_dpp_pipe;
3628 		new_bottom_dpp_pipe->top_pipe = new_top_dpp_pipe;
3629 		last_bottom_dpp_pipe->next_odm_pipe = new_bottom_dpp_pipe;
3630 		new_bottom_dpp_pipe->prev_odm_pipe = last_bottom_dpp_pipe;
3631 		new_bottom_dpp_pipe->next_odm_pipe = NULL;
3632 		last_top_dpp_pipe = last_bottom_dpp_pipe;
3633 	}
3634 
3635 	return true;
3636 }
3637 
3638 /*
3639  * Decrease ODM slice count by 1 by releasing pipes and removing the ODM slice
3640  * at the last index.
3641  * return - true if the last ODM slice is removed and related pipes are
3642  * released. false if there is no removable ODM slice.
3643  *
3644  * In the following example, we have 2 MPC trees and ODM slice 0 and slice 1.
3645  * We want to remove the last ODM i.e slice 1. We are releasing secondary DPP
3646  * pipe 3 and OPP head pipe 2.
3647  *
3648  *       Inter-pipe Relation (Before Releasing and Removing ODM Slice)
3649  *        __________________________________________________
3650  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3651  *       |        |  plane 0      | slice 0   |             |
3652  *       |   0    | -------------MPC---------ODM----------- |
3653  *       |        |  plane 1    | |         | |             |
3654  *       |   1    | ------------- |         | |             |
3655  *       |        |  plane 0      | slice 1 | |             |
3656  *       |   2    | -------------MPC--------- |             |
3657  *       |        |  plane 1    | |           |             |
3658  *       |   3    | ------------- |           |             |
3659  *       |________|_______________|___________|_____________|
3660  *
3661  *       Inter-pipe Relation (After Releasing and Removing ODM Slice)
3662  *        __________________________________________________
3663  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3664  *       |        |  plane 0      | slice 0   |             |
3665  *       |   0    | -------------MPC---------ODM----------- |
3666  *       |        |  plane 1    | |           |             |
3667  *       |   1    | ------------- |           |             |
3668  *       |________|_______________|___________|_____________|
3669  */
3670 static bool release_pipes_and_remove_odm_slice(
3671 		struct pipe_ctx *otg_master_pipe,
3672 		struct dc_state *context,
3673 		const struct resource_pool *pool)
3674 {
3675 	struct pipe_ctx *last_opp_head = get_last_opp_head(otg_master_pipe);
3676 	struct pipe_ctx *tail_pipe = get_tail_pipe(last_opp_head);
3677 
3678 	if (!pool->funcs->release_pipe) {
3679 		ASSERT(0);
3680 		return false;
3681 	}
3682 
3683 	if (resource_is_pipe_type(last_opp_head, OTG_MASTER))
3684 		return false;
3685 
3686 	while (tail_pipe->top_pipe) {
3687 		tail_pipe->prev_odm_pipe->next_odm_pipe = NULL;
3688 		tail_pipe = tail_pipe->top_pipe;
3689 		pool->funcs->release_pipe(context, tail_pipe->bottom_pipe, pool);
3690 		tail_pipe->bottom_pipe = NULL;
3691 	}
3692 	last_opp_head->prev_odm_pipe->next_odm_pipe = NULL;
3693 	pool->funcs->release_pipe(context, last_opp_head, pool);
3694 
3695 	return true;
3696 }
3697 
3698 /*
3699  * Increase MPC slice count by 1 by acquiring a new DPP pipe and add it as the
3700  * last MPC slice of the plane associated with dpp_pipe.
3701  *
3702  * return - true if a new MPC slice is added and required pipes are acquired.
3703  * false if new_ctx is no longer a valid state after new MPC slice is added.
3704  *
3705  * In the following example, we add a new MPC slice for plane 0 into the
3706  * new_ctx. To do so we pass pipe 0 as dpp_pipe. The function acquires a new DPP
3707  * pipe 2 for plane 0 as the bottom most pipe for plane 0.
3708  *
3709  *       Inter-pipe Relation (Before Acquiring and Adding MPC Slice)
3710  *        __________________________________________________
3711  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3712  *       |        |  plane 0      |           |             |
3713  *       |   0    | -------------MPC----------------------- |
3714  *       |        |  plane 1    | |           |             |
3715  *       |   1    | ------------- |           |             |
3716  *       |________|_______________|___________|_____________|
3717  *
3718  *       Inter-pipe Relation (After Acquiring and Adding MPC Slice)
3719  *        __________________________________________________
3720  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3721  *       |        |  plane 0      |           |             |
3722  *       |   0    | -------------MPC----------------------- |
3723  *       |        |  plane 0    | |           |             |
3724  *       |   2    | ------------- |           |             |
3725  *       |        |  plane 1    | |           |             |
3726  *       |   1    | ------------- |           |             |
3727  *       |________|_______________|___________|_____________|
3728  */
3729 static bool acquire_dpp_pipe_and_add_mpc_slice(
3730 		struct pipe_ctx *dpp_pipe,
3731 		struct dc_state *new_ctx,
3732 		const struct dc_state *cur_ctx,
3733 		const struct resource_pool *pool)
3734 {
3735 	struct pipe_ctx *last_dpp_pipe =
3736 			get_last_dpp_pipe_in_mpcc_combine(dpp_pipe);
3737 	struct pipe_ctx *opp_head = resource_get_opp_head(dpp_pipe);
3738 	struct pipe_ctx *new_dpp_pipe;
3739 
3740 	if (!pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe) {
3741 		ASSERT(0);
3742 		return false;
3743 	}
3744 	new_dpp_pipe = pool->funcs->acquire_free_pipe_as_secondary_dpp_pipe(
3745 			cur_ctx, new_ctx, pool, opp_head);
3746 	if (!new_dpp_pipe || resource_get_odm_slice_count(dpp_pipe) > 1)
3747 		return false;
3748 
3749 	new_dpp_pipe->bottom_pipe = last_dpp_pipe->bottom_pipe;
3750 	if (new_dpp_pipe->bottom_pipe)
3751 		new_dpp_pipe->bottom_pipe->top_pipe = new_dpp_pipe;
3752 	new_dpp_pipe->top_pipe = last_dpp_pipe;
3753 	last_dpp_pipe->bottom_pipe = new_dpp_pipe;
3754 	new_dpp_pipe->plane_state = last_dpp_pipe->plane_state;
3755 
3756 	return true;
3757 }
3758 
3759 /*
3760  * Reduce MPC slice count by 1 by releasing the bottom DPP pipe in MPCC combine
3761  * with dpp_pipe and removing last MPC slice of the plane associated with
3762  * dpp_pipe.
3763  *
3764  * return - true if the last MPC slice of the plane associated with dpp_pipe is
3765  * removed and last DPP pipe in MPCC combine with dpp_pipe is released.
3766  * false if there is no removable MPC slice.
3767  *
3768  * In the following example, we remove an MPC slice for plane 0 from the
3769  * context. To do so we pass pipe 0 as dpp_pipe. The function releases pipe 1 as
3770  * it is the last pipe for plane 0.
3771  *
3772  *       Inter-pipe Relation (Before Releasing and Removing MPC Slice)
3773  *        __________________________________________________
3774  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3775  *       |        |  plane 0      |           |             |
3776  *       |   0    | -------------MPC----------------------- |
3777  *       |        |  plane 0    | |           |             |
3778  *       |   1    | ------------- |           |             |
3779  *       |        |  plane 1    | |           |             |
3780  *       |   2    | ------------- |           |             |
3781  *       |________|_______________|___________|_____________|
3782  *
3783  *       Inter-pipe Relation (After Releasing and Removing MPC Slice)
3784  *        __________________________________________________
3785  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3786  *       |        |  plane 0      |           |             |
3787  *       |   0    | -------------MPC----------------------- |
3788  *       |        |  plane 1    | |           |             |
3789  *       |   2    | ------------- |           |             |
3790  *       |________|_______________|___________|_____________|
3791  */
3792 static bool release_dpp_pipe_and_remove_mpc_slice(
3793 		struct pipe_ctx *dpp_pipe,
3794 		struct dc_state *context,
3795 		const struct resource_pool *pool)
3796 {
3797 	struct pipe_ctx *last_dpp_pipe =
3798 			get_last_dpp_pipe_in_mpcc_combine(dpp_pipe);
3799 
3800 	if (!pool->funcs->release_pipe) {
3801 		ASSERT(0);
3802 		return false;
3803 	}
3804 
3805 	if (resource_is_pipe_type(last_dpp_pipe, OPP_HEAD) ||
3806 			resource_get_odm_slice_count(dpp_pipe) > 1)
3807 		return false;
3808 
3809 	last_dpp_pipe->top_pipe->bottom_pipe = last_dpp_pipe->bottom_pipe;
3810 	if (last_dpp_pipe->bottom_pipe)
3811 		last_dpp_pipe->bottom_pipe->top_pipe = last_dpp_pipe->top_pipe;
3812 	pool->funcs->release_pipe(context, last_dpp_pipe, pool);
3813 
3814 	return true;
3815 }
3816 
3817 bool resource_update_pipes_for_stream_with_slice_count(
3818 		struct dc_state *new_ctx,
3819 		const struct dc_state *cur_ctx,
3820 		const struct resource_pool *pool,
3821 		const struct dc_stream_state *stream,
3822 		int new_slice_count)
3823 {
3824 	int i;
3825 	struct pipe_ctx *otg_master = resource_get_otg_master_for_stream(
3826 			&new_ctx->res_ctx, stream);
3827 	int cur_slice_count;
3828 	bool result = true;
3829 
3830 	if (!otg_master)
3831 		return false;
3832 
3833 	cur_slice_count = resource_get_odm_slice_count(otg_master);
3834 
3835 	if (new_slice_count == cur_slice_count)
3836 		return result;
3837 
3838 	if (new_slice_count > cur_slice_count)
3839 		for (i = 0; i < new_slice_count - cur_slice_count && result; i++)
3840 			result = acquire_pipes_and_add_odm_slice(
3841 					otg_master, new_ctx, cur_ctx, pool);
3842 	else
3843 		for (i = 0; i < cur_slice_count - new_slice_count && result; i++)
3844 			result = release_pipes_and_remove_odm_slice(
3845 					otg_master, new_ctx, pool);
3846 	if (result)
3847 		result = update_pipe_params_after_odm_slice_count_change(
3848 				otg_master, new_ctx, pool);
3849 	return result;
3850 }
3851 
3852 bool resource_update_pipes_for_plane_with_slice_count(
3853 		struct dc_state *new_ctx,
3854 		const struct dc_state *cur_ctx,
3855 		const struct resource_pool *pool,
3856 		const struct dc_plane_state *plane,
3857 		int new_slice_count)
3858 {
3859 	int i;
3860 	int dpp_pipe_count;
3861 	int cur_slice_count;
3862 	struct pipe_ctx *dpp_pipes[MAX_PIPES] = {0};
3863 	bool result = true;
3864 
3865 	dpp_pipe_count = resource_get_dpp_pipes_for_plane(plane,
3866 			&new_ctx->res_ctx, dpp_pipes);
3867 	ASSERT(dpp_pipe_count > 0);
3868 	cur_slice_count = resource_get_mpc_slice_count(dpp_pipes[0]);
3869 
3870 	if (new_slice_count == cur_slice_count)
3871 		return result;
3872 
3873 	if (new_slice_count > cur_slice_count)
3874 		for (i = 0; i < new_slice_count - cur_slice_count && result; i++)
3875 			result = acquire_dpp_pipe_and_add_mpc_slice(
3876 					dpp_pipes[0], new_ctx, cur_ctx, pool);
3877 	else
3878 		for (i = 0; i < cur_slice_count - new_slice_count && result; i++)
3879 			result = release_dpp_pipe_and_remove_mpc_slice(
3880 					dpp_pipes[0], new_ctx, pool);
3881 	if (result)
3882 		result = update_pipe_params_after_mpc_slice_count_change(
3883 				dpp_pipes[0]->plane_state, new_ctx, pool);
3884 	return result;
3885 }
3886 
3887 bool dc_is_timing_changed(struct dc_stream_state *cur_stream,
3888 		       struct dc_stream_state *new_stream)
3889 {
3890 	if (cur_stream == NULL)
3891 		return true;
3892 
3893 	/* If output color space is changed, need to reprogram info frames */
3894 	if (cur_stream->output_color_space != new_stream->output_color_space)
3895 		return true;
3896 
3897 	return memcmp(
3898 		&cur_stream->timing,
3899 		&new_stream->timing,
3900 		sizeof(struct dc_crtc_timing)) != 0;
3901 }
3902 
3903 static bool are_stream_backends_same(
3904 	struct dc_stream_state *stream_a, struct dc_stream_state *stream_b)
3905 {
3906 	if (stream_a == stream_b)
3907 		return true;
3908 
3909 	if (stream_a == NULL || stream_b == NULL)
3910 		return false;
3911 
3912 	if (dc_is_timing_changed(stream_a, stream_b))
3913 		return false;
3914 
3915 	if (stream_a->signal != stream_b->signal)
3916 		return false;
3917 
3918 	if (stream_a->dpms_off != stream_b->dpms_off)
3919 		return false;
3920 
3921 	return true;
3922 }
3923 
3924 /*
3925  * dc_is_stream_unchanged() - Compare two stream states for equivalence.
3926  *
3927  * Checks if there a difference between the two states
3928  * that would require a mode change.
3929  *
3930  * Does not compare cursor position or attributes.
3931  */
3932 bool dc_is_stream_unchanged(
3933 	struct dc_stream_state *old_stream, struct dc_stream_state *stream)
3934 {
3935 	if (!old_stream || !stream)
3936 		return false;
3937 
3938 	if (!are_stream_backends_same(old_stream, stream))
3939 		return false;
3940 
3941 	if (old_stream->ignore_msa_timing_param != stream->ignore_msa_timing_param)
3942 		return false;
3943 
3944 	/*compare audio info*/
3945 	if (memcmp(&old_stream->audio_info, &stream->audio_info, sizeof(stream->audio_info)) != 0)
3946 		return false;
3947 
3948 	return true;
3949 }
3950 
3951 /*
3952  * dc_is_stream_scaling_unchanged() - Compare scaling rectangles of two streams.
3953  */
3954 bool dc_is_stream_scaling_unchanged(struct dc_stream_state *old_stream,
3955 				    struct dc_stream_state *stream)
3956 {
3957 	if (old_stream == stream)
3958 		return true;
3959 
3960 	if (old_stream == NULL || stream == NULL)
3961 		return false;
3962 
3963 	if (memcmp(&old_stream->src,
3964 			&stream->src,
3965 			sizeof(struct rect)) != 0)
3966 		return false;
3967 
3968 	if (memcmp(&old_stream->dst,
3969 			&stream->dst,
3970 			sizeof(struct rect)) != 0)
3971 		return false;
3972 
3973 	return true;
3974 }
3975 
3976 /* TODO: release audio object */
3977 void update_audio_usage(
3978 		struct resource_context *res_ctx,
3979 		const struct resource_pool *pool,
3980 		struct audio *audio,
3981 		bool acquired)
3982 {
3983 	unsigned int i;
3984 	for (i = 0; i < pool->audio_count; i++) {
3985 		if (pool->audios[i] == audio)
3986 			res_ctx->is_audio_acquired[i] = acquired;
3987 	}
3988 }
3989 
3990 static struct hpo_dp_stream_encoder *find_first_free_match_hpo_dp_stream_enc_for_link(
3991 		struct resource_context *res_ctx,
3992 		const struct resource_pool *pool,
3993 		struct dc_stream_state *stream)
3994 {
3995 	(void)stream;
3996 	unsigned int i;
3997 
3998 	for (i = 0; i < pool->hpo_dp_stream_enc_count; i++) {
3999 		if (!res_ctx->is_hpo_dp_stream_enc_acquired[i] &&
4000 				pool->hpo_dp_stream_enc[i]) {
4001 
4002 			return pool->hpo_dp_stream_enc[i];
4003 		}
4004 	}
4005 
4006 	return NULL;
4007 }
4008 
4009 static struct audio *find_first_free_audio(
4010 		struct resource_context *res_ctx,
4011 		const struct resource_pool *pool,
4012 		enum engine_id id,
4013 		enum dce_version dc_version)
4014 {
4015 	(void)dc_version;
4016 	int i, available_audio_count;
4017 
4018 	if (id == ENGINE_ID_UNKNOWN)
4019 		return NULL;
4020 
4021 	available_audio_count = pool->audio_count;
4022 
4023 	for (i = 0; i < available_audio_count; i++) {
4024 		if ((res_ctx->is_audio_acquired[i] == false) && (res_ctx->is_stream_enc_acquired[i] == true)) {
4025 			/*we have enough audio endpoint, find the matching inst*/
4026 			if (id != i)
4027 				continue;
4028 			return pool->audios[i];
4029 		}
4030 	}
4031 
4032 	/* use engine id to find free audio */
4033 	if ((id < available_audio_count) && (res_ctx->is_audio_acquired[id] == false)) {
4034 		return pool->audios[id];
4035 	}
4036 	/*not found the matching one, first come first serve*/
4037 	for (i = 0; i < available_audio_count; i++) {
4038 		if (res_ctx->is_audio_acquired[i] == false) {
4039 			return pool->audios[i];
4040 		}
4041 	}
4042 	return NULL;
4043 }
4044 
4045 static struct dc_stream_state *find_pll_sharable_stream(
4046 		struct dc_stream_state *stream_needs_pll,
4047 		struct dc_state *context)
4048 {
4049 	int i;
4050 
4051 	for (i = 0; i < context->stream_count; i++) {
4052 		struct dc_stream_state *stream_has_pll = context->streams[i];
4053 
4054 		/* We are looking for non dp, non virtual stream */
4055 		if (resource_are_streams_timing_synchronizable(
4056 			stream_needs_pll, stream_has_pll)
4057 			&& !dc_is_dp_signal(stream_has_pll->signal)
4058 			&& stream_has_pll->link->connector_signal
4059 			!= SIGNAL_TYPE_VIRTUAL)
4060 			return stream_has_pll;
4061 
4062 	}
4063 
4064 	return NULL;
4065 }
4066 
4067 static int get_norm_pix_clk(const struct dc_crtc_timing *timing)
4068 {
4069 	uint32_t pix_clk = timing->pix_clk_100hz;
4070 	uint32_t normalized_pix_clk = pix_clk;
4071 
4072 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
4073 		pix_clk /= 2;
4074 	if (timing->pixel_encoding != PIXEL_ENCODING_YCBCR422) {
4075 		switch (timing->display_color_depth) {
4076 		case COLOR_DEPTH_666:
4077 		case COLOR_DEPTH_888:
4078 			normalized_pix_clk = pix_clk;
4079 			break;
4080 		case COLOR_DEPTH_101010:
4081 			normalized_pix_clk = (pix_clk * 30) / 24;
4082 			break;
4083 		case COLOR_DEPTH_121212:
4084 			normalized_pix_clk = (pix_clk * 36) / 24;
4085 			break;
4086 		case COLOR_DEPTH_141414:
4087 			normalized_pix_clk = (pix_clk * 42) / 24;
4088 			break;
4089 		case COLOR_DEPTH_161616:
4090 			normalized_pix_clk = (pix_clk * 48) / 24;
4091 			break;
4092 		default:
4093 			ASSERT(0);
4094 		break;
4095 		}
4096 	}
4097 	return normalized_pix_clk;
4098 }
4099 
4100 static void calculate_phy_pix_clks(struct dc_stream_state *stream)
4101 {
4102 	/* update actual pixel clock on all streams */
4103 	if (dc_is_hdmi_signal(stream->signal))
4104 		stream->phy_pix_clk = get_norm_pix_clk(
4105 			&stream->timing) / 10;
4106 	else
4107 		stream->phy_pix_clk =
4108 			stream->timing.pix_clk_100hz / 10;
4109 
4110 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
4111 		stream->phy_pix_clk *= 2;
4112 }
4113 
4114 static int acquire_resource_from_hw_enabled_state(
4115 		struct resource_context *res_ctx,
4116 		const struct resource_pool *pool,
4117 		struct dc_stream_state *stream)
4118 {
4119 	struct dc_link *link = stream->link;
4120 	unsigned int i, inst, tg_inst = 0;
4121 	uint32_t numPipes = 1;
4122 	uint32_t id_src[4] = {0};
4123 
4124 	/* Check for enabled DIG to identify enabled display */
4125 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
4126 		return -1;
4127 
4128 	inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
4129 
4130 	if (inst == ENGINE_ID_UNKNOWN)
4131 		return -1;
4132 
4133 	for (i = 0; i < pool->stream_enc_count; i++) {
4134 		if (pool->stream_enc[i]->id == inst) {
4135 			tg_inst = pool->stream_enc[i]->funcs->dig_source_otg(
4136 				pool->stream_enc[i]);
4137 			break;
4138 		}
4139 	}
4140 
4141 	// tg_inst not found
4142 	if (i == pool->stream_enc_count)
4143 		return -1;
4144 
4145 	if (tg_inst >= pool->timing_generator_count)
4146 		return -1;
4147 
4148 	if (!res_ctx->pipe_ctx[tg_inst].stream) {
4149 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[tg_inst];
4150 
4151 		pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
4152 		id_src[0] = tg_inst;
4153 
4154 		if (pipe_ctx->stream_res.tg->funcs->get_optc_source)
4155 			pipe_ctx->stream_res.tg->funcs->get_optc_source(pipe_ctx->stream_res.tg,
4156 						&numPipes, &id_src[0], &id_src[1]);
4157 
4158 		if (id_src[0] == 0xf && id_src[1] == 0xf) {
4159 			id_src[0] = tg_inst;
4160 			numPipes = 1;
4161 		}
4162 
4163 		for (i = 0; i < numPipes; i++) {
4164 			//Check if src id invalid
4165 			if (id_src[i] == 0xf)
4166 				return -1;
4167 
4168 			pipe_ctx = &res_ctx->pipe_ctx[id_src[i]];
4169 
4170 			pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
4171 			pipe_ctx->plane_res.mi = pool->mis[id_src[i]];
4172 			pipe_ctx->plane_res.hubp = pool->hubps[id_src[i]];
4173 			pipe_ctx->plane_res.ipp = pool->ipps[id_src[i]];
4174 			pipe_ctx->plane_res.xfm = pool->transforms[id_src[i]];
4175 			pipe_ctx->plane_res.dpp = pool->dpps[id_src[i]];
4176 			pipe_ctx->stream_res.opp = pool->opps[id_src[i]];
4177 
4178 			if (pool->dpps[id_src[i]]) {
4179 				pipe_ctx->plane_res.mpcc_inst = (uint8_t)pool->dpps[id_src[i]]->inst;
4180 
4181 				if (pool->mpc->funcs->read_mpcc_state) {
4182 					struct mpcc_state s = {0};
4183 
4184 					pool->mpc->funcs->read_mpcc_state(pool->mpc, pipe_ctx->plane_res.mpcc_inst, &s);
4185 
4186 					if (s.dpp_id < MAX_MPCC)
4187 						pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].dpp_id =
4188 								s.dpp_id;
4189 
4190 					if (s.bot_mpcc_id < MAX_MPCC)
4191 						pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].mpcc_bot =
4192 								&pool->mpc->mpcc_array[s.bot_mpcc_id];
4193 
4194 					if (s.opp_id < MAX_OPP)
4195 						pipe_ctx->stream_res.opp->mpc_tree_params.opp_id = s.opp_id;
4196 				}
4197 			}
4198 			pipe_ctx->pipe_idx = (uint8_t)id_src[i];
4199 
4200 			if (id_src[i] >= pool->timing_generator_count) {
4201 				id_src[i] = pool->timing_generator_count - 1;
4202 
4203 				pipe_ctx->stream_res.tg = pool->timing_generators[id_src[i]];
4204 				pipe_ctx->stream_res.opp = pool->opps[id_src[i]];
4205 			}
4206 
4207 			pipe_ctx->stream = stream;
4208 		}
4209 
4210 		if (numPipes == 2) {
4211 			stream->apply_boot_odm_mode = dm_odm_combine_policy_2to1;
4212 			res_ctx->pipe_ctx[id_src[0]].next_odm_pipe = &res_ctx->pipe_ctx[id_src[1]];
4213 			res_ctx->pipe_ctx[id_src[0]].prev_odm_pipe = NULL;
4214 			res_ctx->pipe_ctx[id_src[1]].next_odm_pipe = NULL;
4215 			res_ctx->pipe_ctx[id_src[1]].prev_odm_pipe = &res_ctx->pipe_ctx[id_src[0]];
4216 		} else
4217 			stream->apply_boot_odm_mode = dm_odm_combine_mode_disabled;
4218 
4219 		return id_src[0];
4220 	}
4221 
4222 	return -1;
4223 }
4224 
4225 static void mark_seamless_boot_stream(const struct dc  *dc,
4226 				      struct dc_stream_state *stream)
4227 {
4228 	struct dc_bios *dcb = dc->ctx->dc_bios;
4229 
4230 	DC_LOGGER_INIT(dc->ctx->logger);
4231 
4232 	if (stream->apply_seamless_boot_optimization)
4233 		return;
4234 	if (!dc->config.allow_seamless_boot_optimization)
4235 		return;
4236 	if (dcb->funcs->is_accelerated_mode(dcb))
4237 		return;
4238 	if (dc_validate_boot_timing(dc, stream->sink, &stream->timing)) {
4239 		stream->apply_seamless_boot_optimization = true;
4240 		DC_LOG_DC("Marked stream for seamless boot optimization\n");
4241 	}
4242 }
4243 
4244 /*
4245  * Acquire a pipe as OTG master and assign to the stream in new dc context.
4246  * return - true if OTG master pipe is acquired and new dc context is updated.
4247  * false if it fails to acquire an OTG master pipe for this stream.
4248  *
4249  * In the example below, we acquired pipe 0 as OTG master pipe for the stream.
4250  * After the function its Inter-pipe Relation is represented by the diagram
4251  * below.
4252  *
4253  *       Inter-pipe Relation
4254  *        __________________________________________________
4255  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
4256  *       |        |               |           |             |
4257  *       |   0    |               |blank ------------------ |
4258  *       |________|_______________|___________|_____________|
4259  */
4260 static bool acquire_otg_master_pipe_for_stream(
4261 		const struct dc_state *cur_ctx,
4262 		struct dc_state *new_ctx,
4263 		const struct resource_pool *pool,
4264 		struct dc_stream_state *stream)
4265 {
4266 	/* TODO: Move this function to DCN specific resource file and acquire
4267 	 * DSC resource here. The reason is that the function should have the
4268 	 * same level of responsibility as when we acquire secondary OPP head.
4269 	 * We acquire DSC when we acquire secondary OPP head, so we should
4270 	 * acquire DSC when we acquire OTG master.
4271 	 */
4272 	int pipe_idx;
4273 	struct pipe_ctx *pipe_ctx = NULL;
4274 
4275 	/*
4276 	 * Upper level code is responsible to optimize unnecessary addition and
4277 	 * removal for unchanged streams. So unchanged stream will keep the same
4278 	 * OTG master instance allocated. When current stream is removed and a
4279 	 * new stream is added, we want to reuse the OTG instance made available
4280 	 * by the removed stream first. If not found, we try to avoid of using
4281 	 * any free pipes already used in current context as this could tear
4282 	 * down exiting ODM/MPC/MPO configuration unnecessarily.
4283 	 */
4284 
4285 	/*
4286 	 * Try to acquire the same OTG master already in use. This is not
4287 	 * optimal because resetting an enabled OTG master pipe for a new stream
4288 	 * requires an extra frame of wait. However there are test automation
4289 	 * and eDP assumptions that rely on reusing the same OTG master pipe
4290 	 * during mode change. We have to keep this logic as is for now.
4291 	 */
4292 	pipe_idx = recource_find_free_pipe_used_as_otg_master_in_cur_res_ctx(
4293 			&cur_ctx->res_ctx, &new_ctx->res_ctx, pool);
4294 	/*
4295 	 * Try to acquire a pipe not used in current resource context to avoid
4296 	 * pipe swapping.
4297 	 */
4298 	if (pipe_idx == FREE_PIPE_INDEX_NOT_FOUND)
4299 		pipe_idx = recource_find_free_pipe_not_used_in_cur_res_ctx(
4300 				&cur_ctx->res_ctx, &new_ctx->res_ctx, pool);
4301 	/*
4302 	 * If pipe swapping is unavoidable, try to acquire pipe used as
4303 	 * secondary DPP pipe in current state as we prioritize to support more
4304 	 * streams over supporting MPO planes.
4305 	 */
4306 	if (pipe_idx == FREE_PIPE_INDEX_NOT_FOUND)
4307 		pipe_idx = resource_find_free_pipe_used_as_cur_sec_dpp(
4308 				&cur_ctx->res_ctx, &new_ctx->res_ctx, pool);
4309 	if (pipe_idx == FREE_PIPE_INDEX_NOT_FOUND)
4310 		pipe_idx = resource_find_any_free_pipe(&new_ctx->res_ctx, pool);
4311 	if (pipe_idx != FREE_PIPE_INDEX_NOT_FOUND) {
4312 		pipe_ctx = &new_ctx->res_ctx.pipe_ctx[pipe_idx];
4313 		memset(pipe_ctx, 0, sizeof(*pipe_ctx));
4314 		pipe_ctx->pipe_idx = (uint8_t)pipe_idx;
4315 		pipe_ctx->stream_res.tg = pool->timing_generators[pipe_idx];
4316 		pipe_ctx->plane_res.mi = pool->mis[pipe_idx];
4317 		pipe_ctx->plane_res.hubp = pool->hubps[pipe_idx];
4318 		pipe_ctx->plane_res.ipp = pool->ipps[pipe_idx];
4319 		pipe_ctx->plane_res.xfm = pool->transforms[pipe_idx];
4320 		pipe_ctx->plane_res.dpp = pool->dpps[pipe_idx];
4321 		pipe_ctx->stream_res.opp = pool->opps[pipe_idx];
4322 		if (pool->dpps[pipe_idx])
4323 			pipe_ctx->plane_res.mpcc_inst = (uint8_t)pool->dpps[pipe_idx]->inst;
4324 
4325 		if ((unsigned int)pipe_idx >= pool->timing_generator_count && pool->timing_generator_count != 0) {
4326 			int tg_inst = pool->timing_generator_count - 1;
4327 
4328 			pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
4329 			pipe_ctx->stream_res.opp = pool->opps[tg_inst];
4330 		}
4331 
4332 		pipe_ctx->stream = stream;
4333 	} else {
4334 		pipe_idx = acquire_first_split_pipe(&new_ctx->res_ctx, pool, stream);
4335 	}
4336 
4337 	return pipe_idx != FREE_PIPE_INDEX_NOT_FOUND;
4338 }
4339 
4340 enum dc_status resource_map_pool_resources(
4341 		const struct dc  *dc,
4342 		struct dc_state *context,
4343 		struct dc_stream_state *stream)
4344 {
4345 	const struct resource_pool *pool = dc->res_pool;
4346 	int i;
4347 	struct dc_context *dc_ctx = dc->ctx;
4348 	struct pipe_ctx *pipe_ctx = NULL;
4349 	int pipe_idx = -1;
4350 	bool acquired = false;
4351 	bool is_dio_encoder = true;
4352 
4353 	calculate_phy_pix_clks(stream);
4354 
4355 	mark_seamless_boot_stream(dc, stream);
4356 
4357 	if (stream->apply_seamless_boot_optimization) {
4358 		pipe_idx = acquire_resource_from_hw_enabled_state(
4359 				&context->res_ctx,
4360 				pool,
4361 				stream);
4362 		if (pipe_idx < 0)
4363 			/* hw resource was assigned to other stream */
4364 			stream->apply_seamless_boot_optimization = false;
4365 		else
4366 			acquired = true;
4367 	}
4368 
4369 	if (!acquired)
4370 		/* acquire new resources */
4371 		acquired = acquire_otg_master_pipe_for_stream(dc->current_state,
4372 				context, pool, stream);
4373 
4374 	pipe_ctx = resource_get_otg_master_for_stream(&context->res_ctx, stream);
4375 
4376 	if (!pipe_ctx || pipe_ctx->stream_res.tg == NULL)
4377 		return DC_NO_CONTROLLER_RESOURCE;
4378 
4379 	pipe_ctx->stream_res.stream_enc =
4380 		dc->res_pool->funcs->find_first_free_match_stream_enc_for_link(
4381 			&context->res_ctx, pool, stream);
4382 
4383 	if (!pipe_ctx->stream_res.stream_enc)
4384 		return DC_NO_STREAM_ENC_RESOURCE;
4385 
4386 	update_stream_engine_usage(
4387 		&context->res_ctx, pool,
4388 		pipe_ctx->stream_res.stream_enc,
4389 		true);
4390 
4391 	if (dc_is_hdmi_frl_signal(stream->signal)) {
4392 		is_dio_encoder = false;
4393 		pipe_ctx->stream_res.hpo_frl_stream_enc =
4394 			find_first_free_match_hpo_frl_stream_enc_for_link(
4395 				&context->res_ctx, pool, stream);
4396 
4397 		if (!pipe_ctx->stream_res.hpo_frl_stream_enc)
4398 			if (stream->timing.pix_clk_100hz < 6000000)
4399 				stream->signal = SIGNAL_TYPE_HDMI_TYPE_A;
4400 			else
4401 				return DC_NO_STREAM_ENC_RESOURCE;
4402 		else {
4403 			update_hpo_frl_stream_engine_usage(
4404 				&context->res_ctx, pool,
4405 				pipe_ctx->stream_res.hpo_frl_stream_enc,
4406 				true);
4407 			pipe_ctx->link_res.hpo_frl_link_enc =
4408 				pipe_ctx->stream->link->hpo_frl_link_enc;
4409 			if (!pipe_ctx->link_res.hpo_frl_link_enc) {
4410 				if (!add_hpo_frl_link_enc_to_ctx(&context->res_ctx, pool, pipe_ctx, stream))
4411 					return DC_NO_LINK_ENC_RESOURCE;
4412 			}
4413 			if (!add_dio_link_enc_to_ctx(dc, context, pool, pipe_ctx, stream))
4414 				return DC_NO_LINK_ENC_RESOURCE;
4415 		}
4416 	}
4417 
4418 	/* Allocate DP HPO Stream Encoder based on signal, hw capabilities
4419 	 * and link settings
4420 	 */
4421 	if (dc_is_dp_signal(stream->signal) ||
4422 			dc_is_virtual_signal(stream->signal)) {
4423 		if (!dc->link_srv->dp_decide_link_settings(stream,
4424 				&pipe_ctx->link_config.dp_link_settings))
4425 			return DC_FAIL_DP_LINK_BANDWIDTH;
4426 
4427 		dc->link_srv->dp_decide_tunnel_settings(stream,
4428 				&pipe_ctx->link_config.dp_tunnel_settings);
4429 
4430 		if (dc->link_srv->dp_get_encoding_format(
4431 				&pipe_ctx->link_config.dp_link_settings) == DP_128b_132b_ENCODING) {
4432 			pipe_ctx->stream_res.hpo_dp_stream_enc =
4433 					find_first_free_match_hpo_dp_stream_enc_for_link(
4434 							&context->res_ctx, pool, stream);
4435 
4436 			if (!pipe_ctx->stream_res.hpo_dp_stream_enc)
4437 				return DC_NO_STREAM_ENC_RESOURCE;
4438 
4439 			update_hpo_dp_stream_engine_usage(
4440 					&context->res_ctx, pool,
4441 					pipe_ctx->stream_res.hpo_dp_stream_enc,
4442 					true);
4443 			if (!add_hpo_dp_link_enc_to_ctx(&context->res_ctx, pool, pipe_ctx, stream))
4444 				return DC_NO_LINK_ENC_RESOURCE;
4445 		}
4446 	}
4447 
4448 	if (dc->config.unify_link_enc_assignment && is_dio_encoder)
4449 		if (!add_dio_link_enc_to_ctx(dc, context, pool, pipe_ctx, stream))
4450 			return DC_NO_LINK_ENC_RESOURCE;
4451 
4452 	/* TODO: Add check if ASIC support and EDID audio */
4453 	if (!stream->converter_disable_audio &&
4454 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
4455 	    stream->audio_info.mode_count &&
4456 		(stream->audio_info.flags.all ||
4457 		(stream->sink && stream->sink->edid_caps.panel_patch.skip_audio_sab_check))) {
4458 		pipe_ctx->stream_res.audio = find_first_free_audio(
4459 		&context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id, dc_ctx->dce_version);
4460 
4461 		/*
4462 		 * Audio assigned in order first come first get.
4463 		 * There are asics which has number of audio
4464 		 * resources less then number of pipes
4465 		 */
4466 		if (pipe_ctx->stream_res.audio)
4467 			update_audio_usage(&context->res_ctx, pool,
4468 					   pipe_ctx->stream_res.audio, true);
4469 	}
4470 
4471 	/* Add ABM to the resource if on EDP */
4472 	if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal)) {
4473 		if (pool->abm)
4474 			pipe_ctx->stream_res.abm = pool->abm;
4475 		else
4476 			pipe_ctx->stream_res.abm = pool->multiple_abms[pipe_ctx->stream_res.tg->inst];
4477 	}
4478 
4479 	for (i = 0; i < context->stream_count; i++)
4480 		if (context->streams[i] == stream) {
4481 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
4482 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->stream_enc_inst;
4483 			if (pipe_ctx->stream_res.hpo_frl_stream_enc != NULL)
4484 				context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.hpo_frl_stream_enc->stream_enc_inst;
4485 			context->stream_status[i].audio_inst =
4486 				pipe_ctx->stream_res.audio ? pipe_ctx->stream_res.audio->inst : -1;
4487 
4488 			return DC_OK;
4489 		}
4490 
4491 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
4492 	return DC_ERROR_UNEXPECTED;
4493 }
4494 
4495 bool dc_resource_is_dsc_encoding_supported(const struct dc *dc)
4496 {
4497 	if (dc->res_pool == NULL)
4498 		return false;
4499 
4500 	return dc->res_pool->res_cap->num_dsc > 0;
4501 }
4502 
4503 static bool planes_changed_for_existing_stream(struct dc_state *context,
4504 					       struct dc_stream_state *stream,
4505 					       const struct dc_validation_set *set)
4506 {
4507 	unsigned int i, j;
4508 	struct dc_stream_status *stream_status = NULL;
4509 
4510 	for (i = 0; i < context->stream_count; i++) {
4511 		if (context->streams[i] == stream) {
4512 			stream_status = &context->stream_status[i];
4513 			break;
4514 		}
4515 	}
4516 
4517 	if (!stream_status) {
4518 		ASSERT(0);
4519 		return false;
4520 	}
4521 
4522 	for (i = 0; i < set->stream_count; i++)
4523 		if (set->streams[i].stream == stream)
4524 			break;
4525 
4526 	if (i == set->stream_count)
4527 		ASSERT(0);
4528 
4529 	if (set->streams[i].plane_count != stream_status->plane_count)
4530 		return true;
4531 
4532 	for (j = 0; j < set->streams[i].plane_count; j++)
4533 		if (set->streams[i].plane_states[j] != stream_status->plane_states[j])
4534 			return true;
4535 
4536 	return false;
4537 }
4538 
4539 static bool add_all_planes_for_stream(
4540 		const struct dc *dc,
4541 		struct dc_stream_state *stream,
4542 		const struct dc_validation_set *set,
4543 		struct dc_state *state)
4544 {
4545 	unsigned int i, j;
4546 
4547 	for (i = 0; i < set->stream_count; i++)
4548 		if (set->streams[i].stream == stream)
4549 			break;
4550 
4551 	if (i == set->stream_count) {
4552 		dm_error("Stream %p not found in set!\n", stream);
4553 		return false;
4554 	}
4555 
4556 	for (j = 0; j < set->streams[i].plane_count; j++)
4557 		if (!dc_state_add_plane(dc, stream, set->streams[i].plane_states[j], state))
4558 			return false;
4559 
4560 	return true;
4561 }
4562 
4563 /**
4564  * resource_validate_probe_set - Validate a probe descriptor set against the ASIC.
4565  * @dc:          DC instance providing the HWSS capability hooks
4566  * @probes:      desired probe descriptors
4567  * @probe_count: number of valid entries in @probes
4568  *
4569  * Return: DC_OK if achievable, otherwise a DC error.
4570  */
4571 enum dc_status resource_validate_probe_set(struct dc *dc,
4572 		const struct dc_probe_state *probes,
4573 		uint8_t probe_count)
4574 {
4575 	uint8_t i;
4576 
4577 	if (probe_count == 0)
4578 		return DC_OK;
4579 
4580 	if (!dc->hwss.program_perfmon)
4581 		return DC_NOT_SUPPORTED;
4582 
4583 	if (probe_count > MAX_PROBES)
4584 		return DC_NOT_SUPPORTED;
4585 
4586 	for (i = 0; i < probe_count; i++) {
4587 		switch (probes[i].type) {
4588 		/* These global probes support a one-frame measurement and reset. */
4589 		case DC_PROBE_PEAK_MEM_BW:
4590 		case DC_PROBE_PEAK_MEM_BW_STRESSED:
4591 		case DC_PROBE_AVG_MEM_BW:
4592 		case DC_PROBE_MEM_LATENCY:
4593 		case DC_PROBE_PREFETCH_DATA_SIZE:
4594 			if (probes[i].scope.type != DC_PROBE_SCOPE_GLOBAL)
4595 				return DC_NOT_SUPPORTED;
4596 			if (probes[i].target_state != DC_PROBE_NOT_MEASURING &&
4597 					probes[i].target_state != DC_PROBE_MEASURED)
4598 				return DC_NOT_SUPPORTED;
4599 			break;
4600 		/* The global urgent assertion counter additionally supports polling. */
4601 		case DC_PROBE_URGENT_ASSERTION_COUNT:
4602 			if (probes[i].scope.type != DC_PROBE_SCOPE_GLOBAL)
4603 				return DC_NOT_SUPPORTED;
4604 			if (probes[i].target_state != DC_PROBE_NOT_MEASURING &&
4605 					probes[i].target_state != DC_PROBE_MEASURING &&
4606 					probes[i].target_state != DC_PROBE_MEASURED)
4607 				return DC_NOT_SUPPORTED;
4608 			break;
4609 		default:
4610 			return DC_NOT_SUPPORTED;
4611 		}
4612 
4613 		if (probes[i].type == DC_PROBE_PEAK_MEM_BW_STRESSED &&
4614 				!dc->res_pool->lsdma_scratch.buffer)
4615 			return DC_NO_DRAM_BUFFER_RESOURCE;
4616 	}
4617 
4618 	return DC_OK;
4619 }
4620 
4621 /**
4622  * dc_validate_with_context - Validate and update the potential new stream in the context object
4623  *
4624  * @dc: Used to get the current state status
4625  * @set: Root validation object holding all streams and their planes
4626  * @context: New context
4627  * @validate_mode: identify the validation mode
4628  *
4629  * This function updates the potential new stream in the context object. It
4630  * creates multiple lists for the add, remove, and unchanged streams. In
4631  * particular, if the unchanged streams have a plane that changed, it is
4632  * necessary to remove all planes from the unchanged streams. In summary, this
4633  * function is responsible for validating the new context.
4634  *
4635  * Return:
4636  * In case of success, return DC_OK (1), otherwise, return a DC error.
4637  */
4638 enum dc_status dc_validate_with_context(struct dc *dc,
4639 					const struct dc_validation_set *set,
4640 					struct dc_state *context,
4641 					enum dc_validate_mode validate_mode)
4642 {
4643 	struct dc_stream_state *unchanged_streams[MAX_PIPES] = { 0 };
4644 	struct dc_stream_state *del_streams[MAX_PIPES] = { 0 };
4645 	struct dc_stream_state *add_streams[MAX_PIPES] = { 0 };
4646 	unsigned int old_stream_count = context->stream_count;
4647 	enum dc_status res = DC_ERROR_UNEXPECTED;
4648 	unsigned int unchanged_streams_count = 0;
4649 	unsigned int del_streams_count = 0;
4650 	unsigned int add_streams_count = 0;
4651 	bool found = false;
4652 	unsigned int i, j, k;
4653 
4654 	DC_LOGGER_INIT(dc->ctx->logger);
4655 
4656 	/* First build a list of streams to be remove from current context */
4657 	for (i = 0; i < old_stream_count; i++) {
4658 		struct dc_stream_state *stream = context->streams[i];
4659 
4660 		for (j = 0; j < set->stream_count; j++) {
4661 			if (stream == set->streams[j].stream) {
4662 				found = true;
4663 				break;
4664 			}
4665 		}
4666 
4667 		if (!found)
4668 			del_streams[del_streams_count++] = stream;
4669 
4670 		found = false;
4671 	}
4672 
4673 	/* Second, build a list of new streams */
4674 	for (i = 0; i < set->stream_count; i++) {
4675 		struct dc_stream_state *stream = set->streams[i].stream;
4676 
4677 		for (j = 0; j < old_stream_count; j++) {
4678 			if (stream == context->streams[j]) {
4679 				found = true;
4680 				break;
4681 			}
4682 		}
4683 
4684 		if (!found)
4685 			add_streams[add_streams_count++] = stream;
4686 
4687 		found = false;
4688 	}
4689 
4690 	/* Build a list of unchanged streams which is necessary for handling
4691 	 * planes change such as added, removed, and updated.
4692 	 */
4693 	for (i = 0; i < set->stream_count; i++) {
4694 		/* Check if stream is part of the delete list */
4695 		for (j = 0; j < del_streams_count; j++) {
4696 			if (set->streams[i].stream == del_streams[j]) {
4697 				found = true;
4698 				break;
4699 			}
4700 		}
4701 
4702 		if (!found) {
4703 			/* Check if stream is part of the add list */
4704 			for (j = 0; j < add_streams_count; j++) {
4705 				if (set->streams[i].stream == add_streams[j]) {
4706 					found = true;
4707 					break;
4708 				}
4709 			}
4710 		}
4711 
4712 		if (!found)
4713 			unchanged_streams[unchanged_streams_count++] = set->streams[i].stream;
4714 
4715 		found = false;
4716 	}
4717 
4718 	/* Remove all planes for unchanged streams if planes changed */
4719 	for (i = 0; i < unchanged_streams_count; i++) {
4720 		if (planes_changed_for_existing_stream(context,
4721 						       unchanged_streams[i],
4722 						       set)) {
4723 
4724 			if (!dc_state_rem_all_planes_for_stream(dc,
4725 							  unchanged_streams[i],
4726 							  context)) {
4727 				res = DC_FAIL_DETACH_SURFACES;
4728 				goto fail;
4729 			}
4730 		}
4731 	}
4732 
4733 	/* Remove all planes for removed streams and then remove the streams */
4734 	for (i = 0; i < del_streams_count; i++) {
4735 		/* Need to cpy the dwb data from the old stream in order to efc to work */
4736 		if (del_streams[i]->num_wb_info > 0) {
4737 			for (j = 0; j < add_streams_count; j++) {
4738 				if (del_streams[i]->sink == add_streams[j]->sink) {
4739 					add_streams[j]->num_wb_info = del_streams[i]->num_wb_info;
4740 					for (k = 0; k < del_streams[i]->num_wb_info; k++)
4741 						add_streams[j]->writeback_info[k] = del_streams[i]->writeback_info[k];
4742 				}
4743 			}
4744 		}
4745 
4746 		if (dc_state_get_stream_subvp_type(context, del_streams[i]) == SUBVP_PHANTOM) {
4747 			/* remove phantoms specifically */
4748 			if (!dc_state_rem_all_phantom_planes_for_stream(dc, del_streams[i], context, true)) {
4749 				res = DC_FAIL_DETACH_SURFACES;
4750 				goto fail;
4751 			}
4752 
4753 			res = dc_state_remove_phantom_stream(dc, context, del_streams[i]);
4754 			dc_state_release_phantom_stream(dc, context, del_streams[i]);
4755 		} else {
4756 			if (!dc_state_rem_all_planes_for_stream(dc, del_streams[i], context)) {
4757 				res = DC_FAIL_DETACH_SURFACES;
4758 				goto fail;
4759 			}
4760 
4761 			res = dc_state_remove_stream(dc, context, del_streams[i]);
4762 		}
4763 
4764 		if (res != DC_OK)
4765 			goto fail;
4766 	}
4767 
4768 	/* Swap seamless boot stream to pipe 0 (if needed) to ensure pipe_ctx
4769 	 * matches. This may change in the future if seamless_boot_stream can be
4770 	 * multiple.
4771 	 */
4772 	for (i = 0; i < add_streams_count; i++) {
4773 		mark_seamless_boot_stream(dc, add_streams[i]);
4774 		if (add_streams[i]->apply_seamless_boot_optimization && i != 0) {
4775 			struct dc_stream_state *temp = add_streams[0];
4776 
4777 			add_streams[0] = add_streams[i];
4778 			add_streams[i] = temp;
4779 			break;
4780 		}
4781 	}
4782 
4783 	/* Add new streams and then add all planes for the new stream */
4784 	for (i = 0; i < add_streams_count; i++) {
4785 		calculate_phy_pix_clks(add_streams[i]);
4786 		res = dc_state_add_stream(dc, context, add_streams[i]);
4787 		if (res != DC_OK)
4788 			goto fail;
4789 
4790 		if (!add_all_planes_for_stream(dc, add_streams[i], set, context)) {
4791 			res = DC_FAIL_ATTACH_SURFACES;
4792 			goto fail;
4793 		}
4794 	}
4795 
4796 	/* Add all planes for unchanged streams if planes changed */
4797 	for (i = 0; i < unchanged_streams_count; i++) {
4798 		if (planes_changed_for_existing_stream(context,
4799 						       unchanged_streams[i],
4800 						       set)) {
4801 			if (!add_all_planes_for_stream(dc, unchanged_streams[i], set, context)) {
4802 				res = DC_FAIL_ATTACH_SURFACES;
4803 				goto fail;
4804 			}
4805 		}
4806 	}
4807 
4808 	/* clear subvp cursor limitations */
4809 	for (i = 0; i < context->stream_count; i++) {
4810 		dc_state_set_stream_subvp_cursor_limit(context->streams[i], context, false);
4811 	}
4812 
4813 	res = dc_validate_global_state(dc, context, validate_mode);
4814 
4815 	/* calculate pixel rate divider after deciding pxiel clock & odm combine  */
4816 	if ((dc->hwss.calculate_pix_rate_divider) && (res == DC_OK)) {
4817 		for (i = 0; i < add_streams_count; i++)
4818 			dc->hwss.calculate_pix_rate_divider(dc, context, add_streams[i]);
4819 	}
4820 
4821 fail:
4822 	if (res != DC_OK)
4823 		DC_LOG_WARNING("%s:resource validation failed, dc_status:%d\n",
4824 			       __func__,
4825 			       res);
4826 
4827 	return res;
4828 }
4829 
4830 #if defined(CONFIG_DRM_AMD_DC_FP)
4831 /**
4832  * dc_update_modified_pix_clock_for_dsc_with_padding() - update pix_clk for dsc with padding
4833  *
4834  * @stream: Pointer to the stream structure.
4835  * @timing: Pointer to the stream dc_crtc_timing structure.
4836  * @dsc_padding_params: Pointer to the DSC padding parameters structure.
4837  *
4838  * This function updated the pix_clk for dsc with padding stored in pipe_ctx
4839  * such that the OTG h_active time fits withing the expected compressed active
4840  * time calculated according to HDMI spec. H_total is then increased to
4841  * maintain the same OTG line time as before the increased pix_clk.
4842  */
4843 static void dc_update_modified_pix_clock_for_dsc_with_padding(const struct dc_stream_state *stream,
4844 		const struct dc_crtc_timing *timing, struct dsc_padding_params *dsc_padding_params)
4845 {
4846 	DC_FP_START();
4847 	frl_modified_pix_clock_for_dsc_padding(stream->link->frl_verified_link_cap.borrow_params.hc_active_target,
4848 	stream->link->frl_verified_link_cap.borrow_params.hc_blank_target,
4849 	stream->link->frl_verified_link_cap.frl_num_lanes,
4850 	timing->pix_clk_100hz,
4851 	stream->link->frl_verified_link_cap.frl_link_rate,
4852 	timing->h_addressable,
4853 	timing->h_border_left,
4854 	timing->h_border_right,
4855 	timing->h_total,
4856 	(timing->h_addressable + dsc_padding_params->dsc_hactive_padding),
4857 	&dsc_padding_params->dsc_pix_clk_100hz,
4858 	&dsc_padding_params->dsc_htotal_padding);
4859 	DC_FP_END();
4860 
4861 	dsc_padding_params->dsc_htotal_padding = dsc_padding_params->dsc_htotal_padding - timing->h_total;
4862 }
4863 #endif /* CONFIG_DRM_AMD_DC_FP */
4864 
4865 /**
4866  * calculate_timing_params_for_dsc_with_padding - Calculates timing parameters for DSC with padding.
4867  * @pipe_ctx: Pointer to the pipe context structure.
4868  *
4869  * This function calculates the timing parameters for a given pipe context based on the
4870  * display stream compression (DSC) configuration. If the horizontal active pixels (hactive) are less
4871  * than the total width of the DSC slices, it sets the dsc_hactive_padding value to the difference. If the
4872  * total horizontal timing minus the dsc_hactive_padding value is less than 32, it resets the dsc_hactive_padding
4873  * value to 0.
4874  */
4875 static void calculate_timing_params_for_dsc_with_padding(struct pipe_ctx *pipe_ctx)
4876 {
4877 	struct dc_stream_state *stream = NULL;
4878 
4879 	if (!pipe_ctx)
4880 		return;
4881 
4882 	stream = pipe_ctx->stream;
4883 	pipe_ctx->dsc_padding_params.dsc_hactive_padding = 0;
4884 	pipe_ctx->dsc_padding_params.dsc_htotal_padding = 0;
4885 
4886 	if (stream)
4887 		pipe_ctx->dsc_padding_params.dsc_pix_clk_100hz = stream->timing.pix_clk_100hz;
4888 
4889 #if defined(CONFIG_DRM_AMD_DC_FP)
4890 	uint32_t hactive;
4891 	uint32_t ceil_slice_width;
4892 	if (stream && stream->timing.flags.DSC && stream->signal == SIGNAL_TYPE_HDMI_FRL) {
4893 		hactive = stream->timing.h_addressable + stream->timing.h_border_left + stream->timing.h_border_right;
4894 
4895 		/* Assume if determined slices does not divide Hactive evenly, Hborrow is needed for padding*/
4896 		if (hactive % stream->timing.dsc_cfg.num_slices_h != 0) {
4897 			ceil_slice_width = (hactive / stream->timing.dsc_cfg.num_slices_h) + 1;
4898 
4899 			/* If YCBCR420 slice width must be even */
4900 			if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420 && ceil_slice_width % 2 != 0)
4901 				ceil_slice_width++;
4902 
4903 			pipe_ctx->dsc_padding_params.dsc_hactive_padding =
4904 				(uint8_t)(ceil_slice_width * stream->timing.dsc_cfg.num_slices_h - hactive);
4905 
4906 			if (stream->timing.h_total - hactive - pipe_ctx->dsc_padding_params.dsc_hactive_padding < 32)
4907 				pipe_ctx->dsc_padding_params.dsc_hactive_padding = 0;
4908 
4909 			dc_update_modified_pix_clock_for_dsc_with_padding(stream, &stream->timing, &pipe_ctx->dsc_padding_params);
4910 		}
4911 	}
4912 #endif
4913 }
4914 
4915 /**
4916  * dc_validate_global_state() - Determine if hardware can support a given state
4917  *
4918  * @dc: dc struct for this driver
4919  * @new_ctx: state to be validated
4920  * @validate_mode: identify the validation mode
4921  *
4922  * Checks hardware resource availability and bandwidth requirement.
4923  *
4924  * Return:
4925  * DC_OK if the result can be programmed. Otherwise, an error code.
4926  */
4927 enum dc_status dc_validate_global_state(
4928 		struct dc *dc,
4929 		struct dc_state *new_ctx,
4930 		enum dc_validate_mode validate_mode)
4931 {
4932 	enum dc_status result = DC_ERROR_UNEXPECTED;
4933 	int i;
4934 	unsigned int j;
4935 
4936 	if (!new_ctx)
4937 		return DC_ERROR_UNEXPECTED;
4938 
4939 	if (dc->res_pool->funcs->validate_global) {
4940 		result = dc->res_pool->funcs->validate_global(dc, new_ctx);
4941 		if (result != DC_OK)
4942 			return result;
4943 	}
4944 
4945 	for (i = 0; i < new_ctx->stream_count; i++) {
4946 		struct dc_stream_state *stream = new_ctx->streams[i];
4947 
4948 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4949 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
4950 
4951 			if (pipe_ctx->stream != stream)
4952 				continue;
4953 
4954 			/* Decide whether hblank borrow is needed and save it in pipe_ctx */
4955 			if (dc->debug.enable_hblank_borrow)
4956 				calculate_timing_params_for_dsc_with_padding(pipe_ctx);
4957 
4958 			if (dc->res_pool->funcs->patch_unknown_plane_state &&
4959 					pipe_ctx->plane_state &&
4960 					pipe_ctx->plane_state->tiling_info.gfx9.swizzle == DC_SW_UNKNOWN) {
4961 				result = dc->res_pool->funcs->patch_unknown_plane_state(pipe_ctx->plane_state);
4962 				if (result != DC_OK)
4963 					return result;
4964 			}
4965 
4966 			/* Switch to dp clock source only if there is
4967 			 * no non dp stream that shares the same timing
4968 			 * with the dp stream.
4969 			 */
4970 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
4971 				!find_pll_sharable_stream(stream, new_ctx)) {
4972 
4973 				resource_unreference_clock_source(
4974 						&new_ctx->res_ctx,
4975 						dc->res_pool,
4976 						pipe_ctx->clock_source);
4977 
4978 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
4979 				resource_reference_clock_source(
4980 						&new_ctx->res_ctx,
4981 						dc->res_pool,
4982 						 pipe_ctx->clock_source);
4983 			}
4984 		}
4985 	}
4986 
4987 	result = resource_build_scaling_params_for_context(dc, new_ctx);
4988 
4989 	if (result == DC_OK)
4990 		result = dc->res_pool->funcs->validate_bandwidth(dc, new_ctx, validate_mode);
4991 
4992 	if (result == DC_OK)
4993 		result = resource_validate_probe_set(dc, new_ctx->probes,
4994 				(uint8_t)new_ctx->probe_count);
4995 
4996 	return result;
4997 }
4998 
4999 static void patch_gamut_packet_checksum(
5000 		struct dc_info_packet *gamut_packet)
5001 {
5002 	/* For gamut we recalc checksum */
5003 	if (gamut_packet->valid) {
5004 		uint8_t chk_sum = 0;
5005 		uint8_t *ptr;
5006 		uint8_t i;
5007 
5008 		/*start of the Gamut data. */
5009 		ptr = &gamut_packet->sb[3];
5010 
5011 		for (i = 0; i <= gamut_packet->sb[1]; i++)
5012 			chk_sum += ptr[i];
5013 
5014 		gamut_packet->sb[2] = (uint8_t)(0x100 - chk_sum);
5015 	}
5016 }
5017 
5018 static void set_avi_info_frame(
5019 		struct dc_info_packet *info_packet,
5020 		struct pipe_ctx *pipe_ctx)
5021 {
5022 	struct dc_stream_state *stream = pipe_ctx->stream;
5023 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
5024 	uint32_t pixel_encoding = 0;
5025 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
5026 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
5027 	uint8_t *check_sum = NULL;
5028 	uint8_t byte_index = 0;
5029 	union hdmi_info_packet hdmi_info;
5030 	unsigned int vic = pipe_ctx->stream->timing.vic;
5031 	unsigned int rid = pipe_ctx->stream->timing.rid;
5032 	unsigned int fr_ind = pipe_ctx->stream->timing.fr_index;
5033 	enum dc_timing_3d_format format;
5034 
5035 	if (stream->avi_infopacket.valid) {
5036 		*info_packet = stream->avi_infopacket;
5037 		return;
5038 	}
5039 
5040 	memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
5041 
5042 
5043 	color_space = pipe_ctx->stream->output_color_space;
5044 	if (color_space == COLOR_SPACE_UNKNOWN)
5045 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
5046 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
5047 
5048 	/* Initialize header */
5049 	hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
5050 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
5051 	* not be used in HDMI 2.0 (Section 10.1) */
5052 	hdmi_info.bits.header.version = 2;
5053 	hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
5054 
5055 	/*
5056 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
5057 	 * according to HDMI 2.0 spec (Section 10.1)
5058 	 */
5059 
5060 	switch (stream->timing.pixel_encoding) {
5061 	case PIXEL_ENCODING_YCBCR422:
5062 		pixel_encoding = 1;
5063 		break;
5064 
5065 	case PIXEL_ENCODING_YCBCR444:
5066 		pixel_encoding = 2;
5067 		break;
5068 	case PIXEL_ENCODING_YCBCR420:
5069 		pixel_encoding = 3;
5070 		break;
5071 
5072 	case PIXEL_ENCODING_RGB:
5073 	default:
5074 		pixel_encoding = 0;
5075 	}
5076 
5077 	/* Y0_Y1_Y2 : The pixel encoding */
5078 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
5079 	hdmi_info.bits.Y0_Y1_Y2 = (uint8_t)pixel_encoding;
5080 
5081 	/* A0 = 1 Active Format Information valid */
5082 	hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
5083 
5084 	/* B0, B1 = 3; Bar info data is valid */
5085 	hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
5086 
5087 	hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
5088 
5089 	/* S0, S1 : Underscan / Overscan */
5090 	/* TODO: un-hardcode scan type */
5091 	scan_type = SCANNING_TYPE_UNDERSCAN;
5092 	hdmi_info.bits.S0_S1 = scan_type;
5093 
5094 	/* C0, C1 : Colorimetry */
5095 	switch (color_space) {
5096 	case COLOR_SPACE_YCBCR709:
5097 	case COLOR_SPACE_YCBCR709_LIMITED:
5098 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
5099 		break;
5100 	case COLOR_SPACE_YCBCR601:
5101 	case COLOR_SPACE_YCBCR601_LIMITED:
5102 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
5103 		break;
5104 	case COLOR_SPACE_2020_RGB_FULLRANGE:
5105 	case COLOR_SPACE_2020_RGB_LIMITEDRANGE:
5106 	case COLOR_SPACE_2020_YCBCR_LIMITED:
5107 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
5108 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
5109 		break;
5110 	case COLOR_SPACE_ADOBERGB:
5111 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
5112 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
5113 		break;
5114 	case COLOR_SPACE_SRGB:
5115 	default:
5116 		hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
5117 		break;
5118 	}
5119 
5120 	if (pixel_encoding && color_space == COLOR_SPACE_2020_YCBCR_LIMITED &&
5121 			stream->out_transfer_func.tf == TRANSFER_FUNCTION_GAMMA22) {
5122 		hdmi_info.bits.EC0_EC2 = 0;
5123 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
5124 	}
5125 
5126 	/* TODO: un-hardcode aspect ratio */
5127 	aspect = stream->timing.aspect_ratio;
5128 
5129 	switch (aspect) {
5130 	case ASPECT_RATIO_4_3:
5131 	case ASPECT_RATIO_16_9:
5132 		hdmi_info.bits.M0_M1 = aspect;
5133 		break;
5134 
5135 	case ASPECT_RATIO_NO_DATA:
5136 	case ASPECT_RATIO_64_27:
5137 	case ASPECT_RATIO_256_135:
5138 	default:
5139 		hdmi_info.bits.M0_M1 = 0;
5140 	}
5141 
5142 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
5143 	hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
5144 
5145 	switch (stream->content_type) {
5146 	case DISPLAY_CONTENT_TYPE_NO_DATA:
5147 		hdmi_info.bits.CN0_CN1 = 0;
5148 		hdmi_info.bits.ITC = 1;
5149 		break;
5150 	case DISPLAY_CONTENT_TYPE_GRAPHICS:
5151 		hdmi_info.bits.CN0_CN1 = 0;
5152 		hdmi_info.bits.ITC = 1;
5153 		break;
5154 	case DISPLAY_CONTENT_TYPE_PHOTO:
5155 		hdmi_info.bits.CN0_CN1 = 1;
5156 		hdmi_info.bits.ITC = 1;
5157 		break;
5158 	case DISPLAY_CONTENT_TYPE_CINEMA:
5159 		hdmi_info.bits.CN0_CN1 = 2;
5160 		hdmi_info.bits.ITC = 1;
5161 		break;
5162 	case DISPLAY_CONTENT_TYPE_GAME:
5163 		hdmi_info.bits.CN0_CN1 = 3;
5164 		hdmi_info.bits.ITC = 1;
5165 		break;
5166 	}
5167 
5168 	if (stream->qs_bit == 1) {
5169 		if (color_space == COLOR_SPACE_SRGB ||
5170 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE)
5171 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
5172 		else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
5173 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
5174 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
5175 		else
5176 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
5177 	} else
5178 		hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
5179 
5180 	/* TODO : We should handle YCC quantization */
5181 	/* but we do not have matrix calculation */
5182 	hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
5183 
5184 	///VIC
5185 	if (pipe_ctx->stream->timing.hdmi_vic != 0)
5186 		vic = 0;
5187 	format = stream->timing.timing_3d_format;
5188 	/*todo, add 3DStereo support*/
5189 	if (format != TIMING_3D_FORMAT_NONE) {
5190 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
5191 		switch (pipe_ctx->stream->timing.hdmi_vic) {
5192 		case 1:
5193 			vic = 95;
5194 			break;
5195 		case 2:
5196 			vic = 94;
5197 			break;
5198 		case 3:
5199 			vic = 93;
5200 			break;
5201 		case 4:
5202 			vic = 98;
5203 			break;
5204 		default:
5205 			break;
5206 		}
5207 	}
5208 	/* If VIC >= 128, the Source shall use AVI InfoFrame Version 3*/
5209 	hdmi_info.bits.VIC0_VIC7 = (uint8_t)vic;
5210 	if (vic >= 128)
5211 		hdmi_info.bits.header.version = 3;
5212 	/* If (C1, C0)=(1, 1) and (EC2, EC1, EC0)=(1, 1, 1),
5213 	 * the Source shall use 20 AVI InfoFrame Version 4
5214 	 */
5215 	if (hdmi_info.bits.C0_C1 == COLORIMETRY_EXTENDED &&
5216 			hdmi_info.bits.EC0_EC2 == COLORIMETRYEX_RESERVED) {
5217 		hdmi_info.bits.header.version = 4;
5218 		hdmi_info.bits.header.length = 14;
5219 	}
5220 
5221 	if (rid != 0 && fr_ind != 0) {
5222 		hdmi_info.bits.header.version = 4;
5223 		hdmi_info.bits.header.length = 15;
5224 
5225 		hdmi_info.bits.FR0_FR3 = fr_ind & 0xF;
5226 		hdmi_info.bits.FR4 = (fr_ind >> 4) & 0x1;
5227 		hdmi_info.bits.RID0_RID5 = (uint8_t)rid;
5228 	}
5229 
5230 	/* pixel repetition
5231 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
5232 	 * repetition start from 1 */
5233 	hdmi_info.bits.PR0_PR3 = 0;
5234 
5235 	/* Bar Info
5236 	 * barTop:    Line Number of End of Top Bar.
5237 	 * barBottom: Line Number of Start of Bottom Bar.
5238 	 * barLeft:   Pixel Number of End of Left Bar.
5239 	 * barRight:  Pixel Number of Start of Right Bar. */
5240 	hdmi_info.bits.bar_top = (uint16_t)stream->timing.v_border_top;
5241 	hdmi_info.bits.bar_bottom = (uint16_t)(stream->timing.v_total
5242 			- stream->timing.v_border_bottom + 1);
5243 	hdmi_info.bits.bar_left  = (uint16_t)stream->timing.h_border_left;
5244 	hdmi_info.bits.bar_right = (uint16_t)(stream->timing.h_total
5245 			- stream->timing.h_border_right + 1);
5246 
5247     /* Additional Colorimetry Extension
5248      * Used in conduction with C0-C1 and EC0-EC2
5249      * 0 = DCI-P3 RGB (D65)
5250      * 1 = DCI-P3 RGB (theater)
5251      */
5252 	hdmi_info.bits.ACE0_ACE3 = 0;
5253 
5254 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
5255 	check_sum = &hdmi_info.packet_raw_data.sb[0];
5256 
5257 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + hdmi_info.bits.header.length + hdmi_info.bits.header.version;
5258 
5259 	for (byte_index = 1; byte_index <= hdmi_info.bits.header.length; byte_index++)
5260 		*check_sum += hdmi_info.packet_raw_data.sb[byte_index];
5261 
5262 	/* one byte complement */
5263 	*check_sum = (uint8_t)(0x100 - *check_sum);
5264 
5265 	/* Store in hw_path_mode */
5266 	info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
5267 	info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
5268 	info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
5269 
5270 	for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
5271 		info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
5272 
5273 	info_packet->valid = true;
5274 }
5275 
5276 static void set_vendor_info_packet(
5277 		struct dc_info_packet *info_packet,
5278 		struct dc_stream_state *stream)
5279 {
5280 	/* SPD info packet for FreeSync */
5281 
5282 	/* Check if Freesync is supported. Return if false. If true,
5283 	 * set the corresponding bit in the info packet
5284 	 */
5285 	if (!stream->vsp_infopacket.valid)
5286 		return;
5287 
5288 	*info_packet = stream->vsp_infopacket;
5289 }
5290 
5291 static void set_spd_info_packet(
5292 		struct dc_info_packet *info_packet,
5293 		struct dc_stream_state *stream)
5294 {
5295 	/* SPD info packet for FreeSync */
5296 
5297 	/* Check if Freesync is supported. Return if false. If true,
5298 	 * set the corresponding bit in the info packet
5299 	 */
5300 	if (!stream->vrr_infopacket.valid)
5301 		return;
5302 
5303 	*info_packet = stream->vrr_infopacket;
5304 }
5305 
5306 static void set_hdr_static_info_packet(
5307 		struct dc_info_packet *info_packet,
5308 		struct dc_stream_state *stream)
5309 {
5310 	/* HDR Static Metadata info packet for HDR10 */
5311 
5312 	if (!stream->hdr_static_metadata.valid ||
5313 			stream->use_dynamic_meta)
5314 		return;
5315 
5316 	*info_packet = stream->hdr_static_metadata;
5317 }
5318 
5319 static void set_vsc_info_packet(
5320 		struct dc_info_packet *info_packet,
5321 		struct dc_stream_state *stream)
5322 {
5323 	if (!stream->vsc_infopacket.valid)
5324 		return;
5325 
5326 	*info_packet = stream->vsc_infopacket;
5327 }
5328 static void set_hfvs_info_packet(
5329 		struct dc_info_packet *info_packet,
5330 		struct dc_stream_state *stream)
5331 {
5332 	if (!stream->hfvsif_infopacket.valid)
5333 		return;
5334 
5335 	*info_packet = stream->hfvsif_infopacket;
5336 }
5337 
5338 static void adaptive_sync_override_dp_info_packets_sdp_line_num(
5339 		const struct dc_crtc_timing *timing,
5340 		struct enc_sdp_line_num *sdp_line_num,
5341 		unsigned int vstartup_start)
5342 {
5343 	uint32_t asic_blank_start = 0;
5344 	uint32_t asic_blank_end   = 0;
5345 	uint32_t v_update = 0;
5346 
5347 	const struct dc_crtc_timing *tg = timing;
5348 
5349 	/* blank_start = frame end - front porch */
5350 	asic_blank_start = tg->v_total - tg->v_front_porch;
5351 
5352 	/* blank_end = blank_start - active */
5353 	asic_blank_end = (asic_blank_start - tg->v_border_bottom -
5354 						tg->v_addressable - tg->v_border_top);
5355 
5356 	if (vstartup_start > asic_blank_end) {
5357 		v_update = (tg->v_total - (vstartup_start - asic_blank_end));
5358 		sdp_line_num->adaptive_sync_line_num_valid = true;
5359 		sdp_line_num->adaptive_sync_line_num = (tg->v_total - v_update - 1);
5360 	} else {
5361 		sdp_line_num->adaptive_sync_line_num_valid = false;
5362 		sdp_line_num->adaptive_sync_line_num = 0;
5363 	}
5364 }
5365 
5366 static void set_adaptive_sync_info_packet(
5367 		struct dc_info_packet *info_packet,
5368 		const struct dc_stream_state *stream,
5369 		struct encoder_info_frame *info_frame,
5370 		unsigned int vstartup_start)
5371 {
5372 	if (!stream->adaptive_sync_infopacket.valid)
5373 		return;
5374 
5375 	adaptive_sync_override_dp_info_packets_sdp_line_num(
5376 			&stream->timing,
5377 			&info_frame->sdp_line_num,
5378 			vstartup_start);
5379 
5380 	*info_packet = stream->adaptive_sync_infopacket;
5381 }
5382 
5383 static void set_vtem_info_packet(
5384 		struct dc_info_packet *info_packet,
5385 		struct dc_stream_state *stream)
5386 {
5387 	if (!stream->vtem_infopacket.valid)
5388 		return;
5389 
5390 	*info_packet = stream->vtem_infopacket;
5391 }
5392 
5393 struct clock_source *dc_resource_find_first_free_pll(
5394 		struct resource_context *res_ctx,
5395 		const struct resource_pool *pool)
5396 {
5397 	unsigned int i;
5398 
5399 	for (i = 0; i < pool->clk_src_count; ++i) {
5400 		if (res_ctx->clock_source_ref_count[i] == 0)
5401 			return pool->clock_sources[i];
5402 	}
5403 
5404 	return NULL;
5405 }
5406 
5407 void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
5408 {
5409 	enum signal_type signal = SIGNAL_TYPE_NONE;
5410 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
5411 	unsigned int vstartup_start = 0;
5412 
5413 	/* default all packets to invalid */
5414 	info->avi.valid = false;
5415 	info->gamut.valid = false;
5416 	info->vendor.valid = false;
5417 	info->spd.valid = false;
5418 	info->hdrsmd.valid = false;
5419 	info->vsc.valid = false;
5420 	info->hfvsif.valid = false;
5421 	info->vtem.valid = false;
5422 	info->adaptive_sync.valid = false;
5423 	signal = pipe_ctx->stream->signal;
5424 
5425 	if (pipe_ctx->stream->ctx->dc->res_pool->funcs->get_vstartup_for_pipe)
5426 		vstartup_start = pipe_ctx->stream->ctx->dc->res_pool->funcs->get_vstartup_for_pipe(pipe_ctx);
5427 
5428 	/* HDMi and DP have different info packets*/
5429 	if (dc_is_hdmi_signal(signal)) {
5430 		set_avi_info_frame(&info->avi, pipe_ctx);
5431 
5432 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
5433 		set_hfvs_info_packet(&info->hfvsif, pipe_ctx->stream);
5434 		set_vtem_info_packet(&info->vtem, pipe_ctx->stream);
5435 
5436 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
5437 
5438 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
5439 
5440 		if (dc_is_hdmi_frl_signal(signal)) {
5441 			/* TODO: additional packets for HDMI 2.1 */
5442 		}
5443 	} else if (dc_is_dp_signal(signal)) {
5444 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
5445 
5446 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
5447 
5448 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
5449 		set_adaptive_sync_info_packet(&info->adaptive_sync,
5450 										pipe_ctx->stream,
5451 										info,
5452 										vstartup_start);
5453 	}
5454 
5455 	patch_gamut_packet_checksum(&info->gamut);
5456 }
5457 
5458 /*
5459  * Physical-clock resource mapping used by the DCE and DCN resource pools.
5460  * Unit tests provide their own stub (see dc_resource_ut_helpers.c).
5461  */
5462 enum {
5463 	CLK_SRC_PLL0 = 0,
5464 	CLK_SRC_PLL1,
5465 	CLK_SRC_PLL2,
5466 	CLK_SRC_PLL3,
5467 	CLK_SRC_PLL4,
5468 	CLK_SRC_PLL5,
5469 };
5470 
5471 static struct clock_source *find_matching_pll(
5472 		struct resource_context *res_ctx,
5473 		const struct resource_pool *pool,
5474 		const struct dc_stream_state *const stream)
5475 {
5476 	(void)res_ctx;
5477 	switch (stream->link->link_enc->transmitter) {
5478 	case TRANSMITTER_UNIPHY_A:
5479 		return pool->clock_sources[CLK_SRC_PLL0];
5480 	case TRANSMITTER_UNIPHY_B:
5481 		return pool->clock_sources[CLK_SRC_PLL1];
5482 	case TRANSMITTER_UNIPHY_C:
5483 		return pool->clock_sources[CLK_SRC_PLL2];
5484 	case TRANSMITTER_UNIPHY_D:
5485 		return pool->clock_sources[CLK_SRC_PLL3];
5486 	case TRANSMITTER_UNIPHY_E:
5487 		return pool->clock_sources[CLK_SRC_PLL4];
5488 	case TRANSMITTER_UNIPHY_F:
5489 		return pool->clock_sources[CLK_SRC_PLL5];
5490 	default:
5491 		return NULL;
5492 	}
5493 }
5494 
5495 enum dc_status resource_map_phy_clock_resources(
5496 		const struct dc *dc,
5497 		struct dc_state *context,
5498 		struct dc_stream_state *stream)
5499 {
5500 
5501 	/* acquire new resources */
5502 	struct pipe_ctx *pipe_ctx = resource_get_otg_master_for_stream(
5503 			&context->res_ctx, stream);
5504 
5505 	if (!pipe_ctx)
5506 		return DC_ERROR_UNEXPECTED;
5507 
5508 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
5509 		|| dc_is_virtual_signal(pipe_ctx->stream->signal))
5510 		pipe_ctx->clock_source =
5511 				dc->res_pool->dp_clock_source;
5512 	else if (pipe_ctx->stream->signal == SIGNAL_TYPE_HDMI_FRL)
5513 			pipe_ctx->clock_source =
5514 				dc->res_pool->dp_clock_source;
5515 	else {
5516 		if (stream && stream->link && stream->link->link_enc)
5517 			pipe_ctx->clock_source = find_matching_pll(
5518 				&context->res_ctx, dc->res_pool,
5519 				stream);
5520 	}
5521 
5522 	if (pipe_ctx->clock_source == NULL)
5523 		return DC_NO_CLOCK_SOURCE_RESOURCE;
5524 
5525 	resource_reference_clock_source(
5526 		&context->res_ctx,
5527 		dc->res_pool,
5528 		pipe_ctx->clock_source);
5529 
5530 	return DC_OK;
5531 }
5532 
5533 enum dc_status resource_map_clock_resources(
5534 		const struct dc  *dc,
5535 		struct dc_state *context,
5536 		struct dc_stream_state *stream)
5537 {
5538 	/* acquire new resources */
5539 	const struct resource_pool *pool = dc->res_pool;
5540 	struct pipe_ctx *pipe_ctx = resource_get_otg_master_for_stream(
5541 				&context->res_ctx, stream);
5542 
5543 	if (!pipe_ctx)
5544 		return DC_ERROR_UNEXPECTED;
5545 
5546 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
5547 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
5548 		pipe_ctx->clock_source = pool->dp_clock_source;
5549 	else {
5550 		pipe_ctx->clock_source = NULL;
5551 
5552 		if (!dc->config.disable_disp_pll_sharing)
5553 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
5554 				&context->res_ctx,
5555 				pipe_ctx);
5556 
5557 		if (pipe_ctx->clock_source == NULL)
5558 			pipe_ctx->clock_source =
5559 				dc_resource_find_first_free_pll(
5560 					&context->res_ctx,
5561 					pool);
5562 	}
5563 
5564 	if (pipe_ctx->clock_source == NULL)
5565 		return DC_NO_CLOCK_SOURCE_RESOURCE;
5566 
5567 	resource_reference_clock_source(
5568 		&context->res_ctx, pool,
5569 		pipe_ctx->clock_source);
5570 
5571 	return DC_OK;
5572 }
5573 
5574 /*
5575  * Note: We need to disable output if clock sources change,
5576  * since bios does optimization and doesn't apply if changing
5577  * PHY when not already disabled.
5578  */
5579 bool pipe_need_reprogram(
5580 		struct pipe_ctx *pipe_ctx_old,
5581 		struct pipe_ctx *pipe_ctx)
5582 {
5583 	if (!pipe_ctx_old->stream)
5584 		return false;
5585 
5586 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
5587 		return true;
5588 
5589 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
5590 		return true;
5591 
5592 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
5593 		return true;
5594 
5595 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
5596 			&& pipe_ctx_old->stream != pipe_ctx->stream)
5597 		return true;
5598 
5599 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
5600 		return true;
5601 
5602 	if (dc_is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
5603 		return true;
5604 
5605 	if (pipe_ctx_old->stream->dpms_off != pipe_ctx->stream->dpms_off)
5606 		return true;
5607 
5608 	if (false == pipe_ctx_old->stream->link->link_state_valid &&
5609 		false == pipe_ctx_old->stream->dpms_off)
5610 		return true;
5611 
5612 	if (pipe_ctx_old->stream_res.dsc != pipe_ctx->stream_res.dsc)
5613 		return true;
5614 
5615 	if (pipe_ctx_old->stream_res.hpo_frl_stream_enc != pipe_ctx->stream_res.hpo_frl_stream_enc)
5616 		return true;
5617 	if (pipe_ctx_old->stream_res.hpo_dp_stream_enc != pipe_ctx->stream_res.hpo_dp_stream_enc)
5618 		return true;
5619 	if (pipe_ctx_old->link_res.hpo_dp_link_enc != pipe_ctx->link_res.hpo_dp_link_enc)
5620 		return true;
5621 
5622 	/* DIG link encoder resource assignment for stream changed. */
5623 	if (pipe_ctx_old->stream->ctx->dc->config.unify_link_enc_assignment) {
5624 		if (pipe_ctx_old->link_res.dio_link_enc != pipe_ctx->link_res.dio_link_enc)
5625 			return true;
5626 	} else if (pipe_ctx_old->stream->ctx->dc->res_pool->funcs->link_encs_assign) {
5627 		bool need_reprogram = false;
5628 		struct dc *dc = pipe_ctx_old->stream->ctx->dc;
5629 		struct link_encoder *link_enc_prev =
5630 			link_enc_cfg_get_link_enc_used_by_stream_current(dc, pipe_ctx_old->stream);
5631 
5632 		if (link_enc_prev != pipe_ctx->stream->link_enc)
5633 			need_reprogram = true;
5634 
5635 		return need_reprogram;
5636 	}
5637 
5638 	return false;
5639 }
5640 
5641 void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
5642 		struct bit_depth_reduction_params *fmt_bit_depth)
5643 {
5644 	enum dc_dither_option option = stream->dither_option;
5645 	enum dc_pixel_encoding pixel_encoding =
5646 			stream->timing.pixel_encoding;
5647 
5648 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
5649 
5650 	if (option == DITHER_OPTION_DEFAULT) {
5651 		switch (stream->timing.display_color_depth) {
5652 		case COLOR_DEPTH_666:
5653 			option = DITHER_OPTION_SPATIAL6;
5654 			break;
5655 		case COLOR_DEPTH_888:
5656 			option = DITHER_OPTION_SPATIAL8;
5657 			break;
5658 		case COLOR_DEPTH_101010:
5659 			option = DITHER_OPTION_SPATIAL10;
5660 			break;
5661 		default:
5662 			option = DITHER_OPTION_DISABLE;
5663 		}
5664 	}
5665 
5666 	if (stream->ctx->dce_version < DCE_VERSION_8_0 &&
5667 	    stream->timing.display_color_depth >= COLOR_DEPTH_101010) {
5668 		/* DCE 6.x doesn't support 10-bit truncation or dither options. */
5669 		option = DITHER_OPTION_DISABLE;
5670 	}
5671 
5672 	if (option == DITHER_OPTION_DISABLE)
5673 		return;
5674 
5675 	if (option == DITHER_OPTION_TRUN6) {
5676 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
5677 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
5678 	} else if (option == DITHER_OPTION_TRUN8 ||
5679 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
5680 			option == DITHER_OPTION_TRUN8_FM6) {
5681 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
5682 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
5683 	} else if (option == DITHER_OPTION_TRUN10        ||
5684 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
5685 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
5686 			option == DITHER_OPTION_TRUN10_FM8     ||
5687 			option == DITHER_OPTION_TRUN10_FM6     ||
5688 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
5689 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
5690 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
5691 		if (option == DITHER_OPTION_TRUN10)
5692 			fmt_bit_depth->flags.TRUNCATE_MODE = 1;
5693 	}
5694 
5695 	/* special case - Formatter can only reduce by 4 bits at most.
5696 	 * When reducing from 12 to 6 bits,
5697 	 * HW recommends we use trunc with round mode
5698 	 * (if we did nothing, trunc to 10 bits would be used)
5699 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
5700 	 * as the input was 10 bits.
5701 	 */
5702 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
5703 			option == DITHER_OPTION_SPATIAL6 ||
5704 			option == DITHER_OPTION_FM6) {
5705 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
5706 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
5707 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
5708 	}
5709 
5710 	/* spatial dither
5711 	 * note that spatial modes 1-3 are never used
5712 	 */
5713 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
5714 			option == DITHER_OPTION_SPATIAL6 ||
5715 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
5716 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
5717 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
5718 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
5719 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
5720 		fmt_bit_depth->flags.RGB_RANDOM =
5721 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
5722 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
5723 			option == DITHER_OPTION_SPATIAL8 ||
5724 			option == DITHER_OPTION_SPATIAL8_FM6        ||
5725 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
5726 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
5727 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
5728 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
5729 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
5730 		fmt_bit_depth->flags.RGB_RANDOM =
5731 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
5732 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
5733 			option == DITHER_OPTION_SPATIAL10 ||
5734 			option == DITHER_OPTION_SPATIAL10_FM8 ||
5735 			option == DITHER_OPTION_SPATIAL10_FM6) {
5736 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
5737 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
5738 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
5739 		fmt_bit_depth->flags.RGB_RANDOM =
5740 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
5741 	}
5742 
5743 	if (option == DITHER_OPTION_SPATIAL6 ||
5744 			option == DITHER_OPTION_SPATIAL8 ||
5745 			option == DITHER_OPTION_SPATIAL10) {
5746 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
5747 	} else {
5748 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
5749 	}
5750 
5751 	//////////////////////
5752 	//// temporal dither
5753 	//////////////////////
5754 	if (option == DITHER_OPTION_FM6           ||
5755 			option == DITHER_OPTION_SPATIAL8_FM6     ||
5756 			option == DITHER_OPTION_SPATIAL10_FM6     ||
5757 			option == DITHER_OPTION_TRUN10_FM6     ||
5758 			option == DITHER_OPTION_TRUN8_FM6      ||
5759 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
5760 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
5761 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
5762 	} else if (option == DITHER_OPTION_FM8        ||
5763 			option == DITHER_OPTION_SPATIAL10_FM8  ||
5764 			option == DITHER_OPTION_TRUN10_FM8) {
5765 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
5766 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
5767 	} else if (option == DITHER_OPTION_FM10) {
5768 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
5769 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
5770 	}
5771 
5772 	fmt_bit_depth->pixel_encoding = pixel_encoding;
5773 }
5774 
5775 enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
5776 {
5777 	if (dc == NULL || stream == NULL)
5778 		return DC_ERROR_UNEXPECTED;
5779 
5780 	struct dc_link *link = stream->link;
5781 	struct timing_generator *tg = dc->res_pool->timing_generators[0];
5782 	enum dc_status res = DC_OK;
5783 
5784 	calculate_phy_pix_clks(stream);
5785 
5786 	if (!tg->funcs->validate_timing(tg, &stream->timing))
5787 		res = DC_FAIL_CONTROLLER_VALIDATE;
5788 
5789 	if (res == DC_OK) {
5790 		if (link->ep_type == DISPLAY_ENDPOINT_PHY &&
5791 				!link->link_enc->funcs->validate_output_with_stream(
5792 						link->link_enc, stream))
5793 			res = DC_FAIL_ENC_VALIDATE;
5794 	}
5795 
5796 	/* TODO: validate audio ASIC caps, encoder */
5797 
5798 	if (res == DC_OK)
5799 		res = dc->link_srv->validate_mode_timing(stream,
5800 		      link,
5801 		      &stream->timing);
5802 
5803 	return res;
5804 }
5805 
5806 enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
5807 {
5808 	enum dc_status res = DC_OK;
5809 
5810 	/* check if surface has invalid dimensions */
5811 	if (plane_state->src_rect.width == 0 || plane_state->src_rect.height == 0 ||
5812 		plane_state->dst_rect.width == 0 || plane_state->dst_rect.height == 0)
5813 		return DC_FAIL_SURFACE_VALIDATE;
5814 
5815 	/* TODO For now validates pixel format only */
5816 	if (dc->res_pool->funcs->validate_plane)
5817 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
5818 
5819 	return res;
5820 }
5821 
5822 unsigned int resource_pixel_format_to_bpp(enum surface_pixel_format format)
5823 {
5824 	switch (format) {
5825 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
5826 		return 8;
5827 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
5828 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
5829 		return 12;
5830 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
5831 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
5832 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
5833 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
5834 		return 16;
5835 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
5836 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
5837 	case SURFACE_PIXEL_FORMAT_GRPH_RGBA8888:
5838 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
5839 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
5840 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
5841 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE:
5842 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE_ALPHA:
5843 		return 32;
5844 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
5845 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
5846 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
5847 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
5848 		return 64;
5849 	default:
5850 		ASSERT_CRITICAL(false);
5851 		return UINT_MAX;
5852 	}
5853 }
5854 static unsigned int get_max_audio_sample_rate(struct audio_mode *modes)
5855 {
5856 	if (modes) {
5857 		if (modes->sample_rates.rate.RATE_192)
5858 			return 192000;
5859 		if (modes->sample_rates.rate.RATE_176_4)
5860 			return 176400;
5861 		if (modes->sample_rates.rate.RATE_96)
5862 			return 96000;
5863 		if (modes->sample_rates.rate.RATE_88_2)
5864 			return 88200;
5865 		if (modes->sample_rates.rate.RATE_48)
5866 			return 48000;
5867 		if (modes->sample_rates.rate.RATE_44_1)
5868 			return 44100;
5869 		if (modes->sample_rates.rate.RATE_32)
5870 			return 32000;
5871 	}
5872 	/*original logic when no audio info*/
5873 	return 441000;
5874 }
5875 
5876 void get_audio_check(struct audio_info *aud_modes,
5877 	struct audio_check *audio_chk)
5878 {
5879 	unsigned int i;
5880 	unsigned int max_sample_rate = 0;
5881 
5882 	if (aud_modes) {
5883 		audio_chk->audio_packet_type = 0x2;/*audio sample packet AP = .25 for layout0, 1 for layout1*/
5884 
5885 		audio_chk->max_audiosample_rate = 0;
5886 		audio_chk->max_channel_count = 0;
5887 		for (i = 0; i < aud_modes->mode_count; i++) {
5888 			max_sample_rate = get_max_audio_sample_rate(&aud_modes->modes[i]);
5889 			if (audio_chk->max_audiosample_rate < max_sample_rate)
5890 				audio_chk->max_audiosample_rate = max_sample_rate;
5891 			if (audio_chk->max_channel_count < aud_modes->modes[i].channel_count)
5892 				audio_chk->max_channel_count = aud_modes->modes[i].channel_count;
5893 			/*dts takes the same as type 2: AP = 0.25*/
5894 		}
5895 		/*check which one take more bandwidth*/
5896 		if (audio_chk->max_audiosample_rate > 192000)
5897 			audio_chk->audio_packet_type = 0x9;/*AP =1*/
5898 		audio_chk->acat = 0;/*not support*/
5899 	}
5900 }
5901 
5902 struct link_encoder *get_temp_dio_link_enc(
5903 		const struct resource_context *res_ctx,
5904 		const struct resource_pool *const pool,
5905 		const struct dc_link *link)
5906 {
5907 	struct link_encoder *link_enc = NULL;
5908 	int enc_index;
5909 
5910 	if (link->is_dig_mapping_flexible)
5911 		enc_index = find_acquired_dio_link_enc_for_link(res_ctx, link);
5912 	else
5913 		enc_index = link->eng_id;
5914 
5915 	if (enc_index < 0)
5916 		enc_index = find_free_dio_link_enc(res_ctx, link, pool, NULL);
5917 
5918 	if (enc_index >= 0)
5919 		link_enc = pool->link_encoders[enc_index];
5920 
5921 	return link_enc;
5922 }
5923 
5924 static struct hpo_dp_link_encoder *get_temp_hpo_dp_link_enc(
5925 		const struct resource_context *res_ctx,
5926 		const struct resource_pool *const pool,
5927 		const struct dc_link *link)
5928 {
5929 	struct hpo_dp_link_encoder *hpo_dp_link_enc = NULL;
5930 	int enc_index;
5931 
5932 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, link);
5933 
5934 	if (enc_index < 0)
5935 		enc_index = find_free_hpo_dp_link_enc(res_ctx, pool);
5936 
5937 	if (enc_index >= 0)
5938 		hpo_dp_link_enc = pool->hpo_dp_link_enc[enc_index];
5939 
5940 	return hpo_dp_link_enc;
5941 }
5942 
5943 bool get_temp_dp_link_res(struct dc_link *link,
5944 		struct link_resource *link_res,
5945 		struct dc_link_settings *link_settings)
5946 {
5947 	const struct dc *dc  = link->dc;
5948 	const struct resource_context *res_ctx = &dc->current_state->res_ctx;
5949 
5950 	memset(link_res, 0, sizeof(*link_res));
5951 
5952 	if (dc->link_srv->dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING) {
5953 		link_res->hpo_dp_link_enc = get_temp_hpo_dp_link_enc(res_ctx, dc->res_pool, link);
5954 		if (!link_res->hpo_dp_link_enc)
5955 			return false;
5956 	} else if (dc->link_srv->dp_get_encoding_format(link_settings) == DP_8b_10b_ENCODING &&
5957 				dc->config.unify_link_enc_assignment) {
5958 		link_res->dio_link_enc = get_temp_dio_link_enc(res_ctx,
5959 				dc->res_pool, link);
5960 		if (!link_res->dio_link_enc)
5961 			return false;
5962 	}
5963 
5964 	return true;
5965 }
5966 
5967 void reset_syncd_pipes_from_disabled_pipes(struct dc *dc,
5968 		struct dc_state *context)
5969 {
5970 	uint8_t i, j;
5971 	struct pipe_ctx *pipe_ctx_old, *pipe_ctx, *pipe_ctx_syncd;
5972 
5973 	/* If pipe backend is reset, need to reset pipe syncd status */
5974 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5975 		pipe_ctx_old =	&dc->current_state->res_ctx.pipe_ctx[i];
5976 		pipe_ctx = &context->res_ctx.pipe_ctx[i];
5977 
5978 		if (!resource_is_pipe_type(pipe_ctx_old, OTG_MASTER))
5979 			continue;
5980 
5981 		if (!pipe_ctx->stream ||
5982 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
5983 
5984 			/* Reset all the syncd pipes from the disabled pipe */
5985 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
5986 				pipe_ctx_syncd = &context->res_ctx.pipe_ctx[j];
5987 				if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_syncd) == pipe_ctx_old->pipe_idx) ||
5988 					!IS_PIPE_SYNCD_VALID(pipe_ctx_syncd))
5989 					SET_PIPE_SYNCD_TO_PIPE(pipe_ctx_syncd, j);
5990 			}
5991 		}
5992 	}
5993 }
5994 
5995 void check_syncd_pipes_for_disabled_master_pipe(struct dc *dc,
5996 	struct dc_state *context,
5997 	uint8_t disabled_master_pipe_idx)
5998 {
5999 	unsigned int i;
6000 	struct pipe_ctx *pipe_ctx, *pipe_ctx_check;
6001 
6002 	pipe_ctx = &context->res_ctx.pipe_ctx[disabled_master_pipe_idx];
6003 	if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx) != disabled_master_pipe_idx) ||
6004 		!IS_PIPE_SYNCD_VALID(pipe_ctx))
6005 		SET_PIPE_SYNCD_TO_PIPE(pipe_ctx, disabled_master_pipe_idx);
6006 
6007 	/* for the pipe disabled, check if any slave pipe exists and assert */
6008 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
6009 		pipe_ctx_check = &context->res_ctx.pipe_ctx[i];
6010 
6011 		if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_check) == disabled_master_pipe_idx) &&
6012 		    IS_PIPE_SYNCD_VALID(pipe_ctx_check) && (i != disabled_master_pipe_idx)) {
6013 			struct pipe_ctx *first_pipe = pipe_ctx_check;
6014 
6015 			while (first_pipe->prev_odm_pipe)
6016 				first_pipe = first_pipe->prev_odm_pipe;
6017 			/* When ODM combine is enabled, this case is expected. If the disabled pipe
6018 			 * is part of the ODM tree, then we should not print an error.
6019 			 * */
6020 			if (first_pipe->pipe_idx == disabled_master_pipe_idx)
6021 				continue;
6022 
6023 			DC_ERR("DC: Failure: pipe_idx[%d] syncd with disabled master pipe_idx[%d]\n",
6024 				   i, disabled_master_pipe_idx);
6025 		}
6026 	}
6027 }
6028 
6029 void reset_sync_context_for_pipe(const struct dc *dc,
6030 	struct dc_state *context,
6031 	uint8_t pipe_idx)
6032 {
6033 	uint8_t i;
6034 	struct pipe_ctx *pipe_ctx_reset;
6035 
6036 	/* reset the otg sync context for the pipe and its slave pipes if any */
6037 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
6038 		pipe_ctx_reset = &context->res_ctx.pipe_ctx[i];
6039 
6040 		if (((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_reset) == pipe_idx) &&
6041 			IS_PIPE_SYNCD_VALID(pipe_ctx_reset)) || (i == pipe_idx))
6042 			SET_PIPE_SYNCD_TO_PIPE(pipe_ctx_reset, i);
6043 	}
6044 }
6045 
6046 uint8_t resource_transmitter_to_phy_idx(const struct dc *dc, enum transmitter transmitter)
6047 {
6048 	/* TODO - get transmitter to phy idx mapping from DMUB */
6049 	uint8_t phy_idx = (uint8_t)(transmitter - TRANSMITTER_UNIPHY_A);
6050 
6051 	if (dc->ctx->dce_version == DCN_VERSION_3_1 &&
6052 			dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0) {
6053 		switch (transmitter) {
6054 		case TRANSMITTER_UNIPHY_A:
6055 			phy_idx = 0;
6056 			break;
6057 		case TRANSMITTER_UNIPHY_B:
6058 			phy_idx = 1;
6059 			break;
6060 		case TRANSMITTER_UNIPHY_C:
6061 			phy_idx = 5;
6062 			break;
6063 		case TRANSMITTER_UNIPHY_D:
6064 			phy_idx = 6;
6065 			break;
6066 		case TRANSMITTER_UNIPHY_E:
6067 			phy_idx = 4;
6068 			break;
6069 		default:
6070 			phy_idx = 0;
6071 			break;
6072 		}
6073 	}
6074 
6075 	return phy_idx;
6076 }
6077 
6078 const struct link_hwss *get_link_hwss(const struct dc_link *link,
6079 		const struct link_resource *link_res)
6080 {
6081 	/* Link_hwss is only accessible by getter function instead of accessing
6082 	 * by pointers in dc with the intent to protect against breaking polymorphism.
6083 	 */
6084 	if (can_use_hpo_dp_link_hwss(link, link_res))
6085 		/* TODO: some assumes that if decided link settings is 128b/132b
6086 		 * channel coding format hpo_dp_link_enc should be used.
6087 		 * Others believe that if hpo_dp_link_enc is available in link
6088 		 * resource then hpo_dp_link_enc must be used. This bound between
6089 		 * hpo_dp_link_enc != NULL and decided link settings is loosely coupled
6090 		 * with a premise that both hpo_dp_link_enc pointer and decided link
6091 		 * settings are determined based on single policy function like
6092 		 * "decide_link_settings" from upper layer. This "convention"
6093 		 * cannot be maintained and enforced at current level.
6094 		 * Therefore a refactor is due so we can enforce a strong bound
6095 		 * between those two parameters at this level.
6096 		 *
6097 		 * To put it simple, we want to make enforcement at low level so that
6098 		 * we will not return link hwss if caller plans to do 8b/10b
6099 		 * with an hpo encoder. Or we can return a very dummy one that doesn't
6100 		 * do work for all functions
6101 		 */
6102 		return (requires_fixed_vs_pe_retimer_hpo_link_hwss(link) ?
6103 				get_hpo_fixed_vs_pe_retimer_dp_link_hwss() : get_hpo_dp_link_hwss());
6104 	else if (can_use_hpo_frl_link_hwss(link, link_res))
6105 		return get_hpo_frl_link_hwss();
6106 	else if (can_use_dpia_link_hwss(link, link_res))
6107 		return get_dpia_link_hwss();
6108 	else if (can_use_dio_link_hwss(link, link_res))
6109 		return (requires_fixed_vs_pe_retimer_dio_link_hwss(link)) ?
6110 				get_dio_fixed_vs_pe_retimer_link_hwss() : get_dio_link_hwss();
6111 	else
6112 		return get_virtual_link_hwss();
6113 }
6114 
6115 bool is_h_timing_divisible_by_2(struct dc_stream_state *stream)
6116 {
6117 	bool divisible = false;
6118 	uint32_t h_blank_start = 0;
6119 	uint32_t h_blank_end = 0;
6120 
6121 	if (stream) {
6122 		h_blank_start = stream->timing.h_total - stream->timing.h_front_porch;
6123 		h_blank_end = h_blank_start - stream->timing.h_addressable;
6124 
6125 		/* HTOTAL, Hblank start/end, and Hsync start/end all must be
6126 		 * divisible by 2 in order for the horizontal timing params
6127 		 * to be considered divisible by 2. Hsync start is always 0.
6128 		 */
6129 		divisible = (stream->timing.h_total % 2 == 0) &&
6130 				(h_blank_start % 2 == 0) &&
6131 				(h_blank_end % 2 == 0) &&
6132 				(stream->timing.h_sync_width % 2 == 0);
6133 	}
6134 	return divisible;
6135 }
6136 
6137 /* This interface is deprecated for new DCNs. It is replaced by the following
6138  * new interfaces. These two interfaces encapsulate pipe selection priority
6139  * with DCN specific minimum hardware transition optimization algorithm. With
6140  * the new interfaces caller no longer needs to know the implementation detail
6141  * of a pipe topology.
6142  *
6143  * resource_update_pipes_with_odm_slice_count
6144  * resource_update_pipes_with_mpc_slice_count
6145  *
6146  */
6147 bool dc_resource_acquire_secondary_pipe_for_mpc_odm_legacy(
6148 		const struct dc *dc,
6149 		struct dc_state *state,
6150 		struct pipe_ctx *pri_pipe,
6151 		struct pipe_ctx *sec_pipe,
6152 		bool odm)
6153 {
6154 	int pipe_idx = sec_pipe->pipe_idx;
6155 	struct pipe_ctx *sec_top, *sec_bottom, *sec_next, *sec_prev;
6156 	const struct resource_pool *pool = dc->res_pool;
6157 
6158 	sec_top = sec_pipe->top_pipe;
6159 	sec_bottom = sec_pipe->bottom_pipe;
6160 	sec_next = sec_pipe->next_odm_pipe;
6161 	sec_prev = sec_pipe->prev_odm_pipe;
6162 
6163 	if (pri_pipe == NULL)
6164 		return false;
6165 
6166 	*sec_pipe = *pri_pipe;
6167 
6168 	sec_pipe->top_pipe = sec_top;
6169 	sec_pipe->bottom_pipe = sec_bottom;
6170 	sec_pipe->next_odm_pipe = sec_next;
6171 	sec_pipe->prev_odm_pipe = sec_prev;
6172 
6173 	sec_pipe->pipe_idx = (uint8_t)pipe_idx;
6174 	sec_pipe->plane_res.mi = pool->mis[pipe_idx];
6175 	sec_pipe->plane_res.hubp = pool->hubps[pipe_idx];
6176 	sec_pipe->plane_res.ipp = pool->ipps[pipe_idx];
6177 	sec_pipe->plane_res.xfm = pool->transforms[pipe_idx];
6178 	sec_pipe->plane_res.dpp = pool->dpps[pipe_idx];
6179 	sec_pipe->plane_res.mpcc_inst = (uint8_t)pool->dpps[pipe_idx]->inst;
6180 	sec_pipe->stream_res.dsc = NULL;
6181 	if (odm) {
6182 		if (!sec_pipe->top_pipe)
6183 			sec_pipe->stream_res.opp = pool->opps[pipe_idx];
6184 		else
6185 			sec_pipe->stream_res.opp = sec_pipe->top_pipe->stream_res.opp;
6186 		if (sec_pipe->stream->timing.flags.DSC == 1) {
6187 #if defined(CONFIG_DRM_AMD_DC_FP)
6188 			dcn20_acquire_dsc(dc, &state->res_ctx, &sec_pipe->stream_res.dsc, sec_pipe->stream_res.opp->inst);
6189 #endif
6190 			ASSERT(sec_pipe->stream_res.dsc);
6191 			if (sec_pipe->stream_res.dsc == NULL)
6192 				return false;
6193 		}
6194 #if defined(CONFIG_DRM_AMD_DC_FP)
6195 		dcn20_build_mapped_resource(dc, state, sec_pipe->stream);
6196 #endif
6197 	}
6198 
6199 	return true;
6200 }
6201 
6202 enum dc_status update_dp_encoder_resources_for_test_harness(const struct dc *dc,
6203 		struct dc_state *context,
6204 		struct pipe_ctx *pipe_ctx)
6205 {
6206 	if (dc->link_srv->dp_get_encoding_format(&pipe_ctx->link_config.dp_link_settings) == DP_128b_132b_ENCODING) {
6207 		if (pipe_ctx->stream_res.hpo_dp_stream_enc == NULL) {
6208 			pipe_ctx->stream_res.hpo_dp_stream_enc =
6209 					find_first_free_match_hpo_dp_stream_enc_for_link(
6210 							&context->res_ctx, dc->res_pool, pipe_ctx->stream);
6211 
6212 			if (!pipe_ctx->stream_res.hpo_dp_stream_enc)
6213 				return DC_NO_STREAM_ENC_RESOURCE;
6214 
6215 			update_hpo_dp_stream_engine_usage(
6216 					&context->res_ctx, dc->res_pool,
6217 					pipe_ctx->stream_res.hpo_dp_stream_enc,
6218 					true);
6219 		}
6220 
6221 		if (pipe_ctx->link_res.hpo_dp_link_enc == NULL) {
6222 			if (!add_hpo_dp_link_enc_to_ctx(&context->res_ctx, dc->res_pool, pipe_ctx, pipe_ctx->stream))
6223 				return DC_NO_LINK_ENC_RESOURCE;
6224 		}
6225 	} else {
6226 		if (pipe_ctx->stream_res.hpo_dp_stream_enc) {
6227 			update_hpo_dp_stream_engine_usage(
6228 					&context->res_ctx, dc->res_pool,
6229 					pipe_ctx->stream_res.hpo_dp_stream_enc,
6230 					false);
6231 			pipe_ctx->stream_res.hpo_dp_stream_enc = NULL;
6232 		}
6233 		if (pipe_ctx->link_res.hpo_dp_link_enc)
6234 			remove_hpo_dp_link_enc_from_ctx(&context->res_ctx, pipe_ctx, pipe_ctx->stream);
6235 	}
6236 
6237 	if (pipe_ctx->link_res.dio_link_enc == NULL && dc->config.unify_link_enc_assignment)
6238 		if (!add_dio_link_enc_to_ctx(dc, context, dc->res_pool, pipe_ctx, pipe_ctx->stream))
6239 			return DC_NO_LINK_ENC_RESOURCE;
6240 
6241 	return DC_OK;
6242 }
6243 
6244 struct dscl_prog_data *resource_get_dscl_prog_data(struct pipe_ctx *pipe_ctx)
6245 {
6246 	return &pipe_ctx->plane_res.scl_data.dscl_prog_data;
6247 }
6248 
6249 static bool resource_allocate_mcache(struct dc_state *context, const struct  dc_mcache_params *mcache_params)
6250 {
6251 	if (context->clk_mgr->ctx->dc->res_pool->funcs->program_mcache_pipe_config)
6252 		context->clk_mgr->ctx->dc->res_pool->funcs->program_mcache_pipe_config(context, mcache_params);
6253 
6254 	return true;
6255 }
6256 
6257 void resource_init_common_dml2_callbacks(struct dc *dc, struct dml2_configuration_options *dml2_options)
6258 {
6259 	dml2_options->callbacks.dc = dc;
6260 	dml2_options->callbacks.build_scaling_params = &resource_build_scaling_params;
6261 	dml2_options->callbacks.build_test_pattern_params = &resource_build_test_pattern_params;
6262 	dml2_options->callbacks.acquire_secondary_pipe_for_mpc_odm = &dc_resource_acquire_secondary_pipe_for_mpc_odm_legacy;
6263 	dml2_options->callbacks.update_pipes_for_stream_with_slice_count = &resource_update_pipes_for_stream_with_slice_count;
6264 	dml2_options->callbacks.update_pipes_for_plane_with_slice_count = &resource_update_pipes_for_plane_with_slice_count;
6265 	dml2_options->callbacks.get_mpc_slice_index = &resource_get_mpc_slice_index;
6266 	dml2_options->callbacks.get_mpc_slice_count = &resource_get_mpc_slice_count;
6267 	dml2_options->callbacks.get_odm_slice_index = &resource_get_odm_slice_index;
6268 	dml2_options->callbacks.get_odm_slice_count = &resource_get_odm_slice_count;
6269 	dml2_options->callbacks.get_opp_head = &resource_get_opp_head;
6270 	dml2_options->callbacks.get_otg_master_for_stream = &resource_get_otg_master_for_stream;
6271 	dml2_options->callbacks.get_opp_heads_for_otg_master = &resource_get_opp_heads_for_otg_master;
6272 	dml2_options->callbacks.get_dpp_pipes_for_plane = &resource_get_dpp_pipes_for_plane;
6273 	dml2_options->callbacks.get_stream_status = &dc_state_get_stream_status;
6274 	dml2_options->callbacks.get_stream_from_id = &dc_state_get_stream_from_id;
6275 	dml2_options->callbacks.get_max_flickerless_instant_vtotal_increase = &dc_stream_get_max_flickerless_instant_vtotal_increase;
6276 	dml2_options->callbacks.allocate_mcache = &resource_allocate_mcache;
6277 
6278 	dml2_options->svp_pstate.callbacks.dc = dc;
6279 	dml2_options->svp_pstate.callbacks.add_phantom_plane = &dc_state_add_phantom_plane;
6280 	dml2_options->svp_pstate.callbacks.add_phantom_stream = &dc_state_add_phantom_stream;
6281 	dml2_options->svp_pstate.callbacks.build_scaling_params = &resource_build_scaling_params;
6282 	dml2_options->svp_pstate.callbacks.create_phantom_plane = &dc_state_create_phantom_plane;
6283 	dml2_options->svp_pstate.callbacks.remove_phantom_plane = &dc_state_remove_phantom_plane;
6284 	dml2_options->svp_pstate.callbacks.remove_phantom_stream = &dc_state_remove_phantom_stream;
6285 	dml2_options->svp_pstate.callbacks.create_phantom_stream = &dc_state_create_phantom_stream;
6286 	dml2_options->svp_pstate.callbacks.release_phantom_plane = &dc_state_release_phantom_plane;
6287 	dml2_options->svp_pstate.callbacks.release_phantom_stream = &dc_state_release_phantom_stream;
6288 	dml2_options->svp_pstate.callbacks.get_pipe_subvp_type = &dc_state_get_pipe_subvp_type;
6289 	dml2_options->svp_pstate.callbacks.get_stream_subvp_type = &dc_state_get_stream_subvp_type;
6290 	dml2_options->svp_pstate.callbacks.get_paired_subvp_stream = &dc_state_get_paired_subvp_stream;
6291 	dml2_options->svp_pstate.callbacks.remove_phantom_streams_and_planes = &dc_state_remove_phantom_streams_and_planes;
6292 	dml2_options->svp_pstate.callbacks.release_phantom_streams_and_planes = &dc_state_release_phantom_streams_and_planes;
6293 }
6294 
6295 /* Returns number of DET segments allocated for a given OTG_MASTER pipe */
6296 int resource_calculate_det_for_stream(struct dc_state *state, struct pipe_ctx *otg_master)
6297 {
6298 	struct pipe_ctx *opp_heads[MAX_PIPES];
6299 	struct pipe_ctx *dpp_pipes[MAX_PIPES];
6300 
6301 	int dpp_count = 0;
6302 	int det_segments = 0;
6303 
6304 	if (!otg_master->stream)
6305 		return 0;
6306 
6307 	int slice_count = resource_get_opp_heads_for_otg_master(otg_master,
6308 			&state->res_ctx, opp_heads);
6309 
6310 	for (int slice_idx = 0; slice_idx < slice_count; slice_idx++) {
6311 		if (opp_heads[slice_idx]->plane_state) {
6312 			dpp_count = resource_get_dpp_pipes_for_opp_head(
6313 					opp_heads[slice_idx],
6314 					&state->res_ctx,
6315 					dpp_pipes);
6316 			for (int dpp_idx = 0; dpp_idx < dpp_count; dpp_idx++)
6317 				det_segments += dpp_pipes[dpp_idx]->hubp_regs.det_size;
6318 		}
6319 	}
6320 	return det_segments;
6321 }
6322 
6323 bool resource_is_hpo_acquired(struct dc_state *context)
6324 {
6325 	int i;
6326 
6327 	for (i = 0; i < MAX_HPO_DP2_ENCODERS; i++) {
6328 		if (context->res_ctx.is_hpo_dp_stream_enc_acquired[i]) {
6329 			return true;
6330 		}
6331 	}
6332 
6333 	for (i = 0; i < MAX_HDMI_FRL_ENCODERS; i++) {
6334 		if (context->res_ctx.is_hpo_frl_stream_enc_acquired[i]) {
6335 			return true;
6336 		}
6337 	}
6338 
6339 	return false;
6340 }
6341