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