xref: /linux/drivers/gpu/drm/amd/display/dc/core/dc_resource.c (revision 442bc81bd344dc52c37d8f80b854cc6da062b2d0)
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->dst_rect.width ||
1459 			!plane_state->dst_rect.height ||
1460 			!plane_state->src_rect.width ||
1461 			!plane_state->src_rect.height) {
1462 		ASSERT(0);
1463 		return false;
1464 	}
1465 
1466 	/* Timing borders are part of vactive that we are also supposed to skip in addition
1467 	 * to any stream dst offset. Since dm logic assumes dst is in addressable
1468 	 * space we need to add the left and top borders to dst offsets temporarily.
1469 	 * TODO: fix in DM, stream dst is supposed to be in vactive
1470 	 */
1471 	pipe_ctx->stream->dst.x += timing->h_border_left;
1472 	pipe_ctx->stream->dst.y += timing->v_border_top;
1473 
1474 	/* Calculate H and V active size */
1475 	pipe_ctx->plane_res.scl_data.h_active = odm_slice_src.width;
1476 	pipe_ctx->plane_res.scl_data.v_active = odm_slice_src.height;
1477 	pipe_ctx->plane_res.scl_data.format = convert_pixel_format_to_dalsurface(
1478 			pipe_ctx->plane_state->format);
1479 
1480 #if defined(CONFIG_DRM_AMD_DC_FP)
1481 	if ((pipe_ctx->stream->ctx->dc->config.use_spl)	&& (!pipe_ctx->stream->ctx->dc->debug.disable_spl)) {
1482 		struct spl_in *spl_in = &pipe_ctx->plane_res.spl_in;
1483 		struct spl_out *spl_out = &pipe_ctx->plane_res.spl_out;
1484 
1485 		if (plane_state->ctx->dce_version > DCE_VERSION_MAX)
1486 			pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_36BPP;
1487 		else
1488 			pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
1489 
1490 		pipe_ctx->plane_res.scl_data.lb_params.alpha_en = plane_state->per_pixel_alpha;
1491 
1492 		// Convert pipe_ctx to respective input params for SPL
1493 		translate_SPL_in_params_from_pipe_ctx(pipe_ctx, spl_in);
1494 		/* Pass visual confirm debug information */
1495 		calculate_adjust_recout_for_visual_confirm(pipe_ctx,
1496 			&spl_in->debug.visual_confirm_base_offset,
1497 			&spl_in->debug.visual_confirm_dpp_offset);
1498 		// Set SPL output parameters to dscl_prog_data to be used for hw registers
1499 		spl_out->dscl_prog_data = resource_get_dscl_prog_data(pipe_ctx);
1500 		// Calculate scaler parameters from SPL
1501 		res = spl_calculate_scaler_params(spl_in, spl_out);
1502 		// Convert respective out params from SPL to scaler data
1503 		translate_SPL_out_params_to_pipe_ctx(pipe_ctx, spl_out);
1504 
1505 		/* Ignore scaler failure if pipe context plane is phantom plane */
1506 		if (!res && plane_state->is_phantom)
1507 			res = true;
1508 	} else {
1509 #endif
1510 	/* depends on h_active */
1511 	calculate_recout(pipe_ctx);
1512 	/* depends on pixel format */
1513 	calculate_scaling_ratios(pipe_ctx);
1514 
1515 	/*
1516 	 * LB calculations depend on vp size, h/v_active and scaling ratios
1517 	 * Setting line buffer pixel depth to 24bpp yields banding
1518 	 * on certain displays, such as the Sharp 4k. 36bpp is needed
1519 	 * to support SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616 and
1520 	 * SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616 with actual > 10 bpc
1521 	 * precision on DCN display engines, but apparently not for DCE, as
1522 	 * far as testing on DCE-11.2 and DCE-8 showed. Various DCE parts have
1523 	 * problems: Carrizo with DCE_VERSION_11_0 does not like 36 bpp lb depth,
1524 	 * neither do DCE-8 at 4k resolution, or DCE-11.2 (broken identify pixel
1525 	 * passthrough). Therefore only use 36 bpp on DCN where it is actually needed.
1526 	 */
1527 	if (plane_state->ctx->dce_version > DCE_VERSION_MAX)
1528 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_36BPP;
1529 	else
1530 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_30BPP;
1531 
1532 	pipe_ctx->plane_res.scl_data.lb_params.alpha_en = plane_state->per_pixel_alpha;
1533 
1534 	// get TAP value with 100x100 dummy data for max scaling qualify, override
1535 	// if a new scaling quality required
1536 	pipe_ctx->plane_res.scl_data.viewport.width = 100;
1537 	pipe_ctx->plane_res.scl_data.viewport.height = 100;
1538 	pipe_ctx->plane_res.scl_data.viewport_c.width = 100;
1539 	pipe_ctx->plane_res.scl_data.viewport_c.height = 100;
1540 	if (pipe_ctx->plane_res.xfm != NULL)
1541 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1542 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1543 
1544 	if (pipe_ctx->plane_res.dpp != NULL)
1545 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1546 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1547 
1548 	temp = pipe_ctx->plane_res.scl_data.taps;
1549 
1550 	calculate_inits_and_viewports(pipe_ctx);
1551 
1552 	if (pipe_ctx->plane_res.xfm != NULL)
1553 		res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1554 				pipe_ctx->plane_res.xfm, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1555 
1556 	if (pipe_ctx->plane_res.dpp != NULL)
1557 		res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1558 				pipe_ctx->plane_res.dpp, &pipe_ctx->plane_res.scl_data, &plane_state->scaling_quality);
1559 
1560 
1561 	if (!res) {
1562 		/* Try 24 bpp linebuffer */
1563 		pipe_ctx->plane_res.scl_data.lb_params.depth = LB_PIXEL_DEPTH_24BPP;
1564 
1565 		if (pipe_ctx->plane_res.xfm != NULL)
1566 			res = pipe_ctx->plane_res.xfm->funcs->transform_get_optimal_number_of_taps(
1567 					pipe_ctx->plane_res.xfm,
1568 					&pipe_ctx->plane_res.scl_data,
1569 					&plane_state->scaling_quality);
1570 
1571 		if (pipe_ctx->plane_res.dpp != NULL)
1572 			res = pipe_ctx->plane_res.dpp->funcs->dpp_get_optimal_number_of_taps(
1573 					pipe_ctx->plane_res.dpp,
1574 					&pipe_ctx->plane_res.scl_data,
1575 					&plane_state->scaling_quality);
1576 	}
1577 
1578 	/* Ignore scaler failure if pipe context plane is phantom plane */
1579 	if (!res && plane_state->is_phantom)
1580 		res = true;
1581 
1582 	if (res && (pipe_ctx->plane_res.scl_data.taps.v_taps != temp.v_taps ||
1583 		pipe_ctx->plane_res.scl_data.taps.h_taps != temp.h_taps ||
1584 		pipe_ctx->plane_res.scl_data.taps.v_taps_c != temp.v_taps_c ||
1585 		pipe_ctx->plane_res.scl_data.taps.h_taps_c != temp.h_taps_c))
1586 		calculate_inits_and_viewports(pipe_ctx);
1587 
1588 	/*
1589 	 * Handle side by side and top bottom 3d recout offsets after vp calculation
1590 	 * since 3d is special and needs to calculate vp as if there is no recout offset
1591 	 * This may break with rotation, good thing we aren't mixing hw rotation and 3d
1592 	 */
1593 	if (pipe_ctx->top_pipe && pipe_ctx->top_pipe->plane_state == plane_state) {
1594 		ASSERT(plane_state->rotation == ROTATION_ANGLE_0 ||
1595 			(pipe_ctx->stream->view_format != VIEW_3D_FORMAT_TOP_AND_BOTTOM &&
1596 				pipe_ctx->stream->view_format != VIEW_3D_FORMAT_SIDE_BY_SIDE));
1597 		if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_TOP_AND_BOTTOM)
1598 			pipe_ctx->plane_res.scl_data.recout.y += pipe_ctx->plane_res.scl_data.recout.height;
1599 		else if (pipe_ctx->stream->view_format == VIEW_3D_FORMAT_SIDE_BY_SIDE)
1600 			pipe_ctx->plane_res.scl_data.recout.x += pipe_ctx->plane_res.scl_data.recout.width;
1601 	}
1602 
1603 	/* Clamp minimum viewport size */
1604 	if (pipe_ctx->plane_res.scl_data.viewport.height < MIN_VIEWPORT_SIZE)
1605 		pipe_ctx->plane_res.scl_data.viewport.height = MIN_VIEWPORT_SIZE;
1606 	if (pipe_ctx->plane_res.scl_data.viewport.width < MIN_VIEWPORT_SIZE)
1607 		pipe_ctx->plane_res.scl_data.viewport.width = MIN_VIEWPORT_SIZE;
1608 #ifdef CONFIG_DRM_AMD_DC_FP
1609 	}
1610 #endif
1611 	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"
1612 			"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",
1613 			__func__,
1614 			pipe_ctx->pipe_idx,
1615 			pipe_ctx->plane_res.scl_data.viewport.height,
1616 			pipe_ctx->plane_res.scl_data.viewport.width,
1617 			pipe_ctx->plane_res.scl_data.viewport.x,
1618 			pipe_ctx->plane_res.scl_data.viewport.y,
1619 			pipe_ctx->plane_res.scl_data.recout.height,
1620 			pipe_ctx->plane_res.scl_data.recout.width,
1621 			pipe_ctx->plane_res.scl_data.recout.x,
1622 			pipe_ctx->plane_res.scl_data.recout.y,
1623 			pipe_ctx->plane_res.scl_data.h_active,
1624 			pipe_ctx->plane_res.scl_data.v_active,
1625 			plane_state->src_rect.height,
1626 			plane_state->src_rect.width,
1627 			plane_state->src_rect.x,
1628 			plane_state->src_rect.y,
1629 			plane_state->dst_rect.height,
1630 			plane_state->dst_rect.width,
1631 			plane_state->dst_rect.x,
1632 			plane_state->dst_rect.y,
1633 			plane_state->clip_rect.height,
1634 			plane_state->clip_rect.width,
1635 			plane_state->clip_rect.x,
1636 			plane_state->clip_rect.y);
1637 
1638 	pipe_ctx->stream->dst.x -= timing->h_border_left;
1639 	pipe_ctx->stream->dst.y -= timing->v_border_top;
1640 
1641 	return res;
1642 }
1643 
1644 
resource_build_scaling_params_for_context(const struct dc * dc,struct dc_state * context)1645 enum dc_status resource_build_scaling_params_for_context(
1646 	const struct dc  *dc,
1647 	struct dc_state *context)
1648 {
1649 	int i;
1650 
1651 	for (i = 0; i < MAX_PIPES; i++) {
1652 		if (context->res_ctx.pipe_ctx[i].plane_state != NULL &&
1653 				context->res_ctx.pipe_ctx[i].stream != NULL)
1654 			if (!resource_build_scaling_params(&context->res_ctx.pipe_ctx[i]))
1655 				return DC_FAIL_SCALING;
1656 	}
1657 
1658 	return DC_OK;
1659 }
1660 
resource_find_free_secondary_pipe_legacy(struct resource_context * res_ctx,const struct resource_pool * pool,const struct pipe_ctx * primary_pipe)1661 struct pipe_ctx *resource_find_free_secondary_pipe_legacy(
1662 		struct resource_context *res_ctx,
1663 		const struct resource_pool *pool,
1664 		const struct pipe_ctx *primary_pipe)
1665 {
1666 	int i;
1667 	struct pipe_ctx *secondary_pipe = NULL;
1668 
1669 	/*
1670 	 * We add a preferred pipe mapping to avoid the chance that
1671 	 * MPCCs already in use will need to be reassigned to other trees.
1672 	 * For example, if we went with the strict, assign backwards logic:
1673 	 *
1674 	 * (State 1)
1675 	 * Display A on, no surface, top pipe = 0
1676 	 * Display B on, no surface, top pipe = 1
1677 	 *
1678 	 * (State 2)
1679 	 * Display A on, no surface, top pipe = 0
1680 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 5
1681 	 *
1682 	 * (State 3)
1683 	 * Display A on, surface enable, top pipe = 0, bottom pipe = 5
1684 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1685 	 *
1686 	 * The state 2->3 transition requires remapping MPCC 5 from display B
1687 	 * to display A.
1688 	 *
1689 	 * However, with the preferred pipe logic, state 2 would look like:
1690 	 *
1691 	 * (State 2)
1692 	 * Display A on, no surface, top pipe = 0
1693 	 * Display B on, surface enable, top pipe = 1, bottom pipe = 4
1694 	 *
1695 	 * This would then cause 2->3 to not require remapping any MPCCs.
1696 	 */
1697 	if (primary_pipe) {
1698 		int preferred_pipe_idx = (pool->pipe_count - 1) - primary_pipe->pipe_idx;
1699 		if (res_ctx->pipe_ctx[preferred_pipe_idx].stream == NULL) {
1700 			secondary_pipe = &res_ctx->pipe_ctx[preferred_pipe_idx];
1701 			secondary_pipe->pipe_idx = preferred_pipe_idx;
1702 		}
1703 	}
1704 
1705 	/*
1706 	 * search backwards for the second pipe to keep pipe
1707 	 * assignment more consistent
1708 	 */
1709 	if (!secondary_pipe)
1710 		for (i = pool->pipe_count - 1; i >= 0; i--) {
1711 			if (res_ctx->pipe_ctx[i].stream == NULL) {
1712 				secondary_pipe = &res_ctx->pipe_ctx[i];
1713 				secondary_pipe->pipe_idx = i;
1714 				break;
1715 			}
1716 		}
1717 
1718 	return secondary_pipe;
1719 }
1720 
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)1721 int resource_find_free_pipe_used_as_sec_opp_head_by_cur_otg_master(
1722 		const struct resource_context *cur_res_ctx,
1723 		struct resource_context *new_res_ctx,
1724 		const struct pipe_ctx *cur_otg_master)
1725 {
1726 	const struct pipe_ctx *cur_sec_opp_head = cur_otg_master->next_odm_pipe;
1727 	struct pipe_ctx *new_pipe;
1728 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1729 
1730 	while (cur_sec_opp_head) {
1731 		new_pipe = &new_res_ctx->pipe_ctx[cur_sec_opp_head->pipe_idx];
1732 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1733 			free_pipe_idx = cur_sec_opp_head->pipe_idx;
1734 			break;
1735 		}
1736 		cur_sec_opp_head = cur_sec_opp_head->next_odm_pipe;
1737 	}
1738 
1739 	return free_pipe_idx;
1740 }
1741 
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)1742 int resource_find_free_pipe_used_in_cur_mpc_blending_tree(
1743 		const struct resource_context *cur_res_ctx,
1744 		struct resource_context *new_res_ctx,
1745 		const struct pipe_ctx *cur_opp_head)
1746 {
1747 	const struct pipe_ctx *cur_sec_dpp = cur_opp_head->bottom_pipe;
1748 	struct pipe_ctx *new_pipe;
1749 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1750 
1751 	while (cur_sec_dpp) {
1752 		/* find a free pipe used in current opp blend tree,
1753 		 * this is to avoid MPO pipe switching to different opp blending
1754 		 * tree
1755 		 */
1756 		new_pipe = &new_res_ctx->pipe_ctx[cur_sec_dpp->pipe_idx];
1757 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1758 			free_pipe_idx = cur_sec_dpp->pipe_idx;
1759 			break;
1760 		}
1761 		cur_sec_dpp = cur_sec_dpp->bottom_pipe;
1762 	}
1763 
1764 	return free_pipe_idx;
1765 }
1766 
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)1767 int recource_find_free_pipe_not_used_in_cur_res_ctx(
1768 		const struct resource_context *cur_res_ctx,
1769 		struct resource_context *new_res_ctx,
1770 		const struct resource_pool *pool)
1771 {
1772 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1773 	const struct pipe_ctx *new_pipe, *cur_pipe;
1774 	int i;
1775 
1776 	for (i = 0; i < pool->pipe_count; i++) {
1777 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1778 		new_pipe = &new_res_ctx->pipe_ctx[i];
1779 
1780 		if (resource_is_pipe_type(cur_pipe, FREE_PIPE) &&
1781 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1782 			free_pipe_idx = i;
1783 			break;
1784 		}
1785 	}
1786 
1787 	return free_pipe_idx;
1788 }
1789 
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)1790 int recource_find_free_pipe_used_as_otg_master_in_cur_res_ctx(
1791 		const struct resource_context *cur_res_ctx,
1792 		struct resource_context *new_res_ctx,
1793 		const struct resource_pool *pool)
1794 {
1795 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1796 	const struct pipe_ctx *new_pipe, *cur_pipe;
1797 	int i;
1798 
1799 	for (i = 0; i < pool->pipe_count; i++) {
1800 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1801 		new_pipe = &new_res_ctx->pipe_ctx[i];
1802 
1803 		if (resource_is_pipe_type(cur_pipe, OTG_MASTER) &&
1804 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1805 			free_pipe_idx = i;
1806 			break;
1807 		}
1808 	}
1809 
1810 	return free_pipe_idx;
1811 }
1812 
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)1813 int resource_find_free_pipe_used_as_cur_sec_dpp(
1814 		const struct resource_context *cur_res_ctx,
1815 		struct resource_context *new_res_ctx,
1816 		const struct resource_pool *pool)
1817 {
1818 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1819 	const struct pipe_ctx *new_pipe, *cur_pipe;
1820 	int i;
1821 
1822 	for (i = 0; i < pool->pipe_count; i++) {
1823 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1824 		new_pipe = &new_res_ctx->pipe_ctx[i];
1825 
1826 		if (resource_is_pipe_type(cur_pipe, DPP_PIPE) &&
1827 				!resource_is_pipe_type(cur_pipe, OPP_HEAD) &&
1828 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1829 			free_pipe_idx = i;
1830 			break;
1831 		}
1832 	}
1833 
1834 	return free_pipe_idx;
1835 }
1836 
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)1837 int resource_find_free_pipe_used_as_cur_sec_dpp_in_mpcc_combine(
1838 		const struct resource_context *cur_res_ctx,
1839 		struct resource_context *new_res_ctx,
1840 		const struct resource_pool *pool)
1841 {
1842 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1843 	const struct pipe_ctx *new_pipe, *cur_pipe;
1844 	int i;
1845 
1846 	for (i = 0; i < pool->pipe_count; i++) {
1847 		cur_pipe = &cur_res_ctx->pipe_ctx[i];
1848 		new_pipe = &new_res_ctx->pipe_ctx[i];
1849 
1850 		if (resource_is_pipe_type(cur_pipe, DPP_PIPE) &&
1851 				!resource_is_pipe_type(cur_pipe, OPP_HEAD) &&
1852 				resource_get_mpc_slice_index(cur_pipe) > 0 &&
1853 				resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1854 			free_pipe_idx = i;
1855 			break;
1856 		}
1857 	}
1858 
1859 	return free_pipe_idx;
1860 }
1861 
resource_find_any_free_pipe(struct resource_context * new_res_ctx,const struct resource_pool * pool)1862 int resource_find_any_free_pipe(struct resource_context *new_res_ctx,
1863 		const struct resource_pool *pool)
1864 {
1865 	int free_pipe_idx = FREE_PIPE_INDEX_NOT_FOUND;
1866 	const struct pipe_ctx *new_pipe;
1867 	int i;
1868 
1869 	for (i = 0; i < pool->pipe_count; i++) {
1870 		new_pipe = &new_res_ctx->pipe_ctx[i];
1871 
1872 		if (resource_is_pipe_type(new_pipe, FREE_PIPE)) {
1873 			free_pipe_idx = i;
1874 			break;
1875 		}
1876 	}
1877 
1878 	return free_pipe_idx;
1879 }
1880 
resource_is_pipe_type(const struct pipe_ctx * pipe_ctx,enum pipe_type type)1881 bool resource_is_pipe_type(const struct pipe_ctx *pipe_ctx, enum pipe_type type)
1882 {
1883 	switch (type) {
1884 	case OTG_MASTER:
1885 		return !pipe_ctx->prev_odm_pipe &&
1886 				!pipe_ctx->top_pipe &&
1887 				pipe_ctx->stream;
1888 	case OPP_HEAD:
1889 		return !pipe_ctx->top_pipe && pipe_ctx->stream;
1890 	case DPP_PIPE:
1891 		return pipe_ctx->plane_state && pipe_ctx->stream;
1892 	case FREE_PIPE:
1893 		return !pipe_ctx->plane_state && !pipe_ctx->stream;
1894 	default:
1895 		return false;
1896 	}
1897 }
1898 
resource_get_otg_master_for_stream(struct resource_context * res_ctx,const struct dc_stream_state * stream)1899 struct pipe_ctx *resource_get_otg_master_for_stream(
1900 		struct resource_context *res_ctx,
1901 		const struct dc_stream_state *stream)
1902 {
1903 	int i;
1904 
1905 	for (i = 0; i < MAX_PIPES; i++) {
1906 		if (res_ctx->pipe_ctx[i].stream == stream &&
1907 				resource_is_pipe_type(&res_ctx->pipe_ctx[i], OTG_MASTER))
1908 			return &res_ctx->pipe_ctx[i];
1909 	}
1910 	return NULL;
1911 }
1912 
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])1913 int resource_get_opp_heads_for_otg_master(const struct pipe_ctx *otg_master,
1914 		struct resource_context *res_ctx,
1915 		struct pipe_ctx *opp_heads[MAX_PIPES])
1916 {
1917 	struct pipe_ctx *opp_head = &res_ctx->pipe_ctx[otg_master->pipe_idx];
1918 	struct dc *dc = otg_master->stream->ctx->dc;
1919 	int i = 0;
1920 
1921 	DC_LOGGER_INIT(dc->ctx->logger);
1922 
1923 	if (!resource_is_pipe_type(otg_master, OTG_MASTER)) {
1924 		DC_LOG_WARNING("%s called from a non OTG master, something "
1925 			       "is wrong in the pipe configuration",
1926 			       __func__);
1927 		ASSERT(0);
1928 		return 0;
1929 	}
1930 	while (opp_head) {
1931 		ASSERT(i < MAX_PIPES);
1932 		opp_heads[i++] = opp_head;
1933 		opp_head = opp_head->next_odm_pipe;
1934 	}
1935 	return i;
1936 }
1937 
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])1938 int resource_get_dpp_pipes_for_opp_head(const struct pipe_ctx *opp_head,
1939 		struct resource_context *res_ctx,
1940 		struct pipe_ctx *dpp_pipes[MAX_PIPES])
1941 {
1942 	struct pipe_ctx *pipe = &res_ctx->pipe_ctx[opp_head->pipe_idx];
1943 	int i = 0;
1944 
1945 	if (!resource_is_pipe_type(opp_head, OPP_HEAD)) {
1946 		ASSERT(0);
1947 		return 0;
1948 	}
1949 	while (pipe && resource_is_pipe_type(pipe, DPP_PIPE)) {
1950 		ASSERT(i < MAX_PIPES);
1951 		dpp_pipes[i++] = pipe;
1952 		pipe = pipe->bottom_pipe;
1953 	}
1954 	return i;
1955 }
1956 
resource_get_dpp_pipes_for_plane(const struct dc_plane_state * plane,struct resource_context * res_ctx,struct pipe_ctx * dpp_pipes[MAX_PIPES])1957 int resource_get_dpp_pipes_for_plane(const struct dc_plane_state *plane,
1958 		struct resource_context *res_ctx,
1959 		struct pipe_ctx *dpp_pipes[MAX_PIPES])
1960 {
1961 	int i = 0, j;
1962 	struct pipe_ctx *pipe;
1963 
1964 	for (j = 0; j < MAX_PIPES; j++) {
1965 		pipe = &res_ctx->pipe_ctx[j];
1966 		if (pipe->plane_state == plane && pipe->prev_odm_pipe == NULL) {
1967 			if (resource_is_pipe_type(pipe, OPP_HEAD) ||
1968 					pipe->top_pipe->plane_state != plane)
1969 				break;
1970 		}
1971 	}
1972 
1973 	if (j < MAX_PIPES) {
1974 		if (pipe->next_odm_pipe)
1975 			while (pipe) {
1976 				dpp_pipes[i++] = pipe;
1977 				pipe = pipe->next_odm_pipe;
1978 			}
1979 		else
1980 			while (pipe && pipe->plane_state == plane) {
1981 				dpp_pipes[i++] = pipe;
1982 				pipe = pipe->bottom_pipe;
1983 			}
1984 	}
1985 	return i;
1986 }
1987 
resource_get_otg_master(const struct pipe_ctx * pipe_ctx)1988 struct pipe_ctx *resource_get_otg_master(const struct pipe_ctx *pipe_ctx)
1989 {
1990 	struct pipe_ctx *otg_master = resource_get_opp_head(pipe_ctx);
1991 
1992 	while (otg_master->prev_odm_pipe)
1993 		otg_master = otg_master->prev_odm_pipe;
1994 	return otg_master;
1995 }
1996 
resource_get_opp_head(const struct pipe_ctx * pipe_ctx)1997 struct pipe_ctx *resource_get_opp_head(const struct pipe_ctx *pipe_ctx)
1998 {
1999 	struct pipe_ctx *opp_head = (struct pipe_ctx *) pipe_ctx;
2000 
2001 	ASSERT(!resource_is_pipe_type(opp_head, FREE_PIPE));
2002 	while (opp_head->top_pipe)
2003 		opp_head = opp_head->top_pipe;
2004 	return opp_head;
2005 }
2006 
resource_get_primary_dpp_pipe(const struct pipe_ctx * dpp_pipe)2007 struct pipe_ctx *resource_get_primary_dpp_pipe(const struct pipe_ctx *dpp_pipe)
2008 {
2009 	struct pipe_ctx *pri_dpp_pipe = (struct pipe_ctx *) dpp_pipe;
2010 
2011 	ASSERT(resource_is_pipe_type(dpp_pipe, DPP_PIPE));
2012 	while (pri_dpp_pipe->prev_odm_pipe)
2013 		pri_dpp_pipe = pri_dpp_pipe->prev_odm_pipe;
2014 	while (pri_dpp_pipe->top_pipe &&
2015 			pri_dpp_pipe->top_pipe->plane_state == pri_dpp_pipe->plane_state)
2016 		pri_dpp_pipe = pri_dpp_pipe->top_pipe;
2017 	return pri_dpp_pipe;
2018 }
2019 
2020 
resource_get_mpc_slice_index(const struct pipe_ctx * pipe_ctx)2021 int resource_get_mpc_slice_index(const struct pipe_ctx *pipe_ctx)
2022 {
2023 	struct pipe_ctx *split_pipe = pipe_ctx->top_pipe;
2024 	int index = 0;
2025 
2026 	while (split_pipe && split_pipe->plane_state == pipe_ctx->plane_state) {
2027 		index++;
2028 		split_pipe = split_pipe->top_pipe;
2029 	}
2030 
2031 	return index;
2032 }
2033 
resource_get_mpc_slice_count(const struct pipe_ctx * pipe)2034 int resource_get_mpc_slice_count(const struct pipe_ctx *pipe)
2035 {
2036 	int mpc_split_count = 1;
2037 	const struct pipe_ctx *other_pipe = pipe->bottom_pipe;
2038 
2039 	while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
2040 		mpc_split_count++;
2041 		other_pipe = other_pipe->bottom_pipe;
2042 	}
2043 	other_pipe = pipe->top_pipe;
2044 	while (other_pipe && other_pipe->plane_state == pipe->plane_state) {
2045 		mpc_split_count++;
2046 		other_pipe = other_pipe->top_pipe;
2047 	}
2048 
2049 	return mpc_split_count;
2050 }
2051 
resource_get_odm_slice_count(const struct pipe_ctx * pipe)2052 int resource_get_odm_slice_count(const struct pipe_ctx *pipe)
2053 {
2054 	int odm_split_count = 1;
2055 
2056 	pipe = resource_get_otg_master(pipe);
2057 
2058 	while (pipe->next_odm_pipe) {
2059 		odm_split_count++;
2060 		pipe = pipe->next_odm_pipe;
2061 	}
2062 	return odm_split_count;
2063 }
2064 
resource_get_odm_slice_index(const struct pipe_ctx * pipe_ctx)2065 int resource_get_odm_slice_index(const struct pipe_ctx *pipe_ctx)
2066 {
2067 	int index = 0;
2068 
2069 	pipe_ctx = resource_get_opp_head(pipe_ctx);
2070 	if (!pipe_ctx)
2071 		return 0;
2072 
2073 	while (pipe_ctx->prev_odm_pipe) {
2074 		index++;
2075 		pipe_ctx = pipe_ctx->prev_odm_pipe;
2076 	}
2077 
2078 	return index;
2079 }
2080 
resource_get_odm_slice_dst_width(struct pipe_ctx * otg_master,bool is_last_segment)2081 int resource_get_odm_slice_dst_width(struct pipe_ctx *otg_master,
2082 		bool is_last_segment)
2083 {
2084 	const struct dc_crtc_timing *timing;
2085 	int count;
2086 	int h_active;
2087 	int width;
2088 	bool two_pixel_alignment_required = false;
2089 
2090 	if (!otg_master || !otg_master->stream)
2091 		return 0;
2092 
2093 	timing = &otg_master->stream->timing;
2094 	count = resource_get_odm_slice_count(otg_master);
2095 	h_active = timing->h_addressable +
2096 			timing->h_border_left +
2097 			timing->h_border_right +
2098 			otg_master->hblank_borrow;
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_161616:
3393 			normalized_pix_clk = (pix_clk * 48) / 24;
3394 		break;
3395 		default:
3396 			ASSERT(0);
3397 		break;
3398 		}
3399 	}
3400 	return normalized_pix_clk;
3401 }
3402 
calculate_phy_pix_clks(struct dc_stream_state * stream)3403 static void calculate_phy_pix_clks(struct dc_stream_state *stream)
3404 {
3405 	/* update actual pixel clock on all streams */
3406 	if (dc_is_hdmi_signal(stream->signal))
3407 		stream->phy_pix_clk = get_norm_pix_clk(
3408 			&stream->timing) / 10;
3409 	else
3410 		stream->phy_pix_clk =
3411 			stream->timing.pix_clk_100hz / 10;
3412 
3413 	if (stream->timing.timing_3d_format == TIMING_3D_FORMAT_HW_FRAME_PACKING)
3414 		stream->phy_pix_clk *= 2;
3415 }
3416 
acquire_resource_from_hw_enabled_state(struct resource_context * res_ctx,const struct resource_pool * pool,struct dc_stream_state * stream)3417 static int acquire_resource_from_hw_enabled_state(
3418 		struct resource_context *res_ctx,
3419 		const struct resource_pool *pool,
3420 		struct dc_stream_state *stream)
3421 {
3422 	struct dc_link *link = stream->link;
3423 	unsigned int i, inst, tg_inst = 0;
3424 	uint32_t numPipes = 1;
3425 	uint32_t id_src[4] = {0};
3426 
3427 	/* Check for enabled DIG to identify enabled display */
3428 	if (!link->link_enc->funcs->is_dig_enabled(link->link_enc))
3429 		return -1;
3430 
3431 	inst = link->link_enc->funcs->get_dig_frontend(link->link_enc);
3432 
3433 	if (inst == ENGINE_ID_UNKNOWN)
3434 		return -1;
3435 
3436 	for (i = 0; i < pool->stream_enc_count; i++) {
3437 		if (pool->stream_enc[i]->id == inst) {
3438 			tg_inst = pool->stream_enc[i]->funcs->dig_source_otg(
3439 				pool->stream_enc[i]);
3440 			break;
3441 		}
3442 	}
3443 
3444 	// tg_inst not found
3445 	if (i == pool->stream_enc_count)
3446 		return -1;
3447 
3448 	if (tg_inst >= pool->timing_generator_count)
3449 		return -1;
3450 
3451 	if (!res_ctx->pipe_ctx[tg_inst].stream) {
3452 		struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[tg_inst];
3453 
3454 		pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
3455 		id_src[0] = tg_inst;
3456 
3457 		if (pipe_ctx->stream_res.tg->funcs->get_optc_source)
3458 			pipe_ctx->stream_res.tg->funcs->get_optc_source(pipe_ctx->stream_res.tg,
3459 						&numPipes, &id_src[0], &id_src[1]);
3460 
3461 		if (id_src[0] == 0xf && id_src[1] == 0xf) {
3462 			id_src[0] = tg_inst;
3463 			numPipes = 1;
3464 		}
3465 
3466 		for (i = 0; i < numPipes; i++) {
3467 			//Check if src id invalid
3468 			if (id_src[i] == 0xf)
3469 				return -1;
3470 
3471 			pipe_ctx = &res_ctx->pipe_ctx[id_src[i]];
3472 
3473 			pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
3474 			pipe_ctx->plane_res.mi = pool->mis[id_src[i]];
3475 			pipe_ctx->plane_res.hubp = pool->hubps[id_src[i]];
3476 			pipe_ctx->plane_res.ipp = pool->ipps[id_src[i]];
3477 			pipe_ctx->plane_res.xfm = pool->transforms[id_src[i]];
3478 			pipe_ctx->plane_res.dpp = pool->dpps[id_src[i]];
3479 			pipe_ctx->stream_res.opp = pool->opps[id_src[i]];
3480 
3481 			if (pool->dpps[id_src[i]]) {
3482 				pipe_ctx->plane_res.mpcc_inst = pool->dpps[id_src[i]]->inst;
3483 
3484 				if (pool->mpc->funcs->read_mpcc_state) {
3485 					struct mpcc_state s = {0};
3486 
3487 					pool->mpc->funcs->read_mpcc_state(pool->mpc, pipe_ctx->plane_res.mpcc_inst, &s);
3488 
3489 					if (s.dpp_id < MAX_MPCC)
3490 						pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].dpp_id =
3491 								s.dpp_id;
3492 
3493 					if (s.bot_mpcc_id < MAX_MPCC)
3494 						pool->mpc->mpcc_array[pipe_ctx->plane_res.mpcc_inst].mpcc_bot =
3495 								&pool->mpc->mpcc_array[s.bot_mpcc_id];
3496 
3497 					if (s.opp_id < MAX_OPP)
3498 						pipe_ctx->stream_res.opp->mpc_tree_params.opp_id = s.opp_id;
3499 				}
3500 			}
3501 			pipe_ctx->pipe_idx = id_src[i];
3502 
3503 			if (id_src[i] >= pool->timing_generator_count) {
3504 				id_src[i] = pool->timing_generator_count - 1;
3505 
3506 				pipe_ctx->stream_res.tg = pool->timing_generators[id_src[i]];
3507 				pipe_ctx->stream_res.opp = pool->opps[id_src[i]];
3508 			}
3509 
3510 			pipe_ctx->stream = stream;
3511 		}
3512 
3513 		if (numPipes == 2) {
3514 			stream->apply_boot_odm_mode = dm_odm_combine_policy_2to1;
3515 			res_ctx->pipe_ctx[id_src[0]].next_odm_pipe = &res_ctx->pipe_ctx[id_src[1]];
3516 			res_ctx->pipe_ctx[id_src[0]].prev_odm_pipe = NULL;
3517 			res_ctx->pipe_ctx[id_src[1]].next_odm_pipe = NULL;
3518 			res_ctx->pipe_ctx[id_src[1]].prev_odm_pipe = &res_ctx->pipe_ctx[id_src[0]];
3519 		} else
3520 			stream->apply_boot_odm_mode = dm_odm_combine_mode_disabled;
3521 
3522 		return id_src[0];
3523 	}
3524 
3525 	return -1;
3526 }
3527 
mark_seamless_boot_stream(const struct dc * dc,struct dc_stream_state * stream)3528 static void mark_seamless_boot_stream(
3529 		const struct dc  *dc,
3530 		struct dc_stream_state *stream)
3531 {
3532 	struct dc_bios *dcb = dc->ctx->dc_bios;
3533 
3534 	if (dc->config.allow_seamless_boot_optimization &&
3535 			!dcb->funcs->is_accelerated_mode(dcb)) {
3536 		if (dc_validate_boot_timing(dc, stream->sink, &stream->timing))
3537 			stream->apply_seamless_boot_optimization = true;
3538 	}
3539 }
3540 
3541 /*
3542  * Acquire a pipe as OTG master and assign to the stream in new dc context.
3543  * return - true if OTG master pipe is acquired and new dc context is updated.
3544  * false if it fails to acquire an OTG master pipe for this stream.
3545  *
3546  * In the example below, we acquired pipe 0 as OTG master pipe for the stream.
3547  * After the function its Inter-pipe Relation is represented by the diagram
3548  * below.
3549  *
3550  *       Inter-pipe Relation
3551  *        __________________________________________________
3552  *       |PIPE IDX|   DPP PIPES   | OPP HEADS | OTG MASTER  |
3553  *       |        |               |           |             |
3554  *       |   0    |               |blank ------------------ |
3555  *       |________|_______________|___________|_____________|
3556  */
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)3557 static bool acquire_otg_master_pipe_for_stream(
3558 		const struct dc_state *cur_ctx,
3559 		struct dc_state *new_ctx,
3560 		const struct resource_pool *pool,
3561 		struct dc_stream_state *stream)
3562 {
3563 	/* TODO: Move this function to DCN specific resource file and acquire
3564 	 * DSC resource here. The reason is that the function should have the
3565 	 * same level of responsibility as when we acquire secondary OPP head.
3566 	 * We acquire DSC when we acquire secondary OPP head, so we should
3567 	 * acquire DSC when we acquire OTG master.
3568 	 */
3569 	int pipe_idx;
3570 	struct pipe_ctx *pipe_ctx = NULL;
3571 
3572 	/*
3573 	 * Upper level code is responsible to optimize unnecessary addition and
3574 	 * removal for unchanged streams. So unchanged stream will keep the same
3575 	 * OTG master instance allocated. When current stream is removed and a
3576 	 * new stream is added, we want to reuse the OTG instance made available
3577 	 * by the removed stream first. If not found, we try to avoid of using
3578 	 * any free pipes already used in current context as this could tear
3579 	 * down exiting ODM/MPC/MPO configuration unnecessarily.
3580 	 */
3581 
3582 	/*
3583 	 * Try to acquire the same OTG master already in use. This is not
3584 	 * optimal because resetting an enabled OTG master pipe for a new stream
3585 	 * requires an extra frame of wait. However there are test automation
3586 	 * and eDP assumptions that rely on reusing the same OTG master pipe
3587 	 * during mode change. We have to keep this logic as is for now.
3588 	 */
3589 	pipe_idx = recource_find_free_pipe_used_as_otg_master_in_cur_res_ctx(
3590 			&cur_ctx->res_ctx, &new_ctx->res_ctx, pool);
3591 	/*
3592 	 * Try to acquire a pipe not used in current resource context to avoid
3593 	 * pipe swapping.
3594 	 */
3595 	if (pipe_idx == FREE_PIPE_INDEX_NOT_FOUND)
3596 		pipe_idx = recource_find_free_pipe_not_used_in_cur_res_ctx(
3597 				&cur_ctx->res_ctx, &new_ctx->res_ctx, pool);
3598 	/*
3599 	 * If pipe swapping is unavoidable, try to acquire pipe used as
3600 	 * secondary DPP pipe in current state as we prioritize to support more
3601 	 * streams over supporting MPO planes.
3602 	 */
3603 	if (pipe_idx == FREE_PIPE_INDEX_NOT_FOUND)
3604 		pipe_idx = resource_find_free_pipe_used_as_cur_sec_dpp(
3605 				&cur_ctx->res_ctx, &new_ctx->res_ctx, pool);
3606 	if (pipe_idx == FREE_PIPE_INDEX_NOT_FOUND)
3607 		pipe_idx = resource_find_any_free_pipe(&new_ctx->res_ctx, pool);
3608 	if (pipe_idx != FREE_PIPE_INDEX_NOT_FOUND) {
3609 		pipe_ctx = &new_ctx->res_ctx.pipe_ctx[pipe_idx];
3610 		memset(pipe_ctx, 0, sizeof(*pipe_ctx));
3611 		pipe_ctx->pipe_idx = pipe_idx;
3612 		pipe_ctx->stream_res.tg = pool->timing_generators[pipe_idx];
3613 		pipe_ctx->plane_res.mi = pool->mis[pipe_idx];
3614 		pipe_ctx->plane_res.hubp = pool->hubps[pipe_idx];
3615 		pipe_ctx->plane_res.ipp = pool->ipps[pipe_idx];
3616 		pipe_ctx->plane_res.xfm = pool->transforms[pipe_idx];
3617 		pipe_ctx->plane_res.dpp = pool->dpps[pipe_idx];
3618 		pipe_ctx->stream_res.opp = pool->opps[pipe_idx];
3619 		if (pool->dpps[pipe_idx])
3620 			pipe_ctx->plane_res.mpcc_inst = pool->dpps[pipe_idx]->inst;
3621 
3622 		if (pipe_idx >= pool->timing_generator_count && pool->timing_generator_count != 0) {
3623 			int tg_inst = pool->timing_generator_count - 1;
3624 
3625 			pipe_ctx->stream_res.tg = pool->timing_generators[tg_inst];
3626 			pipe_ctx->stream_res.opp = pool->opps[tg_inst];
3627 		}
3628 
3629 		pipe_ctx->stream = stream;
3630 	} else {
3631 		pipe_idx = acquire_first_split_pipe(&new_ctx->res_ctx, pool, stream);
3632 	}
3633 
3634 	return pipe_idx != FREE_PIPE_INDEX_NOT_FOUND;
3635 }
3636 
resource_map_pool_resources(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)3637 enum dc_status resource_map_pool_resources(
3638 		const struct dc  *dc,
3639 		struct dc_state *context,
3640 		struct dc_stream_state *stream)
3641 {
3642 	const struct resource_pool *pool = dc->res_pool;
3643 	int i;
3644 	struct dc_context *dc_ctx = dc->ctx;
3645 	struct pipe_ctx *pipe_ctx = NULL;
3646 	int pipe_idx = -1;
3647 	bool acquired = false;
3648 
3649 	calculate_phy_pix_clks(stream);
3650 
3651 	mark_seamless_boot_stream(dc, stream);
3652 
3653 	if (stream->apply_seamless_boot_optimization) {
3654 		pipe_idx = acquire_resource_from_hw_enabled_state(
3655 				&context->res_ctx,
3656 				pool,
3657 				stream);
3658 		if (pipe_idx < 0)
3659 			/* hw resource was assigned to other stream */
3660 			stream->apply_seamless_boot_optimization = false;
3661 		else
3662 			acquired = true;
3663 	}
3664 
3665 	if (!acquired)
3666 		/* acquire new resources */
3667 		acquired = acquire_otg_master_pipe_for_stream(dc->current_state,
3668 				context, pool, stream);
3669 
3670 	pipe_ctx = resource_get_otg_master_for_stream(&context->res_ctx, stream);
3671 
3672 	if (!pipe_ctx || pipe_ctx->stream_res.tg == NULL)
3673 		return DC_NO_CONTROLLER_RESOURCE;
3674 
3675 	pipe_ctx->stream_res.stream_enc =
3676 		dc->res_pool->funcs->find_first_free_match_stream_enc_for_link(
3677 			&context->res_ctx, pool, stream);
3678 
3679 	if (!pipe_ctx->stream_res.stream_enc)
3680 		return DC_NO_STREAM_ENC_RESOURCE;
3681 
3682 	update_stream_engine_usage(
3683 		&context->res_ctx, pool,
3684 		pipe_ctx->stream_res.stream_enc,
3685 		true);
3686 
3687 	/* Allocate DP HPO Stream Encoder based on signal, hw capabilities
3688 	 * and link settings
3689 	 */
3690 	if (dc_is_dp_signal(stream->signal) ||
3691 			dc_is_virtual_signal(stream->signal)) {
3692 		if (!dc->link_srv->dp_decide_link_settings(stream,
3693 				&pipe_ctx->link_config.dp_link_settings))
3694 			return DC_FAIL_DP_LINK_BANDWIDTH;
3695 		if (dc->link_srv->dp_get_encoding_format(
3696 				&pipe_ctx->link_config.dp_link_settings) == DP_128b_132b_ENCODING) {
3697 			pipe_ctx->stream_res.hpo_dp_stream_enc =
3698 					find_first_free_match_hpo_dp_stream_enc_for_link(
3699 							&context->res_ctx, pool, stream);
3700 
3701 			if (!pipe_ctx->stream_res.hpo_dp_stream_enc)
3702 				return DC_NO_STREAM_ENC_RESOURCE;
3703 
3704 			update_hpo_dp_stream_engine_usage(
3705 					&context->res_ctx, pool,
3706 					pipe_ctx->stream_res.hpo_dp_stream_enc,
3707 					true);
3708 			if (!add_hpo_dp_link_enc_to_ctx(&context->res_ctx, pool, pipe_ctx, stream))
3709 				return DC_NO_LINK_ENC_RESOURCE;
3710 		}
3711 	}
3712 
3713 	/* TODO: Add check if ASIC support and EDID audio */
3714 	if (!stream->converter_disable_audio &&
3715 	    dc_is_audio_capable_signal(pipe_ctx->stream->signal) &&
3716 	    stream->audio_info.mode_count && stream->audio_info.flags.all) {
3717 		pipe_ctx->stream_res.audio = find_first_free_audio(
3718 		&context->res_ctx, pool, pipe_ctx->stream_res.stream_enc->id, dc_ctx->dce_version);
3719 
3720 		/*
3721 		 * Audio assigned in order first come first get.
3722 		 * There are asics which has number of audio
3723 		 * resources less then number of pipes
3724 		 */
3725 		if (pipe_ctx->stream_res.audio)
3726 			update_audio_usage(&context->res_ctx, pool,
3727 					   pipe_ctx->stream_res.audio, true);
3728 	}
3729 
3730 	/* Add ABM to the resource if on EDP */
3731 	if (pipe_ctx->stream && dc_is_embedded_signal(pipe_ctx->stream->signal)) {
3732 		if (pool->abm)
3733 			pipe_ctx->stream_res.abm = pool->abm;
3734 		else
3735 			pipe_ctx->stream_res.abm = pool->multiple_abms[pipe_ctx->stream_res.tg->inst];
3736 	}
3737 
3738 	for (i = 0; i < context->stream_count; i++)
3739 		if (context->streams[i] == stream) {
3740 			context->stream_status[i].primary_otg_inst = pipe_ctx->stream_res.tg->inst;
3741 			context->stream_status[i].stream_enc_inst = pipe_ctx->stream_res.stream_enc->stream_enc_inst;
3742 			context->stream_status[i].audio_inst =
3743 				pipe_ctx->stream_res.audio ? pipe_ctx->stream_res.audio->inst : -1;
3744 
3745 			return DC_OK;
3746 		}
3747 
3748 	DC_ERROR("Stream %p not found in new ctx!\n", stream);
3749 	return DC_ERROR_UNEXPECTED;
3750 }
3751 
dc_resource_is_dsc_encoding_supported(const struct dc * dc)3752 bool dc_resource_is_dsc_encoding_supported(const struct dc *dc)
3753 {
3754 	if (dc->res_pool == NULL)
3755 		return false;
3756 
3757 	return dc->res_pool->res_cap->num_dsc > 0;
3758 }
3759 
planes_changed_for_existing_stream(struct dc_state * context,struct dc_stream_state * stream,const struct dc_validation_set set[],int set_count)3760 static bool planes_changed_for_existing_stream(struct dc_state *context,
3761 					       struct dc_stream_state *stream,
3762 					       const struct dc_validation_set set[],
3763 					       int set_count)
3764 {
3765 	int i, j;
3766 	struct dc_stream_status *stream_status = NULL;
3767 
3768 	for (i = 0; i < context->stream_count; i++) {
3769 		if (context->streams[i] == stream) {
3770 			stream_status = &context->stream_status[i];
3771 			break;
3772 		}
3773 	}
3774 
3775 	if (!stream_status) {
3776 		ASSERT(0);
3777 		return false;
3778 	}
3779 
3780 	for (i = 0; i < set_count; i++)
3781 		if (set[i].stream == stream)
3782 			break;
3783 
3784 	if (i == set_count)
3785 		ASSERT(0);
3786 
3787 	if (set[i].plane_count != stream_status->plane_count)
3788 		return true;
3789 
3790 	for (j = 0; j < set[i].plane_count; j++)
3791 		if (set[i].plane_states[j] != stream_status->plane_states[j])
3792 			return true;
3793 
3794 	return false;
3795 }
3796 
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)3797 static bool add_all_planes_for_stream(
3798 		const struct dc *dc,
3799 		struct dc_stream_state *stream,
3800 		const struct dc_validation_set set[],
3801 		int set_count,
3802 		struct dc_state *state)
3803 {
3804 	int i, j;
3805 
3806 	for (i = 0; i < set_count; i++)
3807 		if (set[i].stream == stream)
3808 			break;
3809 
3810 	if (i == set_count) {
3811 		dm_error("Stream %p not found in set!\n", stream);
3812 		return false;
3813 	}
3814 
3815 	for (j = 0; j < set[i].plane_count; j++)
3816 		if (!dc_state_add_plane(dc, stream, set[i].plane_states[j], state))
3817 			return false;
3818 
3819 	return true;
3820 }
3821 
3822 /**
3823  * dc_validate_with_context - Validate and update the potential new stream in the context object
3824  *
3825  * @dc: Used to get the current state status
3826  * @set: An array of dc_validation_set with all the current streams reference
3827  * @set_count: Total of streams
3828  * @context: New context
3829  * @fast_validate: Enable or disable fast validation
3830  *
3831  * This function updates the potential new stream in the context object. It
3832  * creates multiple lists for the add, remove, and unchanged streams. In
3833  * particular, if the unchanged streams have a plane that changed, it is
3834  * necessary to remove all planes from the unchanged streams. In summary, this
3835  * function is responsible for validating the new context.
3836  *
3837  * Return:
3838  * In case of success, return DC_OK (1), otherwise, return a DC error.
3839  */
dc_validate_with_context(struct dc * dc,const struct dc_validation_set set[],int set_count,struct dc_state * context,bool fast_validate)3840 enum dc_status dc_validate_with_context(struct dc *dc,
3841 					const struct dc_validation_set set[],
3842 					int set_count,
3843 					struct dc_state *context,
3844 					bool fast_validate)
3845 {
3846 	struct dc_stream_state *unchanged_streams[MAX_PIPES] = { 0 };
3847 	struct dc_stream_state *del_streams[MAX_PIPES] = { 0 };
3848 	struct dc_stream_state *add_streams[MAX_PIPES] = { 0 };
3849 	int old_stream_count = context->stream_count;
3850 	enum dc_status res = DC_ERROR_UNEXPECTED;
3851 	int unchanged_streams_count = 0;
3852 	int del_streams_count = 0;
3853 	int add_streams_count = 0;
3854 	bool found = false;
3855 	int i, j, k;
3856 
3857 	DC_LOGGER_INIT(dc->ctx->logger);
3858 
3859 	/* First build a list of streams to be remove from current context */
3860 	for (i = 0; i < old_stream_count; i++) {
3861 		struct dc_stream_state *stream = context->streams[i];
3862 
3863 		for (j = 0; j < set_count; j++) {
3864 			if (stream == set[j].stream) {
3865 				found = true;
3866 				break;
3867 			}
3868 		}
3869 
3870 		if (!found)
3871 			del_streams[del_streams_count++] = stream;
3872 
3873 		found = false;
3874 	}
3875 
3876 	/* Second, build a list of new streams */
3877 	for (i = 0; i < set_count; i++) {
3878 		struct dc_stream_state *stream = set[i].stream;
3879 
3880 		for (j = 0; j < old_stream_count; j++) {
3881 			if (stream == context->streams[j]) {
3882 				found = true;
3883 				break;
3884 			}
3885 		}
3886 
3887 		if (!found)
3888 			add_streams[add_streams_count++] = stream;
3889 
3890 		found = false;
3891 	}
3892 
3893 	/* Build a list of unchanged streams which is necessary for handling
3894 	 * planes change such as added, removed, and updated.
3895 	 */
3896 	for (i = 0; i < set_count; i++) {
3897 		/* Check if stream is part of the delete list */
3898 		for (j = 0; j < del_streams_count; j++) {
3899 			if (set[i].stream == del_streams[j]) {
3900 				found = true;
3901 				break;
3902 			}
3903 		}
3904 
3905 		if (!found) {
3906 			/* Check if stream is part of the add list */
3907 			for (j = 0; j < add_streams_count; j++) {
3908 				if (set[i].stream == add_streams[j]) {
3909 					found = true;
3910 					break;
3911 				}
3912 			}
3913 		}
3914 
3915 		if (!found)
3916 			unchanged_streams[unchanged_streams_count++] = set[i].stream;
3917 
3918 		found = false;
3919 	}
3920 
3921 	/* Remove all planes for unchanged streams if planes changed */
3922 	for (i = 0; i < unchanged_streams_count; i++) {
3923 		if (planes_changed_for_existing_stream(context,
3924 						       unchanged_streams[i],
3925 						       set,
3926 						       set_count)) {
3927 
3928 			if (!dc_state_rem_all_planes_for_stream(dc,
3929 							  unchanged_streams[i],
3930 							  context)) {
3931 				res = DC_FAIL_DETACH_SURFACES;
3932 				goto fail;
3933 			}
3934 		}
3935 	}
3936 
3937 	/* Remove all planes for removed streams and then remove the streams */
3938 	for (i = 0; i < del_streams_count; i++) {
3939 		/* Need to cpy the dwb data from the old stream in order to efc to work */
3940 		if (del_streams[i]->num_wb_info > 0) {
3941 			for (j = 0; j < add_streams_count; j++) {
3942 				if (del_streams[i]->sink == add_streams[j]->sink) {
3943 					add_streams[j]->num_wb_info = del_streams[i]->num_wb_info;
3944 					for (k = 0; k < del_streams[i]->num_wb_info; k++)
3945 						add_streams[j]->writeback_info[k] = del_streams[i]->writeback_info[k];
3946 				}
3947 			}
3948 		}
3949 
3950 		if (dc_state_get_stream_subvp_type(context, del_streams[i]) == SUBVP_PHANTOM) {
3951 			/* remove phantoms specifically */
3952 			if (!dc_state_rem_all_phantom_planes_for_stream(dc, del_streams[i], context, true)) {
3953 				res = DC_FAIL_DETACH_SURFACES;
3954 				goto fail;
3955 			}
3956 
3957 			res = dc_state_remove_phantom_stream(dc, context, del_streams[i]);
3958 			dc_state_release_phantom_stream(dc, context, del_streams[i]);
3959 		} else {
3960 			if (!dc_state_rem_all_planes_for_stream(dc, del_streams[i], context)) {
3961 				res = DC_FAIL_DETACH_SURFACES;
3962 				goto fail;
3963 			}
3964 
3965 			res = dc_state_remove_stream(dc, context, del_streams[i]);
3966 		}
3967 
3968 		if (res != DC_OK)
3969 			goto fail;
3970 	}
3971 
3972 	/* Swap seamless boot stream to pipe 0 (if needed) to ensure pipe_ctx
3973 	 * matches. This may change in the future if seamless_boot_stream can be
3974 	 * multiple.
3975 	 */
3976 	for (i = 0; i < add_streams_count; i++) {
3977 		mark_seamless_boot_stream(dc, add_streams[i]);
3978 		if (add_streams[i]->apply_seamless_boot_optimization && i != 0) {
3979 			struct dc_stream_state *temp = add_streams[0];
3980 
3981 			add_streams[0] = add_streams[i];
3982 			add_streams[i] = temp;
3983 			break;
3984 		}
3985 	}
3986 
3987 	/* Add new streams and then add all planes for the new stream */
3988 	for (i = 0; i < add_streams_count; i++) {
3989 		calculate_phy_pix_clks(add_streams[i]);
3990 		res = dc_state_add_stream(dc, context, add_streams[i]);
3991 		if (res != DC_OK)
3992 			goto fail;
3993 
3994 		if (!add_all_planes_for_stream(dc, add_streams[i], set, set_count, context)) {
3995 			res = DC_FAIL_ATTACH_SURFACES;
3996 			goto fail;
3997 		}
3998 	}
3999 
4000 	/* Add all planes for unchanged streams if planes changed */
4001 	for (i = 0; i < unchanged_streams_count; i++) {
4002 		if (planes_changed_for_existing_stream(context,
4003 						       unchanged_streams[i],
4004 						       set,
4005 						       set_count)) {
4006 			if (!add_all_planes_for_stream(dc, unchanged_streams[i], set, set_count, context)) {
4007 				res = DC_FAIL_ATTACH_SURFACES;
4008 				goto fail;
4009 			}
4010 		}
4011 	}
4012 
4013 	res = dc_validate_global_state(dc, context, fast_validate);
4014 
4015 	/* calculate pixel rate divider after deciding pxiel clock & odm combine  */
4016 	if ((dc->hwss.calculate_pix_rate_divider) && (res == DC_OK)) {
4017 		for (i = 0; i < add_streams_count; i++)
4018 			dc->hwss.calculate_pix_rate_divider(dc, context, add_streams[i]);
4019 	}
4020 
4021 fail:
4022 	if (res != DC_OK)
4023 		DC_LOG_WARNING("%s:resource validation failed, dc_status:%d\n",
4024 			       __func__,
4025 			       res);
4026 
4027 	return res;
4028 }
4029 
4030 /**
4031  * decide_hblank_borrow - Decides the horizontal blanking borrow value for a given pipe context.
4032  * @pipe_ctx: Pointer to the pipe context structure.
4033  *
4034  * This function calculates the horizontal blanking borrow value for a given pipe context based on the
4035  * display stream compression (DSC) configuration. If the horizontal active pixels (hactive) are less
4036  * than the total width of the DSC slices, it sets the hblank_borrow value to the difference. If the
4037  * total horizontal timing minus the hblank_borrow value is less than 32, it resets the hblank_borrow
4038  * value to 0.
4039  */
decide_hblank_borrow(struct pipe_ctx * pipe_ctx)4040 static void decide_hblank_borrow(struct pipe_ctx *pipe_ctx)
4041 {
4042 	uint32_t hactive;
4043 	uint32_t ceil_slice_width;
4044 	struct dc_stream_state *stream = NULL;
4045 
4046 	if (!pipe_ctx)
4047 		return;
4048 
4049 	stream = pipe_ctx->stream;
4050 
4051 	if (stream->timing.flags.DSC) {
4052 		hactive = stream->timing.h_addressable + stream->timing.h_border_left + stream->timing.h_border_right;
4053 
4054 		/* Assume if determined slices does not divide Hactive evenly, Hborrow is needed for padding*/
4055 		if (hactive % stream->timing.dsc_cfg.num_slices_h != 0) {
4056 			ceil_slice_width = (hactive / stream->timing.dsc_cfg.num_slices_h) + 1;
4057 			pipe_ctx->hblank_borrow = ceil_slice_width * stream->timing.dsc_cfg.num_slices_h - hactive;
4058 
4059 			if (stream->timing.h_total - hactive - pipe_ctx->hblank_borrow < 32)
4060 				pipe_ctx->hblank_borrow = 0;
4061 		}
4062 	}
4063 }
4064 
4065 /**
4066  * dc_validate_global_state() - Determine if hardware can support a given state
4067  *
4068  * @dc: dc struct for this driver
4069  * @new_ctx: state to be validated
4070  * @fast_validate: set to true if only yes/no to support matters
4071  *
4072  * Checks hardware resource availability and bandwidth requirement.
4073  *
4074  * Return:
4075  * DC_OK if the result can be programmed. Otherwise, an error code.
4076  */
dc_validate_global_state(struct dc * dc,struct dc_state * new_ctx,bool fast_validate)4077 enum dc_status dc_validate_global_state(
4078 		struct dc *dc,
4079 		struct dc_state *new_ctx,
4080 		bool fast_validate)
4081 {
4082 	enum dc_status result = DC_ERROR_UNEXPECTED;
4083 	int i, j;
4084 
4085 	if (!new_ctx)
4086 		return DC_ERROR_UNEXPECTED;
4087 
4088 	if (dc->res_pool->funcs->validate_global) {
4089 		result = dc->res_pool->funcs->validate_global(dc, new_ctx);
4090 		if (result != DC_OK)
4091 			return result;
4092 	}
4093 
4094 	for (i = 0; i < new_ctx->stream_count; i++) {
4095 		struct dc_stream_state *stream = new_ctx->streams[i];
4096 
4097 		for (j = 0; j < dc->res_pool->pipe_count; j++) {
4098 			struct pipe_ctx *pipe_ctx = &new_ctx->res_ctx.pipe_ctx[j];
4099 
4100 			if (pipe_ctx->stream != stream)
4101 				continue;
4102 
4103 			/* Decide whether hblank borrow is needed and save it in pipe_ctx */
4104 			if (dc->debug.enable_hblank_borrow)
4105 				decide_hblank_borrow(pipe_ctx);
4106 
4107 			if (dc->res_pool->funcs->patch_unknown_plane_state &&
4108 					pipe_ctx->plane_state &&
4109 					pipe_ctx->plane_state->tiling_info.gfx9.swizzle == DC_SW_UNKNOWN) {
4110 				result = dc->res_pool->funcs->patch_unknown_plane_state(pipe_ctx->plane_state);
4111 				if (result != DC_OK)
4112 					return result;
4113 			}
4114 
4115 			/* Switch to dp clock source only if there is
4116 			 * no non dp stream that shares the same timing
4117 			 * with the dp stream.
4118 			 */
4119 			if (dc_is_dp_signal(pipe_ctx->stream->signal) &&
4120 				!find_pll_sharable_stream(stream, new_ctx)) {
4121 
4122 				resource_unreference_clock_source(
4123 						&new_ctx->res_ctx,
4124 						dc->res_pool,
4125 						pipe_ctx->clock_source);
4126 
4127 				pipe_ctx->clock_source = dc->res_pool->dp_clock_source;
4128 				resource_reference_clock_source(
4129 						&new_ctx->res_ctx,
4130 						dc->res_pool,
4131 						 pipe_ctx->clock_source);
4132 			}
4133 		}
4134 	}
4135 
4136 	result = resource_build_scaling_params_for_context(dc, new_ctx);
4137 
4138 	if (result == DC_OK)
4139 		if (!dc->res_pool->funcs->validate_bandwidth(dc, new_ctx, fast_validate))
4140 			result = DC_FAIL_BANDWIDTH_VALIDATE;
4141 
4142 	return result;
4143 }
4144 
patch_gamut_packet_checksum(struct dc_info_packet * gamut_packet)4145 static void patch_gamut_packet_checksum(
4146 		struct dc_info_packet *gamut_packet)
4147 {
4148 	/* For gamut we recalc checksum */
4149 	if (gamut_packet->valid) {
4150 		uint8_t chk_sum = 0;
4151 		uint8_t *ptr;
4152 		uint8_t i;
4153 
4154 		/*start of the Gamut data. */
4155 		ptr = &gamut_packet->sb[3];
4156 
4157 		for (i = 0; i <= gamut_packet->sb[1]; i++)
4158 			chk_sum += ptr[i];
4159 
4160 		gamut_packet->sb[2] = (uint8_t) (0x100 - chk_sum);
4161 	}
4162 }
4163 
set_avi_info_frame(struct dc_info_packet * info_packet,struct pipe_ctx * pipe_ctx)4164 static void set_avi_info_frame(
4165 		struct dc_info_packet *info_packet,
4166 		struct pipe_ctx *pipe_ctx)
4167 {
4168 	struct dc_stream_state *stream = pipe_ctx->stream;
4169 	enum dc_color_space color_space = COLOR_SPACE_UNKNOWN;
4170 	uint32_t pixel_encoding = 0;
4171 	enum scanning_type scan_type = SCANNING_TYPE_NODATA;
4172 	enum dc_aspect_ratio aspect = ASPECT_RATIO_NO_DATA;
4173 	uint8_t *check_sum = NULL;
4174 	uint8_t byte_index = 0;
4175 	union hdmi_info_packet hdmi_info;
4176 	unsigned int vic = pipe_ctx->stream->timing.vic;
4177 	unsigned int rid = pipe_ctx->stream->timing.rid;
4178 	unsigned int fr_ind = pipe_ctx->stream->timing.fr_index;
4179 	enum dc_timing_3d_format format;
4180 
4181 	memset(&hdmi_info, 0, sizeof(union hdmi_info_packet));
4182 
4183 	color_space = pipe_ctx->stream->output_color_space;
4184 	if (color_space == COLOR_SPACE_UNKNOWN)
4185 		color_space = (stream->timing.pixel_encoding == PIXEL_ENCODING_RGB) ?
4186 			COLOR_SPACE_SRGB:COLOR_SPACE_YCBCR709;
4187 
4188 	/* Initialize header */
4189 	hdmi_info.bits.header.info_frame_type = HDMI_INFOFRAME_TYPE_AVI;
4190 	/* InfoFrameVersion_3 is defined by CEA861F (Section 6.4), but shall
4191 	* not be used in HDMI 2.0 (Section 10.1) */
4192 	hdmi_info.bits.header.version = 2;
4193 	hdmi_info.bits.header.length = HDMI_AVI_INFOFRAME_SIZE;
4194 
4195 	/*
4196 	 * IDO-defined (Y2,Y1,Y0 = 1,1,1) shall not be used by devices built
4197 	 * according to HDMI 2.0 spec (Section 10.1)
4198 	 */
4199 
4200 	switch (stream->timing.pixel_encoding) {
4201 	case PIXEL_ENCODING_YCBCR422:
4202 		pixel_encoding = 1;
4203 		break;
4204 
4205 	case PIXEL_ENCODING_YCBCR444:
4206 		pixel_encoding = 2;
4207 		break;
4208 	case PIXEL_ENCODING_YCBCR420:
4209 		pixel_encoding = 3;
4210 		break;
4211 
4212 	case PIXEL_ENCODING_RGB:
4213 	default:
4214 		pixel_encoding = 0;
4215 	}
4216 
4217 	/* Y0_Y1_Y2 : The pixel encoding */
4218 	/* H14b AVI InfoFrame has extension on Y-field from 2 bits to 3 bits */
4219 	hdmi_info.bits.Y0_Y1_Y2 = pixel_encoding;
4220 
4221 	/* A0 = 1 Active Format Information valid */
4222 	hdmi_info.bits.A0 = ACTIVE_FORMAT_VALID;
4223 
4224 	/* B0, B1 = 3; Bar info data is valid */
4225 	hdmi_info.bits.B0_B1 = BAR_INFO_BOTH_VALID;
4226 
4227 	hdmi_info.bits.SC0_SC1 = PICTURE_SCALING_UNIFORM;
4228 
4229 	/* S0, S1 : Underscan / Overscan */
4230 	/* TODO: un-hardcode scan type */
4231 	scan_type = SCANNING_TYPE_UNDERSCAN;
4232 	hdmi_info.bits.S0_S1 = scan_type;
4233 
4234 	/* C0, C1 : Colorimetry */
4235 	switch (color_space) {
4236 	case COLOR_SPACE_YCBCR709:
4237 	case COLOR_SPACE_YCBCR709_LIMITED:
4238 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
4239 		break;
4240 	case COLOR_SPACE_YCBCR601:
4241 	case COLOR_SPACE_YCBCR601_LIMITED:
4242 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU601;
4243 		break;
4244 	case COLOR_SPACE_2020_RGB_FULLRANGE:
4245 	case COLOR_SPACE_2020_RGB_LIMITEDRANGE:
4246 	case COLOR_SPACE_2020_YCBCR:
4247 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_BT2020RGBYCBCR;
4248 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
4249 		break;
4250 	case COLOR_SPACE_ADOBERGB:
4251 		hdmi_info.bits.EC0_EC2 = COLORIMETRYEX_ADOBERGB;
4252 		hdmi_info.bits.C0_C1   = COLORIMETRY_EXTENDED;
4253 		break;
4254 	case COLOR_SPACE_SRGB:
4255 	default:
4256 		hdmi_info.bits.C0_C1 = COLORIMETRY_NO_DATA;
4257 		break;
4258 	}
4259 
4260 	if (pixel_encoding && color_space == COLOR_SPACE_2020_YCBCR &&
4261 			stream->out_transfer_func.tf == TRANSFER_FUNCTION_GAMMA22) {
4262 		hdmi_info.bits.EC0_EC2 = 0;
4263 		hdmi_info.bits.C0_C1 = COLORIMETRY_ITU709;
4264 	}
4265 
4266 	/* TODO: un-hardcode aspect ratio */
4267 	aspect = stream->timing.aspect_ratio;
4268 
4269 	switch (aspect) {
4270 	case ASPECT_RATIO_4_3:
4271 	case ASPECT_RATIO_16_9:
4272 		hdmi_info.bits.M0_M1 = aspect;
4273 		break;
4274 
4275 	case ASPECT_RATIO_NO_DATA:
4276 	case ASPECT_RATIO_64_27:
4277 	case ASPECT_RATIO_256_135:
4278 	default:
4279 		hdmi_info.bits.M0_M1 = 0;
4280 	}
4281 
4282 	/* Active Format Aspect ratio - same as Picture Aspect Ratio. */
4283 	hdmi_info.bits.R0_R3 = ACTIVE_FORMAT_ASPECT_RATIO_SAME_AS_PICTURE;
4284 
4285 	switch (stream->content_type) {
4286 	case DISPLAY_CONTENT_TYPE_NO_DATA:
4287 		hdmi_info.bits.CN0_CN1 = 0;
4288 		hdmi_info.bits.ITC = 1;
4289 		break;
4290 	case DISPLAY_CONTENT_TYPE_GRAPHICS:
4291 		hdmi_info.bits.CN0_CN1 = 0;
4292 		hdmi_info.bits.ITC = 1;
4293 		break;
4294 	case DISPLAY_CONTENT_TYPE_PHOTO:
4295 		hdmi_info.bits.CN0_CN1 = 1;
4296 		hdmi_info.bits.ITC = 1;
4297 		break;
4298 	case DISPLAY_CONTENT_TYPE_CINEMA:
4299 		hdmi_info.bits.CN0_CN1 = 2;
4300 		hdmi_info.bits.ITC = 1;
4301 		break;
4302 	case DISPLAY_CONTENT_TYPE_GAME:
4303 		hdmi_info.bits.CN0_CN1 = 3;
4304 		hdmi_info.bits.ITC = 1;
4305 		break;
4306 	}
4307 
4308 	if (stream->qs_bit == 1) {
4309 		if (color_space == COLOR_SPACE_SRGB ||
4310 			color_space == COLOR_SPACE_2020_RGB_FULLRANGE)
4311 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_FULL_RANGE;
4312 		else if (color_space == COLOR_SPACE_SRGB_LIMITED ||
4313 					color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE)
4314 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_LIMITED_RANGE;
4315 		else
4316 			hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
4317 	} else
4318 		hdmi_info.bits.Q0_Q1   = RGB_QUANTIZATION_DEFAULT_RANGE;
4319 
4320 	/* TODO : We should handle YCC quantization */
4321 	/* but we do not have matrix calculation */
4322 	hdmi_info.bits.YQ0_YQ1 = YYC_QUANTIZATION_LIMITED_RANGE;
4323 
4324 	///VIC
4325 	if (pipe_ctx->stream->timing.hdmi_vic != 0)
4326 		vic = 0;
4327 	format = stream->timing.timing_3d_format;
4328 	/*todo, add 3DStereo support*/
4329 	if (format != TIMING_3D_FORMAT_NONE) {
4330 		// Based on HDMI specs hdmi vic needs to be converted to cea vic when 3D is enabled
4331 		switch (pipe_ctx->stream->timing.hdmi_vic) {
4332 		case 1:
4333 			vic = 95;
4334 			break;
4335 		case 2:
4336 			vic = 94;
4337 			break;
4338 		case 3:
4339 			vic = 93;
4340 			break;
4341 		case 4:
4342 			vic = 98;
4343 			break;
4344 		default:
4345 			break;
4346 		}
4347 	}
4348 	/* If VIC >= 128, the Source shall use AVI InfoFrame Version 3*/
4349 	hdmi_info.bits.VIC0_VIC7 = vic;
4350 	if (vic >= 128)
4351 		hdmi_info.bits.header.version = 3;
4352 	/* If (C1, C0)=(1, 1) and (EC2, EC1, EC0)=(1, 1, 1),
4353 	 * the Source shall use 20 AVI InfoFrame Version 4
4354 	 */
4355 	if (hdmi_info.bits.C0_C1 == COLORIMETRY_EXTENDED &&
4356 			hdmi_info.bits.EC0_EC2 == COLORIMETRYEX_RESERVED) {
4357 		hdmi_info.bits.header.version = 4;
4358 		hdmi_info.bits.header.length = 14;
4359 	}
4360 
4361 	if (rid != 0 && fr_ind != 0) {
4362 		hdmi_info.bits.header.version = 4;
4363 		hdmi_info.bits.header.length = 15;
4364 
4365 		hdmi_info.bits.FR0_FR3 = fr_ind & 0xF;
4366 		hdmi_info.bits.FR4 = (fr_ind >> 4) & 0x1;
4367 		hdmi_info.bits.RID0_RID5 = rid;
4368 	}
4369 
4370 	/* pixel repetition
4371 	 * PR0 - PR3 start from 0 whereas pHwPathMode->mode.timing.flags.pixel
4372 	 * repetition start from 1 */
4373 	hdmi_info.bits.PR0_PR3 = 0;
4374 
4375 	/* Bar Info
4376 	 * barTop:    Line Number of End of Top Bar.
4377 	 * barBottom: Line Number of Start of Bottom Bar.
4378 	 * barLeft:   Pixel Number of End of Left Bar.
4379 	 * barRight:  Pixel Number of Start of Right Bar. */
4380 	hdmi_info.bits.bar_top = stream->timing.v_border_top;
4381 	hdmi_info.bits.bar_bottom = (stream->timing.v_total
4382 			- stream->timing.v_border_bottom + 1);
4383 	hdmi_info.bits.bar_left  = stream->timing.h_border_left;
4384 	hdmi_info.bits.bar_right = (stream->timing.h_total
4385 			- stream->timing.h_border_right + 1);
4386 
4387     /* Additional Colorimetry Extension
4388      * Used in conduction with C0-C1 and EC0-EC2
4389      * 0 = DCI-P3 RGB (D65)
4390      * 1 = DCI-P3 RGB (theater)
4391      */
4392 	hdmi_info.bits.ACE0_ACE3 = 0;
4393 
4394 	/* check_sum - Calculate AFMT_AVI_INFO0 ~ AFMT_AVI_INFO3 */
4395 	check_sum = &hdmi_info.packet_raw_data.sb[0];
4396 
4397 	*check_sum = HDMI_INFOFRAME_TYPE_AVI + hdmi_info.bits.header.length + hdmi_info.bits.header.version;
4398 
4399 	for (byte_index = 1; byte_index <= hdmi_info.bits.header.length; byte_index++)
4400 		*check_sum += hdmi_info.packet_raw_data.sb[byte_index];
4401 
4402 	/* one byte complement */
4403 	*check_sum = (uint8_t) (0x100 - *check_sum);
4404 
4405 	/* Store in hw_path_mode */
4406 	info_packet->hb0 = hdmi_info.packet_raw_data.hb0;
4407 	info_packet->hb1 = hdmi_info.packet_raw_data.hb1;
4408 	info_packet->hb2 = hdmi_info.packet_raw_data.hb2;
4409 
4410 	for (byte_index = 0; byte_index < sizeof(hdmi_info.packet_raw_data.sb); byte_index++)
4411 		info_packet->sb[byte_index] = hdmi_info.packet_raw_data.sb[byte_index];
4412 
4413 	info_packet->valid = true;
4414 }
4415 
set_vendor_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)4416 static void set_vendor_info_packet(
4417 		struct dc_info_packet *info_packet,
4418 		struct dc_stream_state *stream)
4419 {
4420 	/* SPD info packet for FreeSync */
4421 
4422 	/* Check if Freesync is supported. Return if false. If true,
4423 	 * set the corresponding bit in the info packet
4424 	 */
4425 	if (!stream->vsp_infopacket.valid)
4426 		return;
4427 
4428 	*info_packet = stream->vsp_infopacket;
4429 }
4430 
set_spd_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)4431 static void set_spd_info_packet(
4432 		struct dc_info_packet *info_packet,
4433 		struct dc_stream_state *stream)
4434 {
4435 	/* SPD info packet for FreeSync */
4436 
4437 	/* Check if Freesync is supported. Return if false. If true,
4438 	 * set the corresponding bit in the info packet
4439 	 */
4440 	if (!stream->vrr_infopacket.valid)
4441 		return;
4442 
4443 	*info_packet = stream->vrr_infopacket;
4444 }
4445 
set_hdr_static_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)4446 static void set_hdr_static_info_packet(
4447 		struct dc_info_packet *info_packet,
4448 		struct dc_stream_state *stream)
4449 {
4450 	/* HDR Static Metadata info packet for HDR10 */
4451 
4452 	if (!stream->hdr_static_metadata.valid ||
4453 			stream->use_dynamic_meta)
4454 		return;
4455 
4456 	*info_packet = stream->hdr_static_metadata;
4457 }
4458 
set_vsc_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)4459 static void set_vsc_info_packet(
4460 		struct dc_info_packet *info_packet,
4461 		struct dc_stream_state *stream)
4462 {
4463 	if (!stream->vsc_infopacket.valid)
4464 		return;
4465 
4466 	*info_packet = stream->vsc_infopacket;
4467 }
set_hfvs_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)4468 static void set_hfvs_info_packet(
4469 		struct dc_info_packet *info_packet,
4470 		struct dc_stream_state *stream)
4471 {
4472 	if (!stream->hfvsif_infopacket.valid)
4473 		return;
4474 
4475 	*info_packet = stream->hfvsif_infopacket;
4476 }
4477 
adaptive_sync_override_dp_info_packets_sdp_line_num(const struct dc_crtc_timing * timing,struct enc_sdp_line_num * sdp_line_num,unsigned int vstartup_start)4478 static void adaptive_sync_override_dp_info_packets_sdp_line_num(
4479 		const struct dc_crtc_timing *timing,
4480 		struct enc_sdp_line_num *sdp_line_num,
4481 		unsigned int vstartup_start)
4482 {
4483 	uint32_t asic_blank_start = 0;
4484 	uint32_t asic_blank_end   = 0;
4485 	uint32_t v_update = 0;
4486 
4487 	const struct dc_crtc_timing *tg = timing;
4488 
4489 	/* blank_start = frame end - front porch */
4490 	asic_blank_start = tg->v_total - tg->v_front_porch;
4491 
4492 	/* blank_end = blank_start - active */
4493 	asic_blank_end = (asic_blank_start - tg->v_border_bottom -
4494 						tg->v_addressable - tg->v_border_top);
4495 
4496 	if (vstartup_start > asic_blank_end) {
4497 		v_update = (tg->v_total - (vstartup_start - asic_blank_end));
4498 		sdp_line_num->adaptive_sync_line_num_valid = true;
4499 		sdp_line_num->adaptive_sync_line_num = (tg->v_total - v_update - 1);
4500 	} else {
4501 		sdp_line_num->adaptive_sync_line_num_valid = false;
4502 		sdp_line_num->adaptive_sync_line_num = 0;
4503 	}
4504 }
4505 
set_adaptive_sync_info_packet(struct dc_info_packet * info_packet,const struct dc_stream_state * stream,struct encoder_info_frame * info_frame,unsigned int vstartup_start)4506 static void set_adaptive_sync_info_packet(
4507 		struct dc_info_packet *info_packet,
4508 		const struct dc_stream_state *stream,
4509 		struct encoder_info_frame *info_frame,
4510 		unsigned int vstartup_start)
4511 {
4512 	if (!stream->adaptive_sync_infopacket.valid)
4513 		return;
4514 
4515 	adaptive_sync_override_dp_info_packets_sdp_line_num(
4516 			&stream->timing,
4517 			&info_frame->sdp_line_num,
4518 			vstartup_start);
4519 
4520 	*info_packet = stream->adaptive_sync_infopacket;
4521 }
4522 
set_vtem_info_packet(struct dc_info_packet * info_packet,struct dc_stream_state * stream)4523 static void set_vtem_info_packet(
4524 		struct dc_info_packet *info_packet,
4525 		struct dc_stream_state *stream)
4526 {
4527 	if (!stream->vtem_infopacket.valid)
4528 		return;
4529 
4530 	*info_packet = stream->vtem_infopacket;
4531 }
4532 
dc_resource_find_first_free_pll(struct resource_context * res_ctx,const struct resource_pool * pool)4533 struct clock_source *dc_resource_find_first_free_pll(
4534 		struct resource_context *res_ctx,
4535 		const struct resource_pool *pool)
4536 {
4537 	int i;
4538 
4539 	for (i = 0; i < pool->clk_src_count; ++i) {
4540 		if (res_ctx->clock_source_ref_count[i] == 0)
4541 			return pool->clock_sources[i];
4542 	}
4543 
4544 	return NULL;
4545 }
4546 
resource_build_info_frame(struct pipe_ctx * pipe_ctx)4547 void resource_build_info_frame(struct pipe_ctx *pipe_ctx)
4548 {
4549 	enum signal_type signal = SIGNAL_TYPE_NONE;
4550 	struct encoder_info_frame *info = &pipe_ctx->stream_res.encoder_info_frame;
4551 	unsigned int vstartup_start = 0;
4552 
4553 	/* default all packets to invalid */
4554 	info->avi.valid = false;
4555 	info->gamut.valid = false;
4556 	info->vendor.valid = false;
4557 	info->spd.valid = false;
4558 	info->hdrsmd.valid = false;
4559 	info->vsc.valid = false;
4560 	info->hfvsif.valid = false;
4561 	info->vtem.valid = false;
4562 	info->adaptive_sync.valid = false;
4563 	signal = pipe_ctx->stream->signal;
4564 
4565 	if (pipe_ctx->stream->ctx->dc->res_pool->funcs->get_vstartup_for_pipe)
4566 		vstartup_start = pipe_ctx->stream->ctx->dc->res_pool->funcs->get_vstartup_for_pipe(pipe_ctx);
4567 
4568 	/* HDMi and DP have different info packets*/
4569 	if (dc_is_hdmi_signal(signal)) {
4570 		set_avi_info_frame(&info->avi, pipe_ctx);
4571 
4572 		set_vendor_info_packet(&info->vendor, pipe_ctx->stream);
4573 		set_hfvs_info_packet(&info->hfvsif, pipe_ctx->stream);
4574 		set_vtem_info_packet(&info->vtem, pipe_ctx->stream);
4575 
4576 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
4577 
4578 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
4579 
4580 	} else if (dc_is_dp_signal(signal)) {
4581 		set_vsc_info_packet(&info->vsc, pipe_ctx->stream);
4582 
4583 		set_spd_info_packet(&info->spd, pipe_ctx->stream);
4584 
4585 		set_hdr_static_info_packet(&info->hdrsmd, pipe_ctx->stream);
4586 		set_adaptive_sync_info_packet(&info->adaptive_sync,
4587 										pipe_ctx->stream,
4588 										info,
4589 										vstartup_start);
4590 	}
4591 
4592 	patch_gamut_packet_checksum(&info->gamut);
4593 }
4594 
resource_map_clock_resources(const struct dc * dc,struct dc_state * context,struct dc_stream_state * stream)4595 enum dc_status resource_map_clock_resources(
4596 		const struct dc  *dc,
4597 		struct dc_state *context,
4598 		struct dc_stream_state *stream)
4599 {
4600 	/* acquire new resources */
4601 	const struct resource_pool *pool = dc->res_pool;
4602 	struct pipe_ctx *pipe_ctx = resource_get_otg_master_for_stream(
4603 				&context->res_ctx, stream);
4604 
4605 	if (!pipe_ctx)
4606 		return DC_ERROR_UNEXPECTED;
4607 
4608 	if (dc_is_dp_signal(pipe_ctx->stream->signal)
4609 		|| pipe_ctx->stream->signal == SIGNAL_TYPE_VIRTUAL)
4610 		pipe_ctx->clock_source = pool->dp_clock_source;
4611 	else {
4612 		pipe_ctx->clock_source = NULL;
4613 
4614 		if (!dc->config.disable_disp_pll_sharing)
4615 			pipe_ctx->clock_source = resource_find_used_clk_src_for_sharing(
4616 				&context->res_ctx,
4617 				pipe_ctx);
4618 
4619 		if (pipe_ctx->clock_source == NULL)
4620 			pipe_ctx->clock_source =
4621 				dc_resource_find_first_free_pll(
4622 					&context->res_ctx,
4623 					pool);
4624 	}
4625 
4626 	if (pipe_ctx->clock_source == NULL)
4627 		return DC_NO_CLOCK_SOURCE_RESOURCE;
4628 
4629 	resource_reference_clock_source(
4630 		&context->res_ctx, pool,
4631 		pipe_ctx->clock_source);
4632 
4633 	return DC_OK;
4634 }
4635 
4636 /*
4637  * Note: We need to disable output if clock sources change,
4638  * since bios does optimization and doesn't apply if changing
4639  * PHY when not already disabled.
4640  */
pipe_need_reprogram(struct pipe_ctx * pipe_ctx_old,struct pipe_ctx * pipe_ctx)4641 bool pipe_need_reprogram(
4642 		struct pipe_ctx *pipe_ctx_old,
4643 		struct pipe_ctx *pipe_ctx)
4644 {
4645 	if (!pipe_ctx_old->stream)
4646 		return false;
4647 
4648 	if (pipe_ctx_old->stream->sink != pipe_ctx->stream->sink)
4649 		return true;
4650 
4651 	if (pipe_ctx_old->stream->signal != pipe_ctx->stream->signal)
4652 		return true;
4653 
4654 	if (pipe_ctx_old->stream_res.audio != pipe_ctx->stream_res.audio)
4655 		return true;
4656 
4657 	if (pipe_ctx_old->clock_source != pipe_ctx->clock_source
4658 			&& pipe_ctx_old->stream != pipe_ctx->stream)
4659 		return true;
4660 
4661 	if (pipe_ctx_old->stream_res.stream_enc != pipe_ctx->stream_res.stream_enc)
4662 		return true;
4663 
4664 	if (dc_is_timing_changed(pipe_ctx_old->stream, pipe_ctx->stream))
4665 		return true;
4666 
4667 	if (pipe_ctx_old->stream->dpms_off != pipe_ctx->stream->dpms_off)
4668 		return true;
4669 
4670 	if (false == pipe_ctx_old->stream->link->link_state_valid &&
4671 		false == pipe_ctx_old->stream->dpms_off)
4672 		return true;
4673 
4674 	if (pipe_ctx_old->stream_res.dsc != pipe_ctx->stream_res.dsc)
4675 		return true;
4676 
4677 	if (pipe_ctx_old->stream_res.hpo_dp_stream_enc != pipe_ctx->stream_res.hpo_dp_stream_enc)
4678 		return true;
4679 	if (pipe_ctx_old->link_res.hpo_dp_link_enc != pipe_ctx->link_res.hpo_dp_link_enc)
4680 		return true;
4681 
4682 	/* DIG link encoder resource assignment for stream changed. */
4683 	if (pipe_ctx_old->stream->ctx->dc->res_pool->funcs->link_encs_assign) {
4684 		bool need_reprogram = false;
4685 		struct dc *dc = pipe_ctx_old->stream->ctx->dc;
4686 		struct link_encoder *link_enc_prev =
4687 			link_enc_cfg_get_link_enc_used_by_stream_current(dc, pipe_ctx_old->stream);
4688 
4689 		if (link_enc_prev != pipe_ctx->stream->link_enc)
4690 			need_reprogram = true;
4691 
4692 		return need_reprogram;
4693 	}
4694 
4695 	return false;
4696 }
4697 
resource_build_bit_depth_reduction_params(struct dc_stream_state * stream,struct bit_depth_reduction_params * fmt_bit_depth)4698 void resource_build_bit_depth_reduction_params(struct dc_stream_state *stream,
4699 		struct bit_depth_reduction_params *fmt_bit_depth)
4700 {
4701 	enum dc_dither_option option = stream->dither_option;
4702 	enum dc_pixel_encoding pixel_encoding =
4703 			stream->timing.pixel_encoding;
4704 
4705 	memset(fmt_bit_depth, 0, sizeof(*fmt_bit_depth));
4706 
4707 	if (option == DITHER_OPTION_DEFAULT) {
4708 		switch (stream->timing.display_color_depth) {
4709 		case COLOR_DEPTH_666:
4710 			option = DITHER_OPTION_SPATIAL6;
4711 			break;
4712 		case COLOR_DEPTH_888:
4713 			option = DITHER_OPTION_SPATIAL8;
4714 			break;
4715 		case COLOR_DEPTH_101010:
4716 			option = DITHER_OPTION_TRUN10;
4717 			break;
4718 		default:
4719 			option = DITHER_OPTION_DISABLE;
4720 		}
4721 	}
4722 
4723 	if (option == DITHER_OPTION_DISABLE)
4724 		return;
4725 
4726 	if (option == DITHER_OPTION_TRUN6) {
4727 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
4728 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 0;
4729 	} else if (option == DITHER_OPTION_TRUN8 ||
4730 			option == DITHER_OPTION_TRUN8_SPATIAL6 ||
4731 			option == DITHER_OPTION_TRUN8_FM6) {
4732 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
4733 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 1;
4734 	} else if (option == DITHER_OPTION_TRUN10        ||
4735 			option == DITHER_OPTION_TRUN10_SPATIAL6   ||
4736 			option == DITHER_OPTION_TRUN10_SPATIAL8   ||
4737 			option == DITHER_OPTION_TRUN10_FM8     ||
4738 			option == DITHER_OPTION_TRUN10_FM6     ||
4739 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
4740 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
4741 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
4742 		if (option == DITHER_OPTION_TRUN10)
4743 			fmt_bit_depth->flags.TRUNCATE_MODE = 1;
4744 	}
4745 
4746 	/* special case - Formatter can only reduce by 4 bits at most.
4747 	 * When reducing from 12 to 6 bits,
4748 	 * HW recommends we use trunc with round mode
4749 	 * (if we did nothing, trunc to 10 bits would be used)
4750 	 * note that any 12->10 bit reduction is ignored prior to DCE8,
4751 	 * as the input was 10 bits.
4752 	 */
4753 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM ||
4754 			option == DITHER_OPTION_SPATIAL6 ||
4755 			option == DITHER_OPTION_FM6) {
4756 		fmt_bit_depth->flags.TRUNCATE_ENABLED = 1;
4757 		fmt_bit_depth->flags.TRUNCATE_DEPTH = 2;
4758 		fmt_bit_depth->flags.TRUNCATE_MODE = 1;
4759 	}
4760 
4761 	/* spatial dither
4762 	 * note that spatial modes 1-3 are never used
4763 	 */
4764 	if (option == DITHER_OPTION_SPATIAL6_FRAME_RANDOM            ||
4765 			option == DITHER_OPTION_SPATIAL6 ||
4766 			option == DITHER_OPTION_TRUN10_SPATIAL6      ||
4767 			option == DITHER_OPTION_TRUN8_SPATIAL6) {
4768 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
4769 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 0;
4770 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
4771 		fmt_bit_depth->flags.RGB_RANDOM =
4772 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
4773 	} else if (option == DITHER_OPTION_SPATIAL8_FRAME_RANDOM            ||
4774 			option == DITHER_OPTION_SPATIAL8 ||
4775 			option == DITHER_OPTION_SPATIAL8_FM6        ||
4776 			option == DITHER_OPTION_TRUN10_SPATIAL8      ||
4777 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
4778 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
4779 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 1;
4780 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
4781 		fmt_bit_depth->flags.RGB_RANDOM =
4782 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
4783 	} else if (option == DITHER_OPTION_SPATIAL10_FRAME_RANDOM ||
4784 			option == DITHER_OPTION_SPATIAL10 ||
4785 			option == DITHER_OPTION_SPATIAL10_FM8 ||
4786 			option == DITHER_OPTION_SPATIAL10_FM6) {
4787 		fmt_bit_depth->flags.SPATIAL_DITHER_ENABLED = 1;
4788 		fmt_bit_depth->flags.SPATIAL_DITHER_DEPTH = 2;
4789 		fmt_bit_depth->flags.HIGHPASS_RANDOM = 1;
4790 		fmt_bit_depth->flags.RGB_RANDOM =
4791 				(pixel_encoding == PIXEL_ENCODING_RGB) ? 1 : 0;
4792 	}
4793 
4794 	if (option == DITHER_OPTION_SPATIAL6 ||
4795 			option == DITHER_OPTION_SPATIAL8 ||
4796 			option == DITHER_OPTION_SPATIAL10) {
4797 		fmt_bit_depth->flags.FRAME_RANDOM = 0;
4798 	} else {
4799 		fmt_bit_depth->flags.FRAME_RANDOM = 1;
4800 	}
4801 
4802 	//////////////////////
4803 	//// temporal dither
4804 	//////////////////////
4805 	if (option == DITHER_OPTION_FM6           ||
4806 			option == DITHER_OPTION_SPATIAL8_FM6     ||
4807 			option == DITHER_OPTION_SPATIAL10_FM6     ||
4808 			option == DITHER_OPTION_TRUN10_FM6     ||
4809 			option == DITHER_OPTION_TRUN8_FM6      ||
4810 			option == DITHER_OPTION_TRUN10_SPATIAL8_FM6) {
4811 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
4812 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 0;
4813 	} else if (option == DITHER_OPTION_FM8        ||
4814 			option == DITHER_OPTION_SPATIAL10_FM8  ||
4815 			option == DITHER_OPTION_TRUN10_FM8) {
4816 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
4817 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 1;
4818 	} else if (option == DITHER_OPTION_FM10) {
4819 		fmt_bit_depth->flags.FRAME_MODULATION_ENABLED = 1;
4820 		fmt_bit_depth->flags.FRAME_MODULATION_DEPTH = 2;
4821 	}
4822 
4823 	fmt_bit_depth->pixel_encoding = pixel_encoding;
4824 }
4825 
dc_validate_stream(struct dc * dc,struct dc_stream_state * stream)4826 enum dc_status dc_validate_stream(struct dc *dc, struct dc_stream_state *stream)
4827 {
4828 	if (dc == NULL || stream == NULL)
4829 		return DC_ERROR_UNEXPECTED;
4830 
4831 	struct dc_link *link = stream->link;
4832 	struct timing_generator *tg = dc->res_pool->timing_generators[0];
4833 	enum dc_status res = DC_OK;
4834 
4835 	calculate_phy_pix_clks(stream);
4836 
4837 	if (!tg->funcs->validate_timing(tg, &stream->timing))
4838 		res = DC_FAIL_CONTROLLER_VALIDATE;
4839 
4840 	if (res == DC_OK) {
4841 		if (link->ep_type == DISPLAY_ENDPOINT_PHY &&
4842 				!link->link_enc->funcs->validate_output_with_stream(
4843 						link->link_enc, stream))
4844 			res = DC_FAIL_ENC_VALIDATE;
4845 	}
4846 
4847 	/* TODO: validate audio ASIC caps, encoder */
4848 
4849 	if (res == DC_OK)
4850 		res = dc->link_srv->validate_mode_timing(stream,
4851 		      link,
4852 		      &stream->timing);
4853 
4854 	return res;
4855 }
4856 
dc_validate_plane(struct dc * dc,const struct dc_plane_state * plane_state)4857 enum dc_status dc_validate_plane(struct dc *dc, const struct dc_plane_state *plane_state)
4858 {
4859 	enum dc_status res = DC_OK;
4860 
4861 	/* check if surface has invalid dimensions */
4862 	if (plane_state->src_rect.width == 0 || plane_state->src_rect.height == 0 ||
4863 		plane_state->dst_rect.width == 0 || plane_state->dst_rect.height == 0)
4864 		return DC_FAIL_SURFACE_VALIDATE;
4865 
4866 	/* TODO For now validates pixel format only */
4867 	if (dc->res_pool->funcs->validate_plane)
4868 		return dc->res_pool->funcs->validate_plane(plane_state, &dc->caps);
4869 
4870 	return res;
4871 }
4872 
resource_pixel_format_to_bpp(enum surface_pixel_format format)4873 unsigned int resource_pixel_format_to_bpp(enum surface_pixel_format format)
4874 {
4875 	switch (format) {
4876 	case SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS:
4877 		return 8;
4878 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
4879 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
4880 		return 12;
4881 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB1555:
4882 	case SURFACE_PIXEL_FORMAT_GRPH_RGB565:
4883 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCbCr:
4884 	case SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb:
4885 		return 16;
4886 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB8888:
4887 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR8888:
4888 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010:
4889 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010:
4890 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010_XR_BIAS:
4891 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE:
4892 	case SURFACE_PIXEL_FORMAT_GRPH_RGBE_ALPHA:
4893 		return 32;
4894 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616:
4895 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616:
4896 	case SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F:
4897 	case SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F:
4898 		return 64;
4899 	default:
4900 		ASSERT_CRITICAL(false);
4901 		return -1;
4902 	}
4903 }
get_max_audio_sample_rate(struct audio_mode * modes)4904 static unsigned int get_max_audio_sample_rate(struct audio_mode *modes)
4905 {
4906 	if (modes) {
4907 		if (modes->sample_rates.rate.RATE_192)
4908 			return 192000;
4909 		if (modes->sample_rates.rate.RATE_176_4)
4910 			return 176400;
4911 		if (modes->sample_rates.rate.RATE_96)
4912 			return 96000;
4913 		if (modes->sample_rates.rate.RATE_88_2)
4914 			return 88200;
4915 		if (modes->sample_rates.rate.RATE_48)
4916 			return 48000;
4917 		if (modes->sample_rates.rate.RATE_44_1)
4918 			return 44100;
4919 		if (modes->sample_rates.rate.RATE_32)
4920 			return 32000;
4921 	}
4922 	/*original logic when no audio info*/
4923 	return 441000;
4924 }
4925 
get_audio_check(struct audio_info * aud_modes,struct audio_check * audio_chk)4926 void get_audio_check(struct audio_info *aud_modes,
4927 	struct audio_check *audio_chk)
4928 {
4929 	unsigned int i;
4930 	unsigned int max_sample_rate = 0;
4931 
4932 	if (aud_modes) {
4933 		audio_chk->audio_packet_type = 0x2;/*audio sample packet AP = .25 for layout0, 1 for layout1*/
4934 
4935 		audio_chk->max_audiosample_rate = 0;
4936 		for (i = 0; i < aud_modes->mode_count; i++) {
4937 			max_sample_rate = get_max_audio_sample_rate(&aud_modes->modes[i]);
4938 			if (audio_chk->max_audiosample_rate < max_sample_rate)
4939 				audio_chk->max_audiosample_rate = max_sample_rate;
4940 			/*dts takes the same as type 2: AP = 0.25*/
4941 		}
4942 		/*check which one take more bandwidth*/
4943 		if (audio_chk->max_audiosample_rate > 192000)
4944 			audio_chk->audio_packet_type = 0x9;/*AP =1*/
4945 		audio_chk->acat = 0;/*not support*/
4946 	}
4947 }
4948 
get_temp_hpo_dp_link_enc(const struct resource_context * res_ctx,const struct resource_pool * const pool,const struct dc_link * link)4949 static struct hpo_dp_link_encoder *get_temp_hpo_dp_link_enc(
4950 		const struct resource_context *res_ctx,
4951 		const struct resource_pool *const pool,
4952 		const struct dc_link *link)
4953 {
4954 	struct hpo_dp_link_encoder *hpo_dp_link_enc = NULL;
4955 	int enc_index;
4956 
4957 	enc_index = find_acquired_hpo_dp_link_enc_for_link(res_ctx, link);
4958 
4959 	if (enc_index < 0)
4960 		enc_index = find_free_hpo_dp_link_enc(res_ctx, pool);
4961 
4962 	if (enc_index >= 0)
4963 		hpo_dp_link_enc = pool->hpo_dp_link_enc[enc_index];
4964 
4965 	return hpo_dp_link_enc;
4966 }
4967 
get_temp_dp_link_res(struct dc_link * link,struct link_resource * link_res,struct dc_link_settings * link_settings)4968 bool get_temp_dp_link_res(struct dc_link *link,
4969 		struct link_resource *link_res,
4970 		struct dc_link_settings *link_settings)
4971 {
4972 	const struct dc *dc  = link->dc;
4973 	const struct resource_context *res_ctx = &dc->current_state->res_ctx;
4974 
4975 	memset(link_res, 0, sizeof(*link_res));
4976 
4977 	if (dc->link_srv->dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING) {
4978 		link_res->hpo_dp_link_enc = get_temp_hpo_dp_link_enc(res_ctx,
4979 				dc->res_pool, link);
4980 		if (!link_res->hpo_dp_link_enc)
4981 			return false;
4982 	}
4983 	return true;
4984 }
4985 
reset_syncd_pipes_from_disabled_pipes(struct dc * dc,struct dc_state * context)4986 void reset_syncd_pipes_from_disabled_pipes(struct dc *dc,
4987 		struct dc_state *context)
4988 {
4989 	int i, j;
4990 	struct pipe_ctx *pipe_ctx_old, *pipe_ctx, *pipe_ctx_syncd;
4991 
4992 	/* If pipe backend is reset, need to reset pipe syncd status */
4993 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
4994 		pipe_ctx_old =	&dc->current_state->res_ctx.pipe_ctx[i];
4995 		pipe_ctx = &context->res_ctx.pipe_ctx[i];
4996 
4997 		if (!resource_is_pipe_type(pipe_ctx_old, OTG_MASTER))
4998 			continue;
4999 
5000 		if (!pipe_ctx->stream ||
5001 				pipe_need_reprogram(pipe_ctx_old, pipe_ctx)) {
5002 
5003 			/* Reset all the syncd pipes from the disabled pipe */
5004 			for (j = 0; j < dc->res_pool->pipe_count; j++) {
5005 				pipe_ctx_syncd = &context->res_ctx.pipe_ctx[j];
5006 				if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_syncd) == pipe_ctx_old->pipe_idx) ||
5007 					!IS_PIPE_SYNCD_VALID(pipe_ctx_syncd))
5008 					SET_PIPE_SYNCD_TO_PIPE(pipe_ctx_syncd, j);
5009 			}
5010 		}
5011 	}
5012 }
5013 
check_syncd_pipes_for_disabled_master_pipe(struct dc * dc,struct dc_state * context,uint8_t disabled_master_pipe_idx)5014 void check_syncd_pipes_for_disabled_master_pipe(struct dc *dc,
5015 	struct dc_state *context,
5016 	uint8_t disabled_master_pipe_idx)
5017 {
5018 	int i;
5019 	struct pipe_ctx *pipe_ctx, *pipe_ctx_check;
5020 
5021 	pipe_ctx = &context->res_ctx.pipe_ctx[disabled_master_pipe_idx];
5022 	if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx) != disabled_master_pipe_idx) ||
5023 		!IS_PIPE_SYNCD_VALID(pipe_ctx))
5024 		SET_PIPE_SYNCD_TO_PIPE(pipe_ctx, disabled_master_pipe_idx);
5025 
5026 	/* for the pipe disabled, check if any slave pipe exists and assert */
5027 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5028 		pipe_ctx_check = &context->res_ctx.pipe_ctx[i];
5029 
5030 		if ((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_check) == disabled_master_pipe_idx) &&
5031 		    IS_PIPE_SYNCD_VALID(pipe_ctx_check) && (i != disabled_master_pipe_idx)) {
5032 			struct pipe_ctx *first_pipe = pipe_ctx_check;
5033 
5034 			while (first_pipe->prev_odm_pipe)
5035 				first_pipe = first_pipe->prev_odm_pipe;
5036 			/* When ODM combine is enabled, this case is expected. If the disabled pipe
5037 			 * is part of the ODM tree, then we should not print an error.
5038 			 * */
5039 			if (first_pipe->pipe_idx == disabled_master_pipe_idx)
5040 				continue;
5041 
5042 			DC_ERR("DC: Failure: pipe_idx[%d] syncd with disabled master pipe_idx[%d]\n",
5043 				   i, disabled_master_pipe_idx);
5044 		}
5045 	}
5046 }
5047 
reset_sync_context_for_pipe(const struct dc * dc,struct dc_state * context,uint8_t pipe_idx)5048 void reset_sync_context_for_pipe(const struct dc *dc,
5049 	struct dc_state *context,
5050 	uint8_t pipe_idx)
5051 {
5052 	int i;
5053 	struct pipe_ctx *pipe_ctx_reset;
5054 
5055 	/* reset the otg sync context for the pipe and its slave pipes if any */
5056 	for (i = 0; i < dc->res_pool->pipe_count; i++) {
5057 		pipe_ctx_reset = &context->res_ctx.pipe_ctx[i];
5058 
5059 		if (((GET_PIPE_SYNCD_FROM_PIPE(pipe_ctx_reset) == pipe_idx) &&
5060 			IS_PIPE_SYNCD_VALID(pipe_ctx_reset)) || (i == pipe_idx))
5061 			SET_PIPE_SYNCD_TO_PIPE(pipe_ctx_reset, i);
5062 	}
5063 }
5064 
resource_transmitter_to_phy_idx(const struct dc * dc,enum transmitter transmitter)5065 uint8_t resource_transmitter_to_phy_idx(const struct dc *dc, enum transmitter transmitter)
5066 {
5067 	/* TODO - get transmitter to phy idx mapping from DMUB */
5068 	uint8_t phy_idx = transmitter - TRANSMITTER_UNIPHY_A;
5069 
5070 	if (dc->ctx->dce_version == DCN_VERSION_3_1 &&
5071 			dc->ctx->asic_id.hw_internal_rev == YELLOW_CARP_B0) {
5072 		switch (transmitter) {
5073 		case TRANSMITTER_UNIPHY_A:
5074 			phy_idx = 0;
5075 			break;
5076 		case TRANSMITTER_UNIPHY_B:
5077 			phy_idx = 1;
5078 			break;
5079 		case TRANSMITTER_UNIPHY_C:
5080 			phy_idx = 5;
5081 			break;
5082 		case TRANSMITTER_UNIPHY_D:
5083 			phy_idx = 6;
5084 			break;
5085 		case TRANSMITTER_UNIPHY_E:
5086 			phy_idx = 4;
5087 			break;
5088 		default:
5089 			phy_idx = 0;
5090 			break;
5091 		}
5092 	}
5093 
5094 	return phy_idx;
5095 }
5096 
get_link_hwss(const struct dc_link * link,const struct link_resource * link_res)5097 const struct link_hwss *get_link_hwss(const struct dc_link *link,
5098 		const struct link_resource *link_res)
5099 {
5100 	/* Link_hwss is only accessible by getter function instead of accessing
5101 	 * by pointers in dc with the intent to protect against breaking polymorphism.
5102 	 */
5103 	if (can_use_hpo_dp_link_hwss(link, link_res))
5104 		/* TODO: some assumes that if decided link settings is 128b/132b
5105 		 * channel coding format hpo_dp_link_enc should be used.
5106 		 * Others believe that if hpo_dp_link_enc is available in link
5107 		 * resource then hpo_dp_link_enc must be used. This bound between
5108 		 * hpo_dp_link_enc != NULL and decided link settings is loosely coupled
5109 		 * with a premise that both hpo_dp_link_enc pointer and decided link
5110 		 * settings are determined based on single policy function like
5111 		 * "decide_link_settings" from upper layer. This "convention"
5112 		 * cannot be maintained and enforced at current level.
5113 		 * Therefore a refactor is due so we can enforce a strong bound
5114 		 * between those two parameters at this level.
5115 		 *
5116 		 * To put it simple, we want to make enforcement at low level so that
5117 		 * we will not return link hwss if caller plans to do 8b/10b
5118 		 * with an hpo encoder. Or we can return a very dummy one that doesn't
5119 		 * do work for all functions
5120 		 */
5121 		return (requires_fixed_vs_pe_retimer_hpo_link_hwss(link) ?
5122 				get_hpo_fixed_vs_pe_retimer_dp_link_hwss() : get_hpo_dp_link_hwss());
5123 	else if (can_use_dpia_link_hwss(link, link_res))
5124 		return get_dpia_link_hwss();
5125 	else if (can_use_dio_link_hwss(link, link_res))
5126 		return (requires_fixed_vs_pe_retimer_dio_link_hwss(link)) ?
5127 				get_dio_fixed_vs_pe_retimer_link_hwss() : get_dio_link_hwss();
5128 	else
5129 		return get_virtual_link_hwss();
5130 }
5131 
is_h_timing_divisible_by_2(struct dc_stream_state * stream)5132 bool is_h_timing_divisible_by_2(struct dc_stream_state *stream)
5133 {
5134 	bool divisible = false;
5135 	uint16_t h_blank_start = 0;
5136 	uint16_t h_blank_end = 0;
5137 
5138 	if (stream) {
5139 		h_blank_start = stream->timing.h_total - stream->timing.h_front_porch;
5140 		h_blank_end = h_blank_start - stream->timing.h_addressable;
5141 
5142 		/* HTOTAL, Hblank start/end, and Hsync start/end all must be
5143 		 * divisible by 2 in order for the horizontal timing params
5144 		 * to be considered divisible by 2. Hsync start is always 0.
5145 		 */
5146 		divisible = (stream->timing.h_total % 2 == 0) &&
5147 				(h_blank_start % 2 == 0) &&
5148 				(h_blank_end % 2 == 0) &&
5149 				(stream->timing.h_sync_width % 2 == 0);
5150 	}
5151 	return divisible;
5152 }
5153 
5154 /* This interface is deprecated for new DCNs. It is replaced by the following
5155  * new interfaces. These two interfaces encapsulate pipe selection priority
5156  * with DCN specific minimum hardware transition optimization algorithm. With
5157  * the new interfaces caller no longer needs to know the implementation detail
5158  * of a pipe topology.
5159  *
5160  * resource_update_pipes_with_odm_slice_count
5161  * resource_update_pipes_with_mpc_slice_count
5162  *
5163  */
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)5164 bool dc_resource_acquire_secondary_pipe_for_mpc_odm_legacy(
5165 		const struct dc *dc,
5166 		struct dc_state *state,
5167 		struct pipe_ctx *pri_pipe,
5168 		struct pipe_ctx *sec_pipe,
5169 		bool odm)
5170 {
5171 	int pipe_idx = sec_pipe->pipe_idx;
5172 	struct pipe_ctx *sec_top, *sec_bottom, *sec_next, *sec_prev;
5173 	const struct resource_pool *pool = dc->res_pool;
5174 
5175 	sec_top = sec_pipe->top_pipe;
5176 	sec_bottom = sec_pipe->bottom_pipe;
5177 	sec_next = sec_pipe->next_odm_pipe;
5178 	sec_prev = sec_pipe->prev_odm_pipe;
5179 
5180 	if (pri_pipe == NULL)
5181 		return false;
5182 
5183 	*sec_pipe = *pri_pipe;
5184 
5185 	sec_pipe->top_pipe = sec_top;
5186 	sec_pipe->bottom_pipe = sec_bottom;
5187 	sec_pipe->next_odm_pipe = sec_next;
5188 	sec_pipe->prev_odm_pipe = sec_prev;
5189 
5190 	sec_pipe->pipe_idx = pipe_idx;
5191 	sec_pipe->plane_res.mi = pool->mis[pipe_idx];
5192 	sec_pipe->plane_res.hubp = pool->hubps[pipe_idx];
5193 	sec_pipe->plane_res.ipp = pool->ipps[pipe_idx];
5194 	sec_pipe->plane_res.xfm = pool->transforms[pipe_idx];
5195 	sec_pipe->plane_res.dpp = pool->dpps[pipe_idx];
5196 	sec_pipe->plane_res.mpcc_inst = pool->dpps[pipe_idx]->inst;
5197 	sec_pipe->stream_res.dsc = NULL;
5198 	if (odm) {
5199 		if (!sec_pipe->top_pipe)
5200 			sec_pipe->stream_res.opp = pool->opps[pipe_idx];
5201 		else
5202 			sec_pipe->stream_res.opp = sec_pipe->top_pipe->stream_res.opp;
5203 		if (sec_pipe->stream->timing.flags.DSC == 1) {
5204 #if defined(CONFIG_DRM_AMD_DC_FP)
5205 			dcn20_acquire_dsc(dc, &state->res_ctx, &sec_pipe->stream_res.dsc, sec_pipe->stream_res.opp->inst);
5206 #endif
5207 			ASSERT(sec_pipe->stream_res.dsc);
5208 			if (sec_pipe->stream_res.dsc == NULL)
5209 				return false;
5210 		}
5211 #if defined(CONFIG_DRM_AMD_DC_FP)
5212 		dcn20_build_mapped_resource(dc, state, sec_pipe->stream);
5213 #endif
5214 	}
5215 
5216 	return true;
5217 }
5218 
update_dp_encoder_resources_for_test_harness(const struct dc * dc,struct dc_state * context,struct pipe_ctx * pipe_ctx)5219 enum dc_status update_dp_encoder_resources_for_test_harness(const struct dc *dc,
5220 		struct dc_state *context,
5221 		struct pipe_ctx *pipe_ctx)
5222 {
5223 	if (dc->link_srv->dp_get_encoding_format(&pipe_ctx->link_config.dp_link_settings) == DP_128b_132b_ENCODING) {
5224 		if (pipe_ctx->stream_res.hpo_dp_stream_enc == NULL) {
5225 			pipe_ctx->stream_res.hpo_dp_stream_enc =
5226 					find_first_free_match_hpo_dp_stream_enc_for_link(
5227 							&context->res_ctx, dc->res_pool, pipe_ctx->stream);
5228 
5229 			if (!pipe_ctx->stream_res.hpo_dp_stream_enc)
5230 				return DC_NO_STREAM_ENC_RESOURCE;
5231 
5232 			update_hpo_dp_stream_engine_usage(
5233 					&context->res_ctx, dc->res_pool,
5234 					pipe_ctx->stream_res.hpo_dp_stream_enc,
5235 					true);
5236 		}
5237 
5238 		if (pipe_ctx->link_res.hpo_dp_link_enc == NULL) {
5239 			if (!add_hpo_dp_link_enc_to_ctx(&context->res_ctx, dc->res_pool, pipe_ctx, pipe_ctx->stream))
5240 				return DC_NO_LINK_ENC_RESOURCE;
5241 		}
5242 	} else {
5243 		if (pipe_ctx->stream_res.hpo_dp_stream_enc) {
5244 			update_hpo_dp_stream_engine_usage(
5245 					&context->res_ctx, dc->res_pool,
5246 					pipe_ctx->stream_res.hpo_dp_stream_enc,
5247 					false);
5248 			pipe_ctx->stream_res.hpo_dp_stream_enc = NULL;
5249 		}
5250 		if (pipe_ctx->link_res.hpo_dp_link_enc)
5251 			remove_hpo_dp_link_enc_from_ctx(&context->res_ctx, pipe_ctx, pipe_ctx->stream);
5252 	}
5253 
5254 	return DC_OK;
5255 }
5256 
check_subvp_sw_cursor_fallback_req(const struct dc * dc,struct dc_stream_state * stream)5257 bool check_subvp_sw_cursor_fallback_req(const struct dc *dc, struct dc_stream_state *stream)
5258 {
5259 	if (!dc->debug.disable_subvp_high_refresh && is_subvp_high_refresh_candidate(stream))
5260 		return true;
5261 	if (dc->current_state->stream_count == 1 && stream->timing.v_addressable >= 2880 &&
5262 			((stream->timing.pix_clk_100hz * 100) / stream->timing.v_total / stream->timing.h_total) < 120)
5263 		return true;
5264 	else if (dc->current_state->stream_count > 1 && stream->timing.v_addressable >= 1080 &&
5265 			((stream->timing.pix_clk_100hz * 100) / stream->timing.v_total / stream->timing.h_total) < 120)
5266 		return true;
5267 
5268 	return false;
5269 }
5270 
resource_get_dscl_prog_data(struct pipe_ctx * pipe_ctx)5271 struct dscl_prog_data *resource_get_dscl_prog_data(struct pipe_ctx *pipe_ctx)
5272 {
5273 	return &pipe_ctx->plane_res.scl_data.dscl_prog_data;
5274 }
5275 
resource_init_common_dml2_callbacks(struct dc * dc,struct dml2_configuration_options * dml2_options)5276 void resource_init_common_dml2_callbacks(struct dc *dc, struct dml2_configuration_options *dml2_options)
5277 {
5278 	dml2_options->callbacks.dc = dc;
5279 	dml2_options->callbacks.build_scaling_params = &resource_build_scaling_params;
5280 	dml2_options->callbacks.build_test_pattern_params = &resource_build_test_pattern_params;
5281 	dml2_options->callbacks.acquire_secondary_pipe_for_mpc_odm = &dc_resource_acquire_secondary_pipe_for_mpc_odm_legacy;
5282 	dml2_options->callbacks.update_pipes_for_stream_with_slice_count = &resource_update_pipes_for_stream_with_slice_count;
5283 	dml2_options->callbacks.update_pipes_for_plane_with_slice_count = &resource_update_pipes_for_plane_with_slice_count;
5284 	dml2_options->callbacks.get_mpc_slice_index = &resource_get_mpc_slice_index;
5285 	dml2_options->callbacks.get_mpc_slice_count = &resource_get_mpc_slice_count;
5286 	dml2_options->callbacks.get_odm_slice_index = &resource_get_odm_slice_index;
5287 	dml2_options->callbacks.get_odm_slice_count = &resource_get_odm_slice_count;
5288 	dml2_options->callbacks.get_opp_head = &resource_get_opp_head;
5289 	dml2_options->callbacks.get_otg_master_for_stream = &resource_get_otg_master_for_stream;
5290 	dml2_options->callbacks.get_opp_heads_for_otg_master = &resource_get_opp_heads_for_otg_master;
5291 	dml2_options->callbacks.get_dpp_pipes_for_plane = &resource_get_dpp_pipes_for_plane;
5292 	dml2_options->callbacks.get_stream_status = &dc_state_get_stream_status;
5293 	dml2_options->callbacks.get_stream_from_id = &dc_state_get_stream_from_id;
5294 	dml2_options->callbacks.get_max_flickerless_instant_vtotal_increase = &dc_stream_get_max_flickerless_instant_vtotal_increase;
5295 
5296 	dml2_options->svp_pstate.callbacks.dc = dc;
5297 	dml2_options->svp_pstate.callbacks.add_phantom_plane = &dc_state_add_phantom_plane;
5298 	dml2_options->svp_pstate.callbacks.add_phantom_stream = &dc_state_add_phantom_stream;
5299 	dml2_options->svp_pstate.callbacks.build_scaling_params = &resource_build_scaling_params;
5300 	dml2_options->svp_pstate.callbacks.create_phantom_plane = &dc_state_create_phantom_plane;
5301 	dml2_options->svp_pstate.callbacks.remove_phantom_plane = &dc_state_remove_phantom_plane;
5302 	dml2_options->svp_pstate.callbacks.remove_phantom_stream = &dc_state_remove_phantom_stream;
5303 	dml2_options->svp_pstate.callbacks.create_phantom_stream = &dc_state_create_phantom_stream;
5304 	dml2_options->svp_pstate.callbacks.release_phantom_plane = &dc_state_release_phantom_plane;
5305 	dml2_options->svp_pstate.callbacks.release_phantom_stream = &dc_state_release_phantom_stream;
5306 	dml2_options->svp_pstate.callbacks.get_pipe_subvp_type = &dc_state_get_pipe_subvp_type;
5307 	dml2_options->svp_pstate.callbacks.get_stream_subvp_type = &dc_state_get_stream_subvp_type;
5308 	dml2_options->svp_pstate.callbacks.get_paired_subvp_stream = &dc_state_get_paired_subvp_stream;
5309 	dml2_options->svp_pstate.callbacks.remove_phantom_streams_and_planes = &dc_state_remove_phantom_streams_and_planes;
5310 	dml2_options->svp_pstate.callbacks.release_phantom_streams_and_planes = &dc_state_release_phantom_streams_and_planes;
5311 }
5312 
5313 /* 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)5314 int resource_calculate_det_for_stream(struct dc_state *state, struct pipe_ctx *otg_master)
5315 {
5316 	struct pipe_ctx *opp_heads[MAX_PIPES];
5317 	struct pipe_ctx *dpp_pipes[MAX_PIPES];
5318 
5319 	int dpp_count = 0;
5320 	int det_segments = 0;
5321 
5322 	if (!otg_master->stream)
5323 		return 0;
5324 
5325 	int slice_count = resource_get_opp_heads_for_otg_master(otg_master,
5326 			&state->res_ctx, opp_heads);
5327 
5328 	for (int slice_idx = 0; slice_idx < slice_count; slice_idx++) {
5329 		if (opp_heads[slice_idx]->plane_state) {
5330 			dpp_count = resource_get_dpp_pipes_for_opp_head(
5331 					opp_heads[slice_idx],
5332 					&state->res_ctx,
5333 					dpp_pipes);
5334 			for (int dpp_idx = 0; dpp_idx < dpp_count; dpp_idx++)
5335 				det_segments += dpp_pipes[dpp_idx]->hubp_regs.det_size;
5336 		}
5337 	}
5338 	return det_segments;
5339 }
5340 
resource_is_hpo_acquired(struct dc_state * context)5341 bool resource_is_hpo_acquired(struct dc_state *context)
5342 {
5343 	int i;
5344 
5345 	for (i = 0; i < MAX_HPO_DP2_ENCODERS; i++) {
5346 		if (context->res_ctx.is_hpo_dp_stream_enc_acquired[i]) {
5347 			return true;
5348 		}
5349 	}
5350 
5351 	return false;
5352 }
5353