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