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