xref: /linux/drivers/gpu/drm/amd/display/dc/link/link_dpms.c (revision 952df08d5b2d39d841da69ff5e58be924886abf8)
1 /*
2  * Copyright 2023 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 /* FILE POLICY AND INTENDED USAGE:
27  * This file owns the programming sequence of stream's dpms state associated
28  * with the link and link's enable/disable sequences as result of the stream's
29  * dpms state change.
30  *
31  * TODO - The reason link owns stream's dpms programming sequence is
32  * because dpms programming sequence is highly dependent on underlying signal
33  * specific link protocols. This unfortunately causes link to own a portion of
34  * stream state programming sequence. This creates a gray area where the
35  * boundary between link and stream is not clearly defined.
36  */
37 
38 #include "link_dpms.h"
39 #include "link_hwss.h"
40 #include "link_validation.h"
41 #include "accessories/link_dp_trace.h"
42 #include "protocols/link_dpcd.h"
43 #include "protocols/link_ddc.h"
44 #include "protocols/link_hpd.h"
45 #include "protocols/link_dp_phy.h"
46 #include "protocols/link_dp_capability.h"
47 #include "protocols/link_dp_training.h"
48 #include "protocols/link_edp_panel_control.h"
49 #include "protocols/link_dp_panel_replay.h"
50 #include "protocols/link_dp_dpia_bw.h"
51 #include "link/protocols/link_hdmi_frl.h"
52 
53 #include "dm_helpers.h"
54 #include "link_enc_cfg.h"
55 #include "resource.h"
56 #include "dsc.h"
57 #include "dccg.h"
58 #include "clk_mgr.h"
59 #include "atomfirmware.h"
60 #include "vpg.h"
61 
62 #define DC_LOGGER \
63 	dc_logger
64 #define DC_LOGGER_INIT(logger) \
65 	struct dal_logger *dc_logger = logger
66 
67 #define LINK_INFO(...) \
68 	DC_LOG_HW_HOTPLUG(  \
69 		__VA_ARGS__)
70 
71 #define RETIMER_REDRIVER_INFO(...) \
72 	DC_LOG_RETIMER_REDRIVER(  \
73 		__VA_ARGS__)
74 
75 #define FRL_INFO(...) \
76 	DC_LOG_HDMI_FRL_LTP(  \
77 		__VA_ARGS__)
78 
79 #define MAX_MTP_SLOT_COUNT 64
80 #define LINK_TRAINING_ATTEMPTS 4
81 #define PEAK_FACTOR_X1000 1006
82 
83 void link_blank_all_dp_displays(struct dc *dc)
84 {
85 	unsigned int i;
86 	uint8_t dpcd_power_state = '\0';
87 	enum dc_status status = DC_ERROR_UNEXPECTED;
88 
89 	for (i = 0; i < dc->link_count; i++) {
90 		if ((dc->links[i]->connector_signal != SIGNAL_TYPE_DISPLAY_PORT) ||
91 			(dc->links[i]->priv == NULL) || (dc->links[i]->local_sink == NULL))
92 			continue;
93 
94 		/* DP 2.0 spec requires that we read LTTPR caps first */
95 		dp_retrieve_lttpr_cap(dc->links[i]);
96 		/* if any of the displays are lit up turn them off */
97 		status = core_link_read_dpcd(dc->links[i], DP_SET_POWER,
98 							&dpcd_power_state, sizeof(dpcd_power_state));
99 
100 		if (status == DC_OK && dpcd_power_state == DP_POWER_STATE_D0)
101 			link_blank_dp_stream(dc->links[i], true);
102 	}
103 
104 }
105 
106 void link_blank_all_edp_displays(struct dc *dc)
107 {
108 	unsigned int i;
109 	uint8_t dpcd_power_state = '\0';
110 	enum dc_status status = DC_ERROR_UNEXPECTED;
111 
112 	for (i = 0; i < dc->link_count; i++) {
113 		if ((dc->links[i]->connector_signal != SIGNAL_TYPE_EDP) ||
114 			(!dc->links[i]->edp_sink_present))
115 			continue;
116 
117 		/* if any of the displays are lit up turn them off */
118 		status = core_link_read_dpcd(dc->links[i], DP_SET_POWER,
119 							&dpcd_power_state, sizeof(dpcd_power_state));
120 
121 		if (status == DC_OK && dpcd_power_state == DP_POWER_STATE_D0)
122 			link_blank_dp_stream(dc->links[i], true);
123 	}
124 }
125 
126 void link_blank_dp_stream(struct dc_link *link, bool hw_init)
127 {
128 	unsigned int j;
129 	struct dc  *dc = link->ctx->dc;
130 	enum signal_type signal = link->connector_signal;
131 
132 	if ((signal == SIGNAL_TYPE_EDP) ||
133 		(signal == SIGNAL_TYPE_DISPLAY_PORT)) {
134 		if (link->ep_type == DISPLAY_ENDPOINT_PHY &&
135 			link->link_enc->funcs->get_dig_frontend &&
136 			link->link_enc->funcs->is_dig_enabled(link->link_enc)) {
137 			int fe = link->link_enc->funcs->get_dig_frontend(link->link_enc);
138 
139 			if (fe != ENGINE_ID_UNKNOWN)
140 				for (j = 0; j < dc->res_pool->stream_enc_count; j++) {
141 					if (fe == dc->res_pool->stream_enc[j]->id) {
142 						dc->res_pool->stream_enc[j]->funcs->dp_blank(link,
143 									dc->res_pool->stream_enc[j]);
144 						break;
145 					}
146 				}
147 		}
148 
149 		if (((!dc->is_switch_in_progress_dest) && ((!link->wa_flags.dp_keep_receiver_powered) || hw_init)) &&
150 			(link->type != dc_connection_none))
151 			dpcd_write_rx_power_ctrl(link, false);
152 	}
153 }
154 
155 void link_set_all_streams_dpms_off_for_link(struct dc_link *link)
156 {
157 	struct pipe_ctx *pipes[MAX_PIPES];
158 	struct dc_stream_state *streams[MAX_PIPES];
159 	struct dc_state *state = link->dc->current_state;
160 	uint8_t count;
161 	int i;
162 	struct dc_stream_update stream_update;
163 	bool dpms_off = true;
164 	struct link_resource link_res = {0};
165 
166 	memset(&stream_update, 0, sizeof(stream_update));
167 	stream_update.dpms_off = &dpms_off;
168 
169 	link_get_master_pipes_with_dpms_on(link, state, &count, pipes);
170 
171 	/* The subsequent call to dc_commit_updates_for_stream for a full update
172 	 * will release the current state and swap to a new state. Releasing the
173 	 * current state results in the stream pointers in the pipe_ctx structs
174 	 * to be zero'd. Hence, cache all streams prior to dc_commit_updates_for_stream.
175 	 */
176 	for (i = 0; i < count; i++)
177 		streams[i] = pipes[i]->stream;
178 
179 	for (i = 0; i < count; i++) {
180 		stream_update.stream = streams[i];
181 		dc_commit_updates_for_stream(link->ctx->dc, NULL, 0,
182 				streams[i], &stream_update,
183 				state);
184 	}
185 
186 	/* link can be also enabled by vbios. In this case it is not recorded
187 	 * in pipe_ctx. Disable link phy here to make sure it is completely off
188 	 */
189 	if (dc_is_dp_signal(link->connector_signal))
190 		dp_disable_link_phy(link, &link_res, link->connector_signal);
191 }
192 
193 void link_resume(struct dc_link *link)
194 {
195 	if (link->connector_signal != SIGNAL_TYPE_VIRTUAL)
196 		program_hpd_filter(link);
197 }
198 
199 /* This function returns true if the pipe is used to feed video signal directly
200  * to the link.
201  */
202 static bool is_master_pipe_for_link(const struct dc_link *link,
203 		const struct pipe_ctx *pipe)
204 {
205 	return resource_is_pipe_type(pipe, OTG_MASTER) &&
206 			pipe->stream->link == link;
207 }
208 
209 /*
210  * This function finds all master pipes feeding to a given link with dpms set to
211  * on in given dc state.
212  */
213 void link_get_master_pipes_with_dpms_on(const struct dc_link *link,
214 		struct dc_state *state,
215 		uint8_t *count,
216 		struct pipe_ctx *pipes[MAX_PIPES])
217 {
218 	int i;
219 	struct pipe_ctx *pipe = NULL;
220 
221 	*count = 0;
222 	for (i = 0; i < MAX_PIPES; i++) {
223 		pipe = &state->res_ctx.pipe_ctx[i];
224 
225 		if (is_master_pipe_for_link(link, pipe) &&
226 				pipe->stream->dpms_off == false) {
227 			pipes[(*count)++] = pipe;
228 		}
229 	}
230 }
231 
232 static struct ext_hdmi_settings create_ext_hdmi_settings(
233 		uint8_t address,
234 		uint8_t reg_num,
235 		uint8_t reg_num_6g,
236 		const struct i2c_reg_info *reg_settings,
237 		const struct i2c_reg_info *reg_settings_6g
238 )
239 {
240 	struct ext_hdmi_settings result = {
241 		.slv_addr = address,
242 		.reg_num = reg_num,
243 		.reg_num_6g = reg_num_6g,
244 	};
245 
246 	memcpy(result.reg_settings, reg_settings, sizeof(result.reg_settings));
247 	memcpy(result.reg_settings_6g, reg_settings_6g, sizeof(result.reg_settings_6g));
248 	return result;
249 }
250 
251 static bool get_ext_hdmi_settings(
252 		const struct integrated_info *info,
253 		enum engine_id eng_id,
254 		struct ext_hdmi_settings *settings
255 )
256 {
257 	if (!settings || !info)
258 		return false;
259 
260 	/*
261 	 * Get retimer settings from sbios for passing SI eye test for DCE11
262 	 * The setting values are varied based on board revision and port id
263 	 * Therefore the setting values of each ports is passed by sbios.
264 	 */
265 
266 	// Check if current bios contains ext Hdmi settings
267 	if (!(info->gpu_cap_info & 0x20))
268 		return false;
269 
270 	switch (eng_id) {
271 	case ENGINE_ID_DIGA:
272 		*settings = create_ext_hdmi_settings(
273 				info->dp0_ext_hdmi_slv_addr,
274 				info->dp0_ext_hdmi_reg_num,
275 				info->dp0_ext_hdmi_6g_reg_num,
276 				info->dp0_ext_hdmi_reg_settings,
277 				info->dp0_ext_hdmi_6g_reg_settings
278 		);
279 		break;
280 	case ENGINE_ID_DIGB:
281 		*settings = create_ext_hdmi_settings(
282 				info->dp1_ext_hdmi_slv_addr,
283 				info->dp1_ext_hdmi_reg_num,
284 				info->dp1_ext_hdmi_6g_reg_num,
285 				info->dp1_ext_hdmi_reg_settings,
286 				info->dp1_ext_hdmi_6g_reg_settings
287 		);
288 		break;
289 	case ENGINE_ID_DIGC:
290 		*settings = create_ext_hdmi_settings(
291 				info->dp2_ext_hdmi_slv_addr,
292 				info->dp2_ext_hdmi_reg_num,
293 				info->dp2_ext_hdmi_6g_reg_num,
294 				info->dp2_ext_hdmi_reg_settings,
295 				info->dp2_ext_hdmi_6g_reg_settings
296 		);
297 		break;
298 	case ENGINE_ID_DIGD:
299 		*settings = create_ext_hdmi_settings(
300 				info->dp3_ext_hdmi_slv_addr,
301 				info->dp3_ext_hdmi_reg_num,
302 				info->dp3_ext_hdmi_6g_reg_num,
303 				info->dp3_ext_hdmi_reg_settings,
304 				info->dp3_ext_hdmi_6g_reg_settings
305 		);
306 		break;
307 	default:
308 		return false;
309 	}
310 
311 	// Validate settings from bios integrated info table
312 	if (
313 			!settings->slv_addr
314 			|| settings->reg_num > ARRAY_SIZE(settings->reg_settings)
315 			|| settings->reg_num_6g > ARRAY_SIZE(settings->reg_settings_6g)
316 	) {
317 		return false;
318 	}
319 
320 	for (size_t i = 0; i < settings->reg_num; i++) {
321 		if (settings->reg_settings[i].i2c_reg_index > 0x20)
322 			return false;
323 	}
324 
325 	for (size_t i = 0; i < settings->reg_num_6g; i++) {
326 		if (settings->reg_settings_6g[i].i2c_reg_index > 0x20)
327 			return false;
328 	}
329 	return true;
330 }
331 
332 static bool write_i2c(
333 		const struct dc_link *link,
334 		uint8_t address,
335 		uint8_t *buffer,
336 		uint32_t length
337 )
338 {
339 	struct i2c_payload payload = {
340 		.write = true,
341 		.address = address,
342 		.length = length,
343 		.data = buffer,
344 	};
345 	struct i2c_command cmd = {
346 		.payloads = &payload,
347 		.number_of_payloads = 1,
348 		.engine = I2C_COMMAND_ENGINE_DEFAULT,
349 		.speed = link->ctx->dc->caps.i2c_speed_in_khz,
350 	};
351 
352 	return dm_helpers_submit_i2c(link->ctx, link, &cmd);
353 }
354 
355 static bool write_i2c_retimer_offset_value(
356 		const struct dc_link *link,
357 		uint8_t address,
358 		uint8_t offset,
359 		uint8_t value
360 )
361 {
362 	DC_LOGGER_INIT(link->ctx->logger);
363 	uint8_t buffer[] = { offset, value };
364 	const bool success = write_i2c(link, address, buffer, sizeof(buffer));
365 
366 	RETIMER_REDRIVER_INFO(
367 			"Retimer write, address: 0x%x, offset: 0x%x, value: 0x%x, success: %d\n",
368 			address, offset, value, success
369 	);
370 	return success;
371 }
372 
373 static bool write_i2c_retimer_vga(
374 		const struct dc_link *link,
375 		uint8_t address
376 )
377 {
378 	DC_LOGGER_INIT(link->ctx->logger);
379 	const uint8_t vga_data[][2] = {
380 		{ 0xFF, 0x01 },
381 		{ 0x00, 0x23 },
382 		{ 0xFF, 0x00 },
383 	};
384 
385 	for (size_t i = 0; i < ARRAY_SIZE(vga_data); i++) {
386 		if (!write_i2c_retimer_offset_value(link, address, vga_data[i][0], vga_data[i][1])) {
387 			DC_LOG_ERROR("Set retimer failed, vga index: %zu\n", i);
388 			return false;
389 		}
390 	}
391 	return true;
392 }
393 
394 static bool write_i2c_retimer_byte(
395 		const struct dc_link *link,
396 		uint8_t address,
397 		uint8_t index,
398 		uint8_t value
399 )
400 {
401 	DC_LOGGER_INIT(link->ctx->logger);
402 	const uint8_t apply_rx_tx_change = 0x4;
403 
404 	if (index > 0x20)
405 		return true;
406 
407 	if (!write_i2c_retimer_offset_value(link, address, index, value)) {
408 		DC_LOG_ERROR("Set retimer failed, 3g index: 0x%x, value: 0x%x\n", index, value);
409 		return false;
410 	}
411 
412 	// Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
413 	// needs to be set to 1 on every 0x0A-0x0C write.
414 	if (0x0A <= index && index <= 0x0C) {
415 		uint8_t offset = 0x0A;
416 
417 		// Query current value from offset 0x0A
418 		if (index == 0x0A) {
419 			// Just written correct value, so no need to read it back
420 		} else {
421 			if (!link_query_ddc_data(
422 					link->ddc, address, &offset, 1, &value, 1
423 			)) {
424 				DC_LOG_ERROR("Set retimer failed, link_query_ddc_data\n");
425 				return false;
426 			}
427 		}
428 
429 		value |= apply_rx_tx_change;
430 		if (!write_i2c_retimer_offset_value(link, address, offset, value)) {
431 			DC_LOG_ERROR("Set retimer failed, 3g offset: 0x%x, value: 0x%x\n", offset, value);
432 			return false;
433 		}
434 	}
435 	return true;
436 }
437 
438 static bool write_i2c_retimer_setting(
439 		const struct dc_link *link,
440 		bool is_vga_mode,
441 		bool is_over_340mhz,
442 		struct ext_hdmi_settings *settings)
443 {
444 	DC_LOGGER_INIT(link->ctx->logger);
445 	const uint8_t address = settings->slv_addr >> 1;
446 
447 	for (size_t i = 0; i < settings->reg_num; i++) {
448 		const uint8_t index = settings->reg_settings[i].i2c_reg_index;
449 		uint8_t value = settings->reg_settings[i].i2c_reg_val;
450 
451 		if (!write_i2c_retimer_byte(link, address, index, value)) {
452 			DC_LOG_ERROR("Set retimer failed, index: %zu\n", i);
453 			return false;
454 		}
455 	}
456 
457 	if (is_over_340mhz) {
458 		for (size_t i = 0; i < settings->reg_num_6g; i++) {
459 			const uint8_t index = settings->reg_settings_6g[i].i2c_reg_index;
460 			uint8_t value = settings->reg_settings_6g[i].i2c_reg_val;
461 
462 			if (!write_i2c_retimer_byte(link, address, index, value)) {
463 				DC_LOG_ERROR("Set retimer failed, 6g index: %zu\n", i);
464 				return false;
465 			}
466 		}
467 	}
468 
469 	if (is_vga_mode) {
470 		return write_i2c_retimer_vga(link, address);
471 	}
472 	return true;
473 }
474 
475 static bool write_i2c_default_retimer_setting(
476 		const struct dc_link *link,
477 		bool is_vga_mode,
478 		bool is_over_340mhz)
479 {
480 	const uint8_t address = 0xBA >> 1;
481 
482 	DC_LOGGER_INIT(link->ctx->logger);
483 
484 	const uint8_t data[][2] = {
485 		{ 0x0A, 0x13 },
486 		{ 0x0A, 0x17 },
487 		{ 0x0B, is_over_340mhz ? 0xDA : 0xD8 },
488 		{ 0x0A, 0x17 },
489 		{ 0x0C, is_over_340mhz ? 0x1D : 0x91 },
490 		{ 0x0A, 0x17 },
491 	};
492 
493 	for (size_t i = 0; i < ARRAY_SIZE(data); i++) {
494 		if (!write_i2c_retimer_offset_value(link, address, data[i][0], data[i][1])) {
495 			DC_LOG_ERROR("Set default retimer failed, index: %zu\n", i);
496 			return false;
497 		}
498 	}
499 
500 	if (is_vga_mode) {
501 		return write_i2c_retimer_vga(link, address);
502 	}
503 	return true;
504 }
505 
506 static bool write_i2c_redriver_setting(
507 		const struct dc_link *link,
508 		bool is_over_340mhz)
509 {
510 	DC_LOGGER_INIT(link->ctx->logger);
511 	const uint8_t address = 0xF0 >> 1;
512 	uint8_t buffer[16] = {
513 		[3] = 0x4E,
514 		[4] = 0x4E,
515 		[5] = 0x4E,
516 		[6] = is_over_340mhz ? 0x4E : 0x4A,
517 	};
518 
519 	const bool success = write_i2c(link, address, buffer, sizeof(buffer));
520 
521 	RETIMER_REDRIVER_INFO(
522 			"Redriver write, address: 0x%x, buffer: { [3]: 0x%x, 0x%x, 0x%x, 0x%x }, success: %d\n",
523 			address, buffer[3], buffer[4], buffer[5], buffer[6], success
524 	);
525 
526 	if (!success)
527 		DC_LOG_ERROR("Set redriver failed");
528 	return success;
529 }
530 
531 static void update_psp_stream_config(struct pipe_ctx *pipe_ctx, bool dpms_off)
532 {
533 	struct cp_psp *cp_psp = &pipe_ctx->stream->ctx->cp_psp;
534 	struct link_encoder *link_enc = pipe_ctx->link_res.dio_link_enc;
535 	struct cp_psp_stream_config config = {0};
536 	enum dp_panel_mode panel_mode =
537 			dp_get_panel_mode(pipe_ctx->stream->link);
538 
539 	if (cp_psp == NULL || cp_psp->funcs.update_stream_config == NULL)
540 		return;
541 	if (!pipe_ctx->stream->ctx->dc->config.unify_link_enc_assignment)
542 		link_enc = link_enc_cfg_get_link_enc(pipe_ctx->stream->link);
543 	ASSERT(link_enc);
544 	if (link_enc == NULL)
545 		return;
546 
547 	/* otg instance */
548 	config.otg_inst = (uint8_t) pipe_ctx->stream_res.tg->inst;
549 
550 	/* dig front end */
551 	config.dig_fe = (uint8_t) pipe_ctx->stream_res.stream_enc->stream_enc_inst;
552 
553 	/* stream encoder index */
554 	config.stream_enc_idx = (uint8_t)(pipe_ctx->stream_res.stream_enc->id - ENGINE_ID_DIGA);
555 	if (dp_is_128b_132b_signal(pipe_ctx))
556 		config.stream_enc_idx =
557 				(uint8_t)(pipe_ctx->stream_res.hpo_dp_stream_enc->id - ENGINE_ID_HPO_DP_0);
558 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
559 		config.stream_enc_idx =
560 				(uint8_t)(pipe_ctx->stream_res.hpo_frl_stream_enc->id - ENGINE_ID_HPO_0);
561 
562 	/* dig back end */
563 	config.dig_be = pipe_ctx->stream->link->link_enc_hw_inst;
564 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
565 		config.dig_be = (uint8_t)pipe_ctx->stream_res.hpo_frl_stream_enc->stream_enc_inst;
566 
567 	/* link encoder index */
568 	config.link_enc_idx = (uint8_t)(link_enc->transmitter - TRANSMITTER_UNIPHY_A);
569 	if (dp_is_128b_132b_signal(pipe_ctx))
570 		config.link_enc_idx = (uint8_t)pipe_ctx->link_res.hpo_dp_link_enc->inst;
571 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
572 		config.link_enc_idx = (uint8_t)pipe_ctx->stream->link->hpo_frl_link_enc->inst;
573 
574 	/* dio output index is dpia index for DPIA endpoint & dcio index by default */
575 	if (pipe_ctx->stream->link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA)
576 		config.dio_output_idx = (uint8_t)(pipe_ctx->stream->link->link_id.enum_id - ENUM_ID_1);
577 	else
578 		config.dio_output_idx = (uint8_t)(link_enc->transmitter - TRANSMITTER_UNIPHY_A);
579 
580 
581 	/* phy index */
582 	config.phy_idx = resource_transmitter_to_phy_idx(
583 			pipe_ctx->stream->link->dc, link_enc->transmitter);
584 	if (pipe_ctx->stream->link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA)
585 		/* USB4 DPIA doesn't use PHY in our soc, initialize it to 0 */
586 		config.phy_idx = 0;
587 
588 	/* stream properties */
589 	config.assr_enabled = (panel_mode == DP_PANEL_MODE_EDP) ? 1 : 0;
590 	config.mst_enabled = (pipe_ctx->stream->signal ==
591 			SIGNAL_TYPE_DISPLAY_PORT_MST) ? 1 : 0;
592 	config.frl_enabled = dc_is_hdmi_frl_signal(pipe_ctx->stream->signal) ? 1 : 0;
593 	config.dp2_enabled = dp_is_128b_132b_signal(pipe_ctx) ? 1 : 0;
594 	config.usb4_enabled = (pipe_ctx->stream->link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA) ?
595 			1 : 0;
596 	config.dpms_off = dpms_off;
597 
598 	/* dm stream context */
599 	config.dm_stream_ctx = pipe_ctx->stream->dm_stream_context;
600 
601 	cp_psp->funcs.update_stream_config(cp_psp->handle, &config);
602 }
603 
604 void link_wait_for_unlocked(struct dc_link *link)
605 {
606 	unsigned long long enter_timestamp;
607 	unsigned long long finish_timestamp;
608 	unsigned long long time_taken_in_ns;
609 	bool waited = false;
610 
611 	DC_LOGGER_INIT(link->ctx->logger);
612 
613 	enter_timestamp = dm_get_timestamp(link->ctx);
614 
615 	while (link->is_link_locked) { // busy wait
616 		if (!waited) {
617 			DC_LOG_DC("%s: 0x%p ...", __func__, link);
618 
619 			waited = true;
620 		}
621 
622 		udelay(1);
623 	}
624 
625 	if (!waited)
626 		return;
627 
628 	finish_timestamp = dm_get_timestamp(link->ctx);
629 	time_taken_in_ns = dm_get_elapse_time_in_ns(link->ctx,
630 							finish_timestamp, enter_timestamp);
631 
632 	DC_LOG_DC("%s: 0x%p took %llu ms.", __func__,
633 			link, div_u64(time_taken_in_ns, 1000000));
634 }
635 
636 static void set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
637 {
638 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
639 
640 	if (!dc_is_hdmi_signal(pipe_ctx->stream->signal))
641 		return;
642 
643 	if (pipe_ctx->stream->timing.flags.DSC)
644 		return;
645 
646 	dc->hwss.set_avmute(pipe_ctx, enable);
647 }
648 
649 static void enable_mst_on_sink(struct dc_link *link, bool enable)
650 {
651 	unsigned char mstmCntl = 0;
652 
653 	core_link_read_dpcd(link, DP_MSTM_CTRL, &mstmCntl, 1);
654 	if (enable)
655 		mstmCntl |= DP_MST_EN;
656 	else
657 		mstmCntl &= (~DP_MST_EN);
658 
659 	core_link_write_dpcd(link, DP_MSTM_CTRL, &mstmCntl, 1);
660 }
661 
662 static void dsc_optc_config_log(struct display_stream_compressor *dsc,
663 		struct dsc_optc_config *config)
664 {
665 	uint32_t precision = 1 << 28;
666 	uint32_t bytes_per_pixel_int = config->bytes_per_pixel / precision;
667 	uint32_t bytes_per_pixel_mod = config->bytes_per_pixel % precision;
668 	uint64_t ll_bytes_per_pix_fraq = bytes_per_pixel_mod;
669 	DC_LOGGER_INIT(dsc->ctx->logger);
670 
671 	/* 7 fractional digits decimal precision for bytes per pixel is enough because DSC
672 	 * bits per pixel precision is 1/16th of a pixel, which means bytes per pixel precision is
673 	 * 1/16/8 = 1/128 of a byte, or 0.0078125 decimal
674 	 */
675 	ll_bytes_per_pix_fraq *= 10000000;
676 	ll_bytes_per_pix_fraq /= precision;
677 
678 	DC_LOG_DSC("\tbytes_per_pixel 0x%08x (%d.%07d)",
679 			config->bytes_per_pixel, bytes_per_pixel_int, (uint32_t)ll_bytes_per_pix_fraq);
680 	DC_LOG_DSC("\tis_pixel_format_444 %d", config->is_pixel_format_444);
681 	DC_LOG_DSC("\tslice_width %d", config->slice_width);
682 }
683 
684 static bool dp_set_dsc_on_rx(struct pipe_ctx *pipe_ctx, bool enable)
685 {
686 	struct dc *dc = pipe_ctx->stream->ctx->dc;
687 	struct dc_stream_state *stream = pipe_ctx->stream;
688 	bool result = false;
689 
690 	if (dc_is_virtual_signal(stream->signal))
691 		result = true;
692 	else
693 		result = dm_helpers_dp_write_dsc_enable(dc->ctx, stream, enable);
694 	return result;
695 }
696 
697 static bool dp_set_hblank_reduction_on_rx(struct pipe_ctx *pipe_ctx)
698 {
699 	struct dc *dc = pipe_ctx->stream->ctx->dc;
700 	struct dc_stream_state *stream = pipe_ctx->stream;
701 	bool result = false;
702 
703 	if (dc_is_virtual_signal(stream->signal))
704 		result = true;
705 	else
706 		result = dm_helpers_dp_write_hblank_reduction(dc->ctx, stream);
707 	return result;
708 }
709 
710 
711 /* The stream with these settings can be sent (unblanked) only after DSC was enabled on RX first,
712  * i.e. after dp_enable_dsc_on_rx() had been called
713  */
714 void link_set_dsc_on_stream(struct pipe_ctx *pipe_ctx, bool enable)
715 {
716 	/* TODO: Move this to HWSS as this is hardware programming sequence not a
717 	 * link layer sequence
718 	 */
719 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
720 	struct dc *dc = pipe_ctx->stream->ctx->dc;
721 	struct dc_stream_state *stream = pipe_ctx->stream;
722 	struct pipe_ctx *odm_pipe;
723 	int opp_cnt = 1;
724 	struct dccg *dccg = dc->res_pool->dccg;
725 	/* It has been found that when DSCCLK is lower than 16Mhz, we will get DCN
726 	 * register access hung. When DSCCLk is based on refclk, DSCCLk is always a
727 	 * fixed value higher than 16Mhz so the issue doesn't occur. When DSCCLK is
728 	 * generated by DTO, DSCCLK would be based on 1/3 dispclk. For small timings
729 	 * with DSC such as 480p60Hz, the dispclk could be low enough to trigger
730 	 * this problem. We are implementing a workaround here to keep using dscclk
731 	 * based on fixed value refclk when timing is smaller than 3x16Mhz (i.e
732 	 * 48Mhz) pixel clock to avoid hitting this problem.
733 	 */
734 	bool should_use_dto_dscclk = (dccg->funcs->set_dto_dscclk != NULL) &&
735 			stream->timing.pix_clk_100hz > 480000;
736 	DC_LOGGER_INIT(dsc->ctx->logger);
737 
738 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
739 		opp_cnt++;
740 
741 	if (enable) {
742 		struct dsc_config dsc_cfg;
743 		struct dsc_optc_config dsc_optc_cfg = {0};
744 		enum optc_dsc_mode optc_dsc_mode;
745 
746 		/* Enable DSC hw block */
747 		dsc_cfg.pic_width = (stream->timing.h_addressable + pipe_ctx->dsc_padding_params.dsc_hactive_padding +
748 				stream->timing.h_border_left + stream->timing.h_border_right) / opp_cnt;
749 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top + stream->timing.v_border_bottom;
750 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
751 		dsc_cfg.color_depth = stream->timing.display_color_depth;
752 		dsc_cfg.is_odm = pipe_ctx->next_odm_pipe ? true : false;
753 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
754 		ASSERT(dsc_cfg.dc_dsc_cfg.num_slices_h % opp_cnt == 0);
755 		dsc_cfg.dc_dsc_cfg.num_slices_h /= opp_cnt;
756 		dsc_cfg.dsc_padding = 0;
757 
758 		if (should_use_dto_dscclk)
759 			dccg->funcs->set_dto_dscclk(dccg, dsc->inst, dsc_cfg.dc_dsc_cfg.num_slices_h);
760 		dsc->funcs->dsc_set_config(dsc, &dsc_cfg, &dsc_optc_cfg);
761 		dsc->funcs->dsc_enable(dsc, pipe_ctx->stream_res.opp->inst);
762 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
763 			struct display_stream_compressor *odm_dsc = odm_pipe->stream_res.dsc;
764 
765 			if (should_use_dto_dscclk)
766 				dccg->funcs->set_dto_dscclk(dccg, odm_dsc->inst, dsc_cfg.dc_dsc_cfg.num_slices_h);
767 			odm_dsc->funcs->dsc_set_config(odm_dsc, &dsc_cfg, &dsc_optc_cfg);
768 			odm_dsc->funcs->dsc_enable(odm_dsc, odm_pipe->stream_res.opp->inst);
769 		}
770 		dsc_cfg.dc_dsc_cfg.num_slices_h *= opp_cnt;
771 		dsc_cfg.pic_width *= opp_cnt;
772 		dsc_cfg.dsc_padding = pipe_ctx->dsc_padding_params.dsc_hactive_padding;
773 
774 		optc_dsc_mode = dsc_optc_cfg.is_pixel_format_444 ? OPTC_DSC_ENABLED_444 : OPTC_DSC_ENABLED_NATIVE_SUBSAMPLED;
775 
776 		/* Enable DSC in encoder */
777 		if (dc_is_dp_signal(stream->signal) && !dp_is_128b_132b_signal(pipe_ctx)) {
778 			DC_LOG_DSC("Setting stream encoder DSC config for engine %d:", (int)pipe_ctx->stream_res.stream_enc->id);
779 			dsc_optc_config_log(dsc, &dsc_optc_cfg);
780 			if (pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_config)
781 				pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_config(pipe_ctx->stream_res.stream_enc,
782 										optc_dsc_mode,
783 										dsc_optc_cfg.bytes_per_pixel,
784 										dsc_optc_cfg.slice_width);
785 
786 			/* PPS SDP is set elsewhere because it has to be done after DIG FE is connected to DIG BE */
787 		}
788 
789 		/* Enable DSC in OPTC */
790 		DC_LOG_DSC("Setting optc DSC config for tg instance %d:", pipe_ctx->stream_res.tg->inst);
791 		dsc_optc_config_log(dsc, &dsc_optc_cfg);
792 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(pipe_ctx->stream_res.tg,
793 							optc_dsc_mode,
794 							dsc_optc_cfg.bytes_per_pixel,
795 							dsc_optc_cfg.slice_width);
796 	} else {
797 		/* disable DSC in OPTC */
798 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(
799 				pipe_ctx->stream_res.tg,
800 				OPTC_DSC_DISABLED, 0, 0);
801 
802 		/* disable DSC in stream encoder */
803 		if (dc_is_dp_signal(stream->signal)) {
804 			if (dp_is_128b_132b_signal(pipe_ctx))
805 				pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_set_dsc_pps_info_packet(
806 										pipe_ctx->stream_res.hpo_dp_stream_enc,
807 										false,
808 										NULL,
809 										true);
810 			else {
811 				if (pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_config)
812 					pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_config(
813 							pipe_ctx->stream_res.stream_enc,
814 							OPTC_DSC_DISABLED, 0, 0);
815 				pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_pps_info_packet(
816 							pipe_ctx->stream_res.stream_enc, false, NULL, true);
817 			}
818 		}
819 
820 		/* disable DSC block */
821 		for (odm_pipe = pipe_ctx; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
822 			odm_pipe->stream_res.dsc->funcs->dsc_disconnect(odm_pipe->stream_res.dsc);
823 			/*
824 			 * TODO - dsc_disconnect is a double buffered register.
825 			 * by the time we call dsc_disable, dsc may still remain
826 			 * connected to OPP. In this case OPTC will no longer
827 			 * get correct pixel data because DSCC is off. However
828 			 * we also can't wait for the  disconnect pending
829 			 * complete, because this function can be called
830 			 * with/without OTG master lock acquired. When the lock
831 			 * is acquired we will never get pending complete until
832 			 * we release the lock later. So there is no easy way to
833 			 * solve this problem especially when the lock is
834 			 * acquired. DSC is a front end hw block it should be
835 			 * programmed as part of front end sequence, where the
836 			 * commit sequence without lock and update sequence
837 			 * with lock are completely separated. However because
838 			 * we are programming dsc as part of back end link
839 			 * programming sequence, we don't know if front end OPTC
840 			 * master lock is acquired. The back end should be
841 			 * agnostic to front end lock. DSC programming shouldn't
842 			 * belong to this sequence.
843 			 */
844 			odm_pipe->stream_res.dsc->funcs->dsc_disable(odm_pipe->stream_res.dsc);
845 			if (dccg->funcs->set_ref_dscclk)
846 				dccg->funcs->set_ref_dscclk(dccg, odm_pipe->stream_res.dsc->inst);
847 		}
848 	}
849 }
850 
851 /*
852  * For dynamic bpp change case, dsc is programmed with MASTER_UPDATE_LOCK enabled;
853  * hence PPS info packet update need to use frame update instead of immediate update.
854  * Added parameter immediate_update for this purpose.
855  * The decision to use frame update is hard-coded in function dp_update_dsc_config(),
856  * which is the only place where a "false" would be passed in for param immediate_update.
857  *
858  * immediate_update is only applicable when DSC is enabled.
859  */
860 bool link_set_dsc_pps_packet(struct pipe_ctx *pipe_ctx, bool enable, bool immediate_update)
861 {
862 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
863 	struct dc_stream_state *stream = pipe_ctx->stream;
864 
865 	if (!pipe_ctx->stream->timing.flags.DSC)
866 		return false;
867 
868 	if (!dsc)
869 		return false;
870 
871 	DC_LOGGER_INIT(dsc->ctx->logger);
872 
873 	if (enable) {
874 		struct dsc_config dsc_cfg;
875 		uint8_t dsc_packed_pps[128];
876 
877 		memset(&dsc_cfg, 0, sizeof(dsc_cfg));
878 		memset(dsc_packed_pps, 0, 128);
879 
880 		/* Enable DSC hw block */
881 		dsc_cfg.pic_width = stream->timing.h_addressable + stream->timing.h_border_left + stream->timing.h_border_right;
882 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top + stream->timing.v_border_bottom;
883 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
884 		dsc_cfg.color_depth = stream->timing.display_color_depth;
885 		dsc_cfg.is_odm = pipe_ctx->next_odm_pipe ? true : false;
886 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
887 		dsc_cfg.dsc_padding = pipe_ctx->dsc_padding_params.dsc_hactive_padding;
888 
889 		dsc->funcs->dsc_get_packed_pps(dsc, &dsc_cfg, &dsc_packed_pps[0]);
890 		memcpy(&stream->dsc_packed_pps[0], &dsc_packed_pps[0], sizeof(stream->dsc_packed_pps));
891 		if (dc_is_dp_signal(stream->signal)) {
892 			DC_LOG_DSC("Setting stream encoder DSC PPS SDP for engine %d\n", (int)pipe_ctx->stream_res.stream_enc->id);
893 			if (dp_is_128b_132b_signal(pipe_ctx))
894 				pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_set_dsc_pps_info_packet(
895 										pipe_ctx->stream_res.hpo_dp_stream_enc,
896 										true,
897 										&dsc_packed_pps[0],
898 										immediate_update);
899 			else
900 				pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_pps_info_packet(
901 						pipe_ctx->stream_res.stream_enc,
902 						true,
903 						&dsc_packed_pps[0],
904 						immediate_update);
905 		}
906 	} else {
907 		/* disable DSC PPS in stream encoder */
908 		memset(&stream->dsc_packed_pps[0], 0, sizeof(stream->dsc_packed_pps));
909 		if (dc_is_dp_signal(stream->signal)) {
910 			if (dp_is_128b_132b_signal(pipe_ctx))
911 				pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_set_dsc_pps_info_packet(
912 										pipe_ctx->stream_res.hpo_dp_stream_enc,
913 										false,
914 										NULL,
915 										true);
916 			else
917 				pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_pps_info_packet(
918 						pipe_ctx->stream_res.stream_enc, false, NULL, true);
919 		}
920 	}
921 
922 	return true;
923 }
924 
925 bool link_set_dsc_enable(struct pipe_ctx *pipe_ctx, bool enable)
926 {
927 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
928 	bool result = false;
929 
930 	if (!pipe_ctx->stream->timing.flags.DSC)
931 		goto out;
932 	if (!dsc)
933 		goto out;
934 
935 	if (enable) {
936 		{
937 			link_set_dsc_on_stream(pipe_ctx, true);
938 			result = true;
939 		}
940 	} else {
941 		dp_set_dsc_on_rx(pipe_ctx, false);
942 		link_set_dsc_on_stream(pipe_ctx, false);
943 		result = true;
944 	}
945 out:
946 	return result;
947 }
948 
949 bool link_update_dsc_config(struct pipe_ctx *pipe_ctx)
950 {
951 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
952 
953 	if (!pipe_ctx->stream->timing.flags.DSC)
954 		return false;
955 	if (!dsc)
956 		return false;
957 
958 	link_set_dsc_on_stream(pipe_ctx, true);
959 	link_set_dsc_pps_packet(pipe_ctx, true, false);
960 	return true;
961 }
962 
963 static void enable_stream_features(struct pipe_ctx *pipe_ctx)
964 {
965 	struct dc_stream_state *stream = pipe_ctx->stream;
966 
967 	if (pipe_ctx->stream->signal != SIGNAL_TYPE_DISPLAY_PORT_MST) {
968 		struct dc_link *link = stream->link;
969 		union down_spread_ctrl old_downspread;
970 		union down_spread_ctrl new_downspread;
971 
972 		memset(&old_downspread, 0, sizeof(old_downspread));
973 
974 		core_link_read_dpcd(link, DP_DOWNSPREAD_CTRL,
975 				&old_downspread.raw, sizeof(old_downspread));
976 
977 		new_downspread.raw = old_downspread.raw;
978 
979 		new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
980 				(stream->ignore_msa_timing_param) ? 1 : 0;
981 
982 		if (new_downspread.raw != old_downspread.raw) {
983 			core_link_write_dpcd(link, DP_DOWNSPREAD_CTRL,
984 				&new_downspread.raw, sizeof(new_downspread));
985 		}
986 
987 	} else {
988 		dm_helpers_mst_enable_stream_features(stream);
989 	}
990 }
991 
992 static void log_vcp_x_y(const struct dc_link *link, struct fixed31_32 avg_time_slots_per_mtp)
993 {
994 	const uint32_t VCP_Y_PRECISION = 1000;
995 	uint64_t vcp_x, vcp_y;
996 	DC_LOGGER_INIT(link->ctx->logger);
997 
998 	// Add 0.5*(1/VCP_Y_PRECISION) to round up to decimal precision
999 	avg_time_slots_per_mtp = dc_fixpt_add(
1000 			avg_time_slots_per_mtp,
1001 			dc_fixpt_from_fraction(
1002 				1,
1003 				2*VCP_Y_PRECISION));
1004 
1005 	vcp_x = dc_fixpt_floor(
1006 			avg_time_slots_per_mtp);
1007 	vcp_y = dc_fixpt_floor(
1008 			dc_fixpt_mul_int(
1009 				dc_fixpt_sub_int(
1010 					avg_time_slots_per_mtp,
1011 					dc_fixpt_floor(
1012 							avg_time_slots_per_mtp)),
1013 				VCP_Y_PRECISION));
1014 
1015 
1016 	if (link->type == dc_connection_mst_branch)
1017 		DC_LOG_DP2("MST Update Payload: set_throttled_vcp_size slot X.Y for MST stream "
1018 				"X: %llu "
1019 				"Y: %llu/%d",
1020 				vcp_x,
1021 				vcp_y,
1022 				VCP_Y_PRECISION);
1023 	else
1024 		DC_LOG_DP2("SST Update Payload: set_throttled_vcp_size slot X.Y for SST stream "
1025 				"X: %llu "
1026 				"Y: %llu/%d",
1027 				vcp_x,
1028 				vcp_y,
1029 				VCP_Y_PRECISION);
1030 }
1031 
1032 static struct fixed31_32 get_pbn_per_slot(struct dc_stream_state *stream)
1033 {
1034 	struct fixed31_32 mbytes_per_sec;
1035 	uint32_t link_rate_in_mbytes_per_sec = dp_link_bandwidth_kbps(stream->link,
1036 			&stream->link->cur_link_settings);
1037 	link_rate_in_mbytes_per_sec /= 8000; /* Kbits to MBytes */
1038 
1039 	mbytes_per_sec = dc_fixpt_from_int(link_rate_in_mbytes_per_sec);
1040 
1041 	return dc_fixpt_div_int(mbytes_per_sec, 54);
1042 }
1043 
1044 static struct fixed31_32 get_pbn_from_bw_in_kbps(uint64_t kbps)
1045 {
1046 	struct fixed31_32 peak_kbps;
1047 	uint32_t numerator = 0;
1048 	uint32_t denominator = 1;
1049 
1050 	/*
1051 	 * The 1.006 factor (margin 5300ppm + 300ppm ~ 0.6% as per spec) is not
1052 	 * required when determining PBN/time slot utilization on the link between
1053 	 * us and the branch, since that overhead is already accounted for in
1054 	 * the get_pbn_per_slot function.
1055 	 *
1056 	 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
1057 	 * common multiplier to render an integer PBN for all link rate/lane
1058 	 * counts combinations
1059 	 * calculate
1060 	 * peak_kbps *= (64/54)
1061 	 * peak_kbps /= (8 * 1000) convert to bytes
1062 	 */
1063 
1064 	numerator = 64;
1065 	denominator = 54 * 8 * 1000;
1066 	kbps *= numerator;
1067 	peak_kbps = dc_fixpt_from_fraction(kbps, denominator);
1068 
1069 	return peak_kbps;
1070 }
1071 
1072 static struct fixed31_32 get_pbn_from_timing(struct pipe_ctx *pipe_ctx)
1073 {
1074 	uint64_t kbps;
1075 	enum dc_link_encoding_format link_encoding;
1076 
1077 	if (dp_is_128b_132b_signal(pipe_ctx))
1078 		link_encoding = DC_LINK_ENCODING_DP_128b_132b;
1079 	else
1080 		link_encoding = DC_LINK_ENCODING_DP_8b_10b;
1081 
1082 	kbps = dc_bandwidth_in_kbps_from_timing(&pipe_ctx->stream->timing, link_encoding);
1083 	return get_pbn_from_bw_in_kbps(kbps);
1084 }
1085 
1086 
1087 // TODO - DP2.0 Link: Fix get_lane_status to handle LTTPR offset (SST and MST)
1088 static void get_lane_status(
1089 	struct dc_link *link,
1090 	uint32_t lane_count,
1091 	union lane_status *status,
1092 	union lane_align_status_updated *status_updated)
1093 {
1094 	unsigned int lane;
1095 	uint8_t dpcd_buf[3] = {0};
1096 
1097 	if (status == NULL || status_updated == NULL) {
1098 		return;
1099 	}
1100 
1101 	core_link_read_dpcd(
1102 			link,
1103 			DP_LANE0_1_STATUS,
1104 			dpcd_buf,
1105 			sizeof(dpcd_buf));
1106 
1107 	for (lane = 0; lane < lane_count; lane++) {
1108 		status[lane].raw = dp_get_nibble_at_index(&dpcd_buf[0], lane);
1109 	}
1110 
1111 	status_updated->raw = dpcd_buf[2];
1112 }
1113 
1114 static bool poll_for_allocation_change_trigger(struct dc_link *link)
1115 {
1116 	/*
1117 	 * wait for ACT handled
1118 	 */
1119 	int i;
1120 	const int act_retries = 30;
1121 	enum act_return_status result = ACT_FAILED;
1122 	enum dc_connection_type display_connected = (link->type != dc_connection_none);
1123 	union payload_table_update_status update_status = {0};
1124 	union lane_status dpcd_lane_status[LANE_COUNT_DP_MAX];
1125 	union lane_align_status_updated lane_status_updated;
1126 	DC_LOGGER_INIT(link->ctx->logger);
1127 
1128 	if (!display_connected || link->aux_access_disabled)
1129 		return true;
1130 	for (i = 0; i < act_retries; i++) {
1131 		get_lane_status(link, link->cur_link_settings.lane_count, dpcd_lane_status, &lane_status_updated);
1132 
1133 		if (!dp_is_cr_done(link->cur_link_settings.lane_count, dpcd_lane_status) ||
1134 				!dp_is_ch_eq_done(link->cur_link_settings.lane_count, dpcd_lane_status) ||
1135 				!dp_is_symbol_locked(link->cur_link_settings.lane_count, dpcd_lane_status) ||
1136 				!dp_is_interlane_aligned(lane_status_updated)) {
1137 			DC_LOG_ERROR("SST Update Payload: Link loss occurred while "
1138 					"polling for ACT handled.");
1139 			result = ACT_LINK_LOST;
1140 			break;
1141 		}
1142 		core_link_read_dpcd(
1143 				link,
1144 				DP_PAYLOAD_TABLE_UPDATE_STATUS,
1145 				&update_status.raw,
1146 				1);
1147 
1148 		if (update_status.bits.ACT_HANDLED == 1) {
1149 			DC_LOG_DP2("SST Update Payload: ACT handled by downstream.");
1150 			result = ACT_SUCCESS;
1151 			break;
1152 		}
1153 
1154 		fsleep(5000);
1155 	}
1156 
1157 	if (result == ACT_FAILED) {
1158 		DC_LOG_ERROR("SST Update Payload: ACT still not handled after retries, "
1159 				"continue on. Something is wrong with the branch.");
1160 	}
1161 
1162 	return (result == ACT_SUCCESS);
1163 }
1164 
1165 static void update_mst_stream_alloc_table(
1166 	struct dc_link *link,
1167 	struct stream_encoder *stream_enc,
1168 	struct hpo_dp_stream_encoder *hpo_dp_stream_enc, // TODO: Rename stream_enc to dio_stream_enc?
1169 	const struct dc_dp_mst_stream_allocation_table *proposed_table)
1170 {
1171 	struct link_mst_stream_allocation work_table[MAX_CONTROLLER_NUM] = { 0 };
1172 	struct link_mst_stream_allocation *dc_alloc;
1173 
1174 	int i;
1175 	int j;
1176 
1177 	/* if DRM proposed_table has more than one new payload */
1178 	ASSERT(proposed_table->stream_count -
1179 			link->mst_stream_alloc_table.stream_count < 2);
1180 
1181 	/* copy proposed_table to link, add stream encoder */
1182 	for (i = 0; i < proposed_table->stream_count; i++) {
1183 
1184 		for (j = 0; j < link->mst_stream_alloc_table.stream_count; j++) {
1185 			dc_alloc =
1186 			&link->mst_stream_alloc_table.stream_allocations[j];
1187 
1188 			if (dc_alloc->vcp_id ==
1189 				proposed_table->stream_allocations[i].vcp_id) {
1190 
1191 				work_table[i] = *dc_alloc;
1192 				work_table[i].slot_count = proposed_table->stream_allocations[i].slot_count;
1193 				break; /* exit j loop */
1194 			}
1195 		}
1196 
1197 		/* new vcp_id */
1198 		if (j == link->mst_stream_alloc_table.stream_count) {
1199 			work_table[i].vcp_id =
1200 				proposed_table->stream_allocations[i].vcp_id;
1201 			work_table[i].slot_count =
1202 				proposed_table->stream_allocations[i].slot_count;
1203 			work_table[i].stream_enc = stream_enc;
1204 			work_table[i].hpo_dp_stream_enc = hpo_dp_stream_enc;
1205 		}
1206 	}
1207 
1208 	/* update link->mst_stream_alloc_table with work_table */
1209 	link->mst_stream_alloc_table.stream_count =
1210 			proposed_table->stream_count;
1211 	for (i = 0; i < MAX_CONTROLLER_NUM; i++)
1212 		link->mst_stream_alloc_table.stream_allocations[i] =
1213 				work_table[i];
1214 }
1215 
1216 static void remove_stream_from_alloc_table(
1217 		struct dc_link *link,
1218 		struct stream_encoder *dio_stream_enc,
1219 		struct hpo_dp_stream_encoder *hpo_dp_stream_enc)
1220 {
1221 	int i = 0;
1222 	struct link_mst_stream_allocation_table *table =
1223 			&link->mst_stream_alloc_table;
1224 
1225 	if (hpo_dp_stream_enc) {
1226 		for (; i < table->stream_count; i++)
1227 			if (hpo_dp_stream_enc == table->stream_allocations[i].hpo_dp_stream_enc)
1228 				break;
1229 	} else {
1230 		for (; i < table->stream_count; i++)
1231 			if (dio_stream_enc == table->stream_allocations[i].stream_enc)
1232 				break;
1233 	}
1234 
1235 	if (i < table->stream_count) {
1236 		i++;
1237 		for (; i < table->stream_count; i++)
1238 			table->stream_allocations[i-1] = table->stream_allocations[i];
1239 		memset(&table->stream_allocations[table->stream_count-1], 0,
1240 				sizeof(struct link_mst_stream_allocation));
1241 		table->stream_count--;
1242 	}
1243 }
1244 
1245 static void print_mst_streams(struct dc_link *link)
1246 {
1247 	int i;
1248 
1249 	DC_LOGGER_INIT(link->ctx->logger);
1250 
1251 	DC_LOG_MST("%s stream_count: %d:\n",
1252 		   __func__,
1253 		   link->mst_stream_alloc_table.stream_count);
1254 
1255 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
1256 		DC_LOG_MST("stream_enc[%d]: %p\n", i,
1257 			   (void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc);
1258 		DC_LOG_MST("stream[%d].hpo_dp_stream_enc: %p\n", i,
1259 			   (void *) link->mst_stream_alloc_table.stream_allocations[i].hpo_dp_stream_enc);
1260 		DC_LOG_MST("stream[%d].vcp_id: %d\n", i,
1261 			   link->mst_stream_alloc_table.stream_allocations[i].vcp_id);
1262 		DC_LOG_MST("stream[%d].slot_count: %d\n", i,
1263 			   link->mst_stream_alloc_table.stream_allocations[i].slot_count);
1264 	}
1265 }
1266 
1267 static enum dc_status deallocate_mst_payload(struct pipe_ctx *pipe_ctx)
1268 {
1269 	struct dc_stream_state *stream = pipe_ctx->stream;
1270 	struct dc_link *link = stream->link;
1271 	struct dc_dp_mst_stream_allocation_table proposed_table = {0};
1272 	struct fixed31_32 avg_time_slots_per_mtp = dc_fixpt_from_int(0);
1273 	bool mst_mode = (link->type == dc_connection_mst_branch);
1274 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1275 	const struct dc_link_settings empty_link_settings = {0};
1276 	DC_LOGGER_INIT(link->ctx->logger);
1277 
1278 	/* deallocate_mst_payload is called before disable link. When mode or
1279 	 * disable/enable monitor, new stream is created which is not in link
1280 	 * stream[] yet. For this, payload is not allocated yet, so de-alloc
1281 	 * should not done. For new mode set, map_resources will get engine
1282 	 * for new stream, so stream_enc->id should be validated until here.
1283 	 */
1284 
1285 	/* slot X.Y */
1286 	if (link_hwss->ext.set_throttled_vcp_size)
1287 		link_hwss->ext.set_throttled_vcp_size(pipe_ctx, avg_time_slots_per_mtp);
1288 	if (link_hwss->ext.set_hblank_min_symbol_width)
1289 		link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1290 				&empty_link_settings,
1291 				avg_time_slots_per_mtp);
1292 
1293 	if (mst_mode) {
1294 		/* when link is in mst mode, reply on mst manager to remove
1295 		 * payload
1296 		 */
1297 		if (dm_helpers_dp_mst_write_payload_allocation_table(
1298 				stream->ctx,
1299 				stream,
1300 				&proposed_table,
1301 				false))
1302 			update_mst_stream_alloc_table(
1303 					link,
1304 					pipe_ctx->stream_res.stream_enc,
1305 					pipe_ctx->stream_res.hpo_dp_stream_enc,
1306 					&proposed_table);
1307 		else
1308 			DC_LOG_WARNING("Failed to update MST allocation table for idx %d\n",
1309 					pipe_ctx->pipe_idx);
1310 	} else {
1311 		/* when link is no longer in mst mode (mst hub unplugged),
1312 		 * remove payload with default dc logic
1313 		 */
1314 		remove_stream_from_alloc_table(link, pipe_ctx->stream_res.stream_enc,
1315 				pipe_ctx->stream_res.hpo_dp_stream_enc);
1316 	}
1317 
1318 	print_mst_streams(link);
1319 
1320 	/* update mst stream allocation table hardware state */
1321 	if (link_hwss->ext.update_stream_allocation_table == NULL ||
1322 			link_dp_get_encoding_format(&link->cur_link_settings) == DP_UNKNOWN_ENCODING) {
1323 		DC_LOG_DEBUG("Unknown encoding format\n");
1324 		return DC_ERROR_UNEXPECTED;
1325 	}
1326 
1327 	link_hwss->ext.update_stream_allocation_table(link, &pipe_ctx->link_res,
1328 			&link->mst_stream_alloc_table);
1329 
1330 	if (mst_mode)
1331 		dm_helpers_dp_mst_poll_for_allocation_change_trigger(
1332 			stream->ctx,
1333 			stream);
1334 
1335 	dm_helpers_dp_mst_update_mst_mgr_for_deallocation(
1336 			stream->ctx,
1337 			stream);
1338 
1339 	return DC_OK;
1340 }
1341 
1342 /* convert link_mst_stream_alloc_table to dm dp_mst_stream_alloc_table
1343  * because stream_encoder is not exposed to dm
1344  */
1345 static enum dc_status allocate_mst_payload(struct pipe_ctx *pipe_ctx)
1346 {
1347 	struct dc_stream_state *stream = pipe_ctx->stream;
1348 	struct dc_link *link = stream->link;
1349 	struct dc_dp_mst_stream_allocation_table proposed_table = {0};
1350 	struct fixed31_32 avg_time_slots_per_mtp;
1351 	struct fixed31_32 pbn;
1352 	struct fixed31_32 pbn_per_slot;
1353 	enum act_return_status ret;
1354 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1355 	DC_LOGGER_INIT(link->ctx->logger);
1356 
1357 	/* enable_link_dp_mst already check link->enabled_stream_count
1358 	 * and stream is in link->stream[]. This is called during set mode,
1359 	 * stream_enc is available.
1360 	 */
1361 
1362 	/* get calculate VC payload for stream: stream_alloc */
1363 	if (dm_helpers_dp_mst_write_payload_allocation_table(
1364 		stream->ctx,
1365 		stream,
1366 		&proposed_table,
1367 		true))
1368 		update_mst_stream_alloc_table(
1369 					link,
1370 					pipe_ctx->stream_res.stream_enc,
1371 					pipe_ctx->stream_res.hpo_dp_stream_enc,
1372 					&proposed_table);
1373 	else
1374 		DC_LOG_WARNING("Failed to update MST allocation table for idx %d\n",
1375 				pipe_ctx->pipe_idx);
1376 
1377 	print_mst_streams(link);
1378 
1379 	ASSERT(proposed_table.stream_count > 0);
1380 
1381 	/* program DP source TX for payload */
1382 	if (link_hwss->ext.update_stream_allocation_table == NULL ||
1383 			link_dp_get_encoding_format(&link->cur_link_settings) == DP_UNKNOWN_ENCODING) {
1384 		DC_LOG_ERROR("Failure: unknown encoding format\n");
1385 		return DC_ERROR_UNEXPECTED;
1386 	}
1387 
1388 	link_hwss->ext.update_stream_allocation_table(link,
1389 			&pipe_ctx->link_res,
1390 			&link->mst_stream_alloc_table);
1391 
1392 	/* send down message */
1393 	ret = dm_helpers_dp_mst_poll_for_allocation_change_trigger(
1394 			stream->ctx,
1395 			stream);
1396 
1397 	if (ret != ACT_LINK_LOST)
1398 		dm_helpers_dp_mst_send_payload_allocation(
1399 				stream->ctx,
1400 				stream);
1401 
1402 	/* slot X.Y for only current stream */
1403 	pbn_per_slot = get_pbn_per_slot(stream);
1404 	if (pbn_per_slot.value == 0) {
1405 		DC_LOG_ERROR("Failure: pbn_per_slot==0 not allowed. Cannot continue, returning DC_UNSUPPORTED_VALUE.\n");
1406 		return DC_UNSUPPORTED_VALUE;
1407 	}
1408 	pbn = get_pbn_from_timing(pipe_ctx);
1409 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
1410 
1411 	log_vcp_x_y(link, avg_time_slots_per_mtp);
1412 
1413 	if (link_hwss->ext.set_throttled_vcp_size)
1414 		link_hwss->ext.set_throttled_vcp_size(pipe_ctx, avg_time_slots_per_mtp);
1415 	if (link_hwss->ext.set_hblank_min_symbol_width)
1416 		link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1417 				&link->cur_link_settings,
1418 				avg_time_slots_per_mtp);
1419 
1420 	return DC_OK;
1421 }
1422 
1423 struct fixed31_32 link_calculate_sst_avg_time_slots_per_mtp(
1424 		const struct dc_stream_state *stream,
1425 		const struct dc_link *link)
1426 {
1427 	struct fixed31_32 link_bw_effective =
1428 			dc_fixpt_from_int(
1429 					dp_link_bandwidth_kbps(link, &link->cur_link_settings));
1430 	struct fixed31_32 timeslot_bw_effective =
1431 			dc_fixpt_div_int(link_bw_effective, MAX_MTP_SLOT_COUNT);
1432 	struct fixed31_32 timing_bw =
1433 			dc_fixpt_from_int(
1434 					dc_bandwidth_in_kbps_from_timing(&stream->timing,
1435 							dc_link_get_highest_encoding_format(link)));
1436 	struct fixed31_32 avg_time_slots_per_mtp =
1437 			dc_fixpt_div(timing_bw, timeslot_bw_effective);
1438 
1439 	return avg_time_slots_per_mtp;
1440 }
1441 
1442 
1443 static bool write_128b_132b_sst_payload_allocation_table(
1444 		const struct dc_stream_state *stream,
1445 		struct dc_link *link,
1446 		struct link_mst_stream_allocation_table *proposed_table,
1447 		bool allocate)
1448 {
1449 	const uint8_t vc_id = 1; /// VC ID always 1 for SST
1450 	const uint8_t start_time_slot = 0; /// Always start at time slot 0 for SST
1451 	bool result = false;
1452 	uint8_t req_slot_count = 0;
1453 	struct fixed31_32 avg_time_slots_per_mtp = { 0 };
1454 	union payload_table_update_status update_status = { 0 };
1455 	const uint32_t max_retries = 30;
1456 	uint32_t retries = 0;
1457 	enum dc_connection_type display_connected = (link->type != dc_connection_none);
1458 	DC_LOGGER_INIT(link->ctx->logger);
1459 
1460 	if (allocate)	{
1461 		avg_time_slots_per_mtp = link_calculate_sst_avg_time_slots_per_mtp(stream, link);
1462 		req_slot_count = (uint8_t)dc_fixpt_ceil(avg_time_slots_per_mtp);
1463 		/// Validation should filter out modes that exceed link BW
1464 		ASSERT(req_slot_count <= MAX_MTP_SLOT_COUNT);
1465 		if (req_slot_count > MAX_MTP_SLOT_COUNT)
1466 			return false;
1467 	} else {
1468 		/// Leave req_slot_count = 0 if allocate is false.
1469 	}
1470 
1471 	proposed_table->stream_count = 1; /// Always 1 stream for SST
1472 	proposed_table->stream_allocations[0].slot_count = req_slot_count;
1473 	proposed_table->stream_allocations[0].vcp_id = vc_id;
1474 
1475 	if (!display_connected || link->aux_access_disabled)
1476 		return true;
1477 
1478 	/// Write DPCD 2C0 = 1 to start updating
1479 	update_status.bits.VC_PAYLOAD_TABLE_UPDATED = 1;
1480 	core_link_write_dpcd(
1481 			link,
1482 			DP_PAYLOAD_TABLE_UPDATE_STATUS,
1483 			&update_status.raw,
1484 			1);
1485 
1486 	/// Program the changes in DPCD 1C0 - 1C2
1487 	ASSERT(vc_id == 1);
1488 	core_link_write_dpcd(
1489 			link,
1490 			DP_PAYLOAD_ALLOCATE_SET,
1491 			&vc_id,
1492 			1);
1493 
1494 	ASSERT(start_time_slot == 0);
1495 	core_link_write_dpcd(
1496 			link,
1497 			DP_PAYLOAD_ALLOCATE_START_TIME_SLOT,
1498 			&start_time_slot,
1499 			1);
1500 
1501 	core_link_write_dpcd(
1502 			link,
1503 			DP_PAYLOAD_ALLOCATE_TIME_SLOT_COUNT,
1504 			&req_slot_count,
1505 			1);
1506 
1507 	/// Poll till DPCD 2C0 read 1
1508 	/// Try for at least 150ms (30 retries, with 5ms delay after each attempt)
1509 
1510 	while (retries < max_retries) {
1511 		if (core_link_read_dpcd(
1512 				link,
1513 				DP_PAYLOAD_TABLE_UPDATE_STATUS,
1514 				&update_status.raw,
1515 				1) == DC_OK) {
1516 			if (update_status.bits.VC_PAYLOAD_TABLE_UPDATED == 1) {
1517 				DC_LOG_DP2("SST Update Payload: downstream payload table updated.");
1518 				result = true;
1519 				break;
1520 			}
1521 		} else {
1522 			union dpcd_rev dpcdRev = {0};
1523 
1524 			if (core_link_read_dpcd(
1525 					link,
1526 					DP_DPCD_REV,
1527 					&dpcdRev.raw,
1528 					1) != DC_OK) {
1529 				DC_LOG_ERROR("SST Update Payload: Unable to read DPCD revision "
1530 						"of sink while polling payload table "
1531 						"updated status bit.");
1532 				break;
1533 			}
1534 		}
1535 		retries++;
1536 		fsleep(5000);
1537 	}
1538 
1539 	if (!result && retries == max_retries) {
1540 		DC_LOG_ERROR("SST Update Payload: Payload table not updated after retries, "
1541 				"continue on. Something is wrong with the branch.");
1542 		// TODO - DP2.0 Payload: Read and log the payload table from downstream branch
1543 	}
1544 
1545 	return result;
1546 }
1547 
1548 /*
1549  * Payload allocation/deallocation for SST introduced in DP2.0
1550  */
1551 static enum dc_status update_sst_payload(struct pipe_ctx *pipe_ctx,
1552 						 bool allocate)
1553 {
1554 	struct dc_stream_state *stream = pipe_ctx->stream;
1555 	struct dc_link *link = stream->link;
1556 	struct link_mst_stream_allocation_table proposed_table = {0};
1557 	struct fixed31_32 avg_time_slots_per_mtp;
1558 	const struct dc_link_settings empty_link_settings = {0};
1559 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1560 	DC_LOGGER_INIT(link->ctx->logger);
1561 
1562 	/* slot X.Y for SST payload deallocate */
1563 	if (!allocate) {
1564 		avg_time_slots_per_mtp = dc_fixpt_from_int(0);
1565 
1566 		log_vcp_x_y(link, avg_time_slots_per_mtp);
1567 
1568 		if (link_hwss->ext.set_throttled_vcp_size)
1569 			link_hwss->ext.set_throttled_vcp_size(pipe_ctx,
1570 					avg_time_slots_per_mtp);
1571 		if (link_hwss->ext.set_hblank_min_symbol_width)
1572 			link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1573 					&empty_link_settings,
1574 					avg_time_slots_per_mtp);
1575 	}
1576 
1577 	/* calculate VC payload and update branch with new payload allocation table*/
1578 	if (!write_128b_132b_sst_payload_allocation_table(
1579 			stream,
1580 			link,
1581 			&proposed_table,
1582 			allocate)) {
1583 		DC_LOG_ERROR("SST Update Payload: Failed to update "
1584 						"allocation table for "
1585 						"pipe idx: %d\n",
1586 						pipe_ctx->pipe_idx);
1587 		return DC_FAIL_DP_PAYLOAD_ALLOCATION;
1588 	}
1589 
1590 	proposed_table.stream_allocations[0].hpo_dp_stream_enc = pipe_ctx->stream_res.hpo_dp_stream_enc;
1591 
1592 	ASSERT(proposed_table.stream_count == 1);
1593 
1594 	//TODO - DP2.0 Logging: Instead of hpo_dp_stream_enc pointer, log instance id
1595 	DC_LOG_DP2("SST Update Payload: hpo_dp_stream_enc: %p      "
1596 		"vcp_id: %d      "
1597 		"slot_count: %d\n",
1598 		(void *) proposed_table.stream_allocations[0].hpo_dp_stream_enc,
1599 		proposed_table.stream_allocations[0].vcp_id,
1600 		proposed_table.stream_allocations[0].slot_count);
1601 
1602 	/* program DP source TX for payload */
1603 	link_hwss->ext.update_stream_allocation_table(link, &pipe_ctx->link_res,
1604 			&proposed_table);
1605 
1606 	/* poll for ACT handled */
1607 	if (!poll_for_allocation_change_trigger(link)) {
1608 		// Failures will result in blackscreen and errors logged
1609 		BREAK_TO_DEBUGGER();
1610 	}
1611 
1612 	/* slot X.Y for SST payload allocate */
1613 	if (allocate && link_dp_get_encoding_format(&link->cur_link_settings) ==
1614 			DP_128b_132b_ENCODING) {
1615 		avg_time_slots_per_mtp = link_calculate_sst_avg_time_slots_per_mtp(stream, link);
1616 
1617 		log_vcp_x_y(link, avg_time_slots_per_mtp);
1618 
1619 		if (link_hwss->ext.set_throttled_vcp_size)
1620 			link_hwss->ext.set_throttled_vcp_size(pipe_ctx,
1621 					avg_time_slots_per_mtp);
1622 		if (link_hwss->ext.set_hblank_min_symbol_width)
1623 			link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1624 					&link->cur_link_settings,
1625 					avg_time_slots_per_mtp);
1626 	}
1627 
1628 	/* Always return DC_OK.
1629 	 * If part of sequence fails, log failure(s) and show blackscreen
1630 	 */
1631 	return DC_OK;
1632 }
1633 
1634 enum dc_status link_reduce_mst_payload(struct pipe_ctx *pipe_ctx, uint32_t bw_in_kbps)
1635 {
1636 	struct dc_stream_state *stream = pipe_ctx->stream;
1637 	struct dc_link *link = stream->link;
1638 	struct fixed31_32 avg_time_slots_per_mtp;
1639 	struct fixed31_32 pbn;
1640 	struct fixed31_32 pbn_per_slot;
1641 	struct dc_dp_mst_stream_allocation_table proposed_table = {0};
1642 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1643 	DC_LOGGER_INIT(link->ctx->logger);
1644 
1645 	/* decrease throttled vcp size */
1646 	pbn_per_slot = get_pbn_per_slot(stream);
1647 	pbn = get_pbn_from_bw_in_kbps(bw_in_kbps);
1648 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
1649 
1650 	if (link_hwss->ext.set_throttled_vcp_size)
1651 		link_hwss->ext.set_throttled_vcp_size(pipe_ctx, avg_time_slots_per_mtp);
1652 	if (link_hwss->ext.set_hblank_min_symbol_width)
1653 		link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1654 				&link->cur_link_settings,
1655 				avg_time_slots_per_mtp);
1656 
1657 	/* send ALLOCATE_PAYLOAD sideband message with updated pbn */
1658 	dm_helpers_dp_mst_send_payload_allocation(
1659 			stream->ctx,
1660 			stream);
1661 
1662 	/* notify immediate branch device table update */
1663 	if (dm_helpers_dp_mst_write_payload_allocation_table(
1664 			stream->ctx,
1665 			stream,
1666 			&proposed_table,
1667 			true)) {
1668 		/* update mst stream allocation table software state */
1669 		update_mst_stream_alloc_table(
1670 				link,
1671 				pipe_ctx->stream_res.stream_enc,
1672 				pipe_ctx->stream_res.hpo_dp_stream_enc,
1673 				&proposed_table);
1674 	} else {
1675 		DC_LOG_WARNING("Failed to update MST allocation table for idx %d\n",
1676 				pipe_ctx->pipe_idx);
1677 	}
1678 
1679 	print_mst_streams(link);
1680 
1681 	ASSERT(proposed_table.stream_count > 0);
1682 
1683 	/* update mst stream allocation table hardware state */
1684 	if (link_hwss->ext.update_stream_allocation_table == NULL ||
1685 			link_dp_get_encoding_format(&link->cur_link_settings) == DP_UNKNOWN_ENCODING) {
1686 		DC_LOG_ERROR("Failure: unknown encoding format\n");
1687 		return DC_ERROR_UNEXPECTED;
1688 	}
1689 
1690 	link_hwss->ext.update_stream_allocation_table(link, &pipe_ctx->link_res,
1691 			&link->mst_stream_alloc_table);
1692 
1693 	/* poll for immediate branch device ACT handled */
1694 	dm_helpers_dp_mst_poll_for_allocation_change_trigger(
1695 			stream->ctx,
1696 			stream);
1697 
1698 	return DC_OK;
1699 }
1700 
1701 enum dc_status link_increase_mst_payload(struct pipe_ctx *pipe_ctx, uint32_t bw_in_kbps)
1702 {
1703 	struct dc_stream_state *stream = pipe_ctx->stream;
1704 	struct dc_link *link = stream->link;
1705 	struct fixed31_32 avg_time_slots_per_mtp;
1706 	struct fixed31_32 pbn;
1707 	struct fixed31_32 pbn_per_slot;
1708 	struct dc_dp_mst_stream_allocation_table proposed_table = {0};
1709 	enum act_return_status ret;
1710 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1711 	DC_LOGGER_INIT(link->ctx->logger);
1712 
1713 	/* notify immediate branch device table update */
1714 	if (dm_helpers_dp_mst_write_payload_allocation_table(
1715 				stream->ctx,
1716 				stream,
1717 				&proposed_table,
1718 				true)) {
1719 		/* update mst stream allocation table software state */
1720 		update_mst_stream_alloc_table(
1721 				link,
1722 				pipe_ctx->stream_res.stream_enc,
1723 				pipe_ctx->stream_res.hpo_dp_stream_enc,
1724 				&proposed_table);
1725 	}
1726 
1727 	print_mst_streams(link);
1728 
1729 	ASSERT(proposed_table.stream_count > 0);
1730 
1731 	/* update mst stream allocation table hardware state */
1732 	if (link_hwss->ext.update_stream_allocation_table == NULL ||
1733 			link_dp_get_encoding_format(&link->cur_link_settings) == DP_UNKNOWN_ENCODING) {
1734 		DC_LOG_ERROR("Failure: unknown encoding format\n");
1735 		return DC_ERROR_UNEXPECTED;
1736 	}
1737 
1738 	link_hwss->ext.update_stream_allocation_table(link, &pipe_ctx->link_res,
1739 			&link->mst_stream_alloc_table);
1740 
1741 	/* poll for immediate branch device ACT handled */
1742 	ret = dm_helpers_dp_mst_poll_for_allocation_change_trigger(
1743 			stream->ctx,
1744 			stream);
1745 
1746 	if (ret != ACT_LINK_LOST) {
1747 		/* send ALLOCATE_PAYLOAD sideband message with updated pbn */
1748 		dm_helpers_dp_mst_send_payload_allocation(
1749 				stream->ctx,
1750 				stream);
1751 	}
1752 
1753 	/* increase throttled vcp size */
1754 	pbn = get_pbn_from_bw_in_kbps(bw_in_kbps);
1755 	pbn_per_slot = get_pbn_per_slot(stream);
1756 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
1757 
1758 	if (link_hwss->ext.set_throttled_vcp_size)
1759 		link_hwss->ext.set_throttled_vcp_size(pipe_ctx, avg_time_slots_per_mtp);
1760 	if (link_hwss->ext.set_hblank_min_symbol_width)
1761 		link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1762 				&link->cur_link_settings,
1763 				avg_time_slots_per_mtp);
1764 
1765 	return DC_OK;
1766 }
1767 
1768 static void disable_link_dp(struct dc_link *link,
1769 		const struct link_resource *link_res,
1770 		enum signal_type signal)
1771 {
1772 	struct dc_link_settings link_settings = link->cur_link_settings;
1773 
1774 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST &&
1775 			link->mst_stream_alloc_table.stream_count > 0)
1776 		/* disable MST link only when last vc payload is deallocated */
1777 		return;
1778 
1779 	dp_disable_link_phy(link, link_res, signal);
1780 
1781 	if (link->connector_signal == SIGNAL_TYPE_EDP) {
1782 		if (!link->skip_implict_edp_power_control)
1783 			link->dc->hwss.edp_power_control(link, false);
1784 	}
1785 
1786 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST && link->sink_count == 0)
1787 		/* set the sink to SST mode after disabling the link */
1788 		enable_mst_on_sink(link, false);
1789 
1790 	if (link_dp_get_encoding_format(&link_settings) ==
1791 			DP_8b_10b_ENCODING) {
1792 		dp_set_fec_enable(link, link_res, false);
1793 		dp_set_fec_ready(link, link_res, false);
1794 	}
1795 }
1796 
1797 static void disable_link(struct dc_link *link,
1798 		const struct link_resource *link_res,
1799 		enum signal_type signal)
1800 {
1801 	if (dc_is_dp_signal(signal)) {
1802 		disable_link_dp(link, link_res, signal);
1803 	} else if (signal == SIGNAL_TYPE_VIRTUAL) {
1804 		link->dc->hwss.disable_link_output(link, link_res, SIGNAL_TYPE_DISPLAY_PORT);
1805 	} else {
1806 		link->dc->hwss.disable_link_output(link, link_res, signal);
1807 	}
1808 
1809 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
1810 		/* MST disable link only when no stream use the link */
1811 		if (link->mst_stream_alloc_table.stream_count <= 0)
1812 			link->link_status.link_active = false;
1813 	} else {
1814 		link->link_status.link_active = false;
1815 	}
1816 }
1817 
1818 static void enable_link_hdmi(struct pipe_ctx *pipe_ctx)
1819 {
1820 	struct dc_stream_state *stream = pipe_ctx->stream;
1821 	struct dc_link *link = stream->link;
1822 	enum dc_color_depth display_color_depth;
1823 	enum engine_id eng_id;
1824 	struct ext_hdmi_settings settings = {0};
1825 	bool is_over_340mhz = false;
1826 	bool is_vga_mode = (stream->timing.h_addressable == 640)
1827 			&& (stream->timing.v_addressable == 480);
1828 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1829 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1830 
1831 	if (stream->phy_pix_clk == 0)
1832 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
1833 	if (stream->phy_pix_clk > 340000)
1834 		is_over_340mhz = true;
1835 	if (dc_is_tmds_signal(stream->signal) && stream->phy_pix_clk > 6000000UL) {
1836 		ASSERT(false);
1837 		return;
1838 	}
1839 
1840 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
1841 		unsigned short masked_chip_caps = pipe_ctx->stream->link->chip_caps &
1842 				AMD_EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
1843 		if (masked_chip_caps == AMD_EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
1844 			/* DP159, Retimer settings */
1845 			eng_id = pipe_ctx->stream_res.stream_enc->id;
1846 
1847 			if (get_ext_hdmi_settings(stream->ctx->dc_bios->integrated_info, eng_id, &settings)) {
1848 				write_i2c_retimer_setting(link, is_vga_mode, is_over_340mhz, &settings);
1849 			} else {
1850 				write_i2c_default_retimer_setting(link, is_vga_mode, is_over_340mhz);
1851 			}
1852 		} else if (masked_chip_caps == AMD_EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
1853 			/* PI3EQX1204, Redriver settings */
1854 			write_i2c_redriver_setting(link, is_over_340mhz);
1855 		}
1856 	}
1857 
1858 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
1859 		write_scdc_data(
1860 			stream->link->ddc,
1861 			stream->phy_pix_clk,
1862 			(stream->timing.flags.LTE_340MCSC_SCRAMBLE != 0));
1863 
1864 	if (dc->debug.enable_hdmi_idcc) {
1865 		union hdmi_idcc_source_id source_id;
1866 
1867 		source_id.raw = 0xff;
1868 		write_idcc_data(stream->link->ddc, HDMI_IDCC_SCOPE_WRITE,
1869 				&source_id.raw, 0, 1);
1870 	}
1871 	memset(&stream->link->cur_link_settings, 0,
1872 			sizeof(struct dc_link_settings));
1873 
1874 	display_color_depth = stream->timing.display_color_depth;
1875 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
1876 		display_color_depth = COLOR_DEPTH_888;
1877 
1878 	/* We need to enable stream encoder for TMDS first to apply 1/4 TMDS
1879 	 * character clock in case that beyond 340MHz.
1880 	 */
1881 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal) || dc_is_dvi_signal(pipe_ctx->stream->signal))
1882 		link_hwss->setup_stream_encoder(pipe_ctx);
1883 
1884 	dc->hwss.enable_tmds_link_output(
1885 			link,
1886 			&pipe_ctx->link_res,
1887 			pipe_ctx->stream->signal,
1888 			pipe_ctx->clock_source->id,
1889 			display_color_depth,
1890 			stream->phy_pix_clk);
1891 
1892 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
1893 		read_scdc_data(link->ddc);
1894 }
1895 
1896 static enum dc_status enable_link_hdmi_frl(struct pipe_ctx *pipe_ctx)
1897 {
1898 	enum link_result link_stat = LINK_RESULT_UNKNOWN;
1899 	enum dc_status status = DC_OK;
1900 	struct dc_stream_state *stream = pipe_ctx->stream;
1901 	struct dc *core_dc = pipe_ctx->stream->ctx->dc;
1902 	enum clock_source_id frl_phy_clock_source_id;
1903 	bool frl_poll_start = true;
1904 
1905 	DC_LOGGER_INIT(stream->ctx->logger);
1906 
1907 	if ((!stream->link->link_enc) ||
1908 			(!stream->link->hpo_frl_link_enc) ||
1909 			(!core_dc->res_pool->dccg->funcs->enable_hdmicharclk) ||
1910 			(!(pipe_ctx->stream_res.hpo_frl_stream_enc)))
1911 		return DC_ERROR_UNEXPECTED;
1912 
1913 	/* get link settings for video mode timing */
1914 	hdmi_frl_decide_link_settings(stream, &stream->link->frl_link_settings, &pipe_ctx->dsc_padding_params);
1915 
1916 	switch (stream->link->frl_link_settings.frl_link_rate) {
1917 	case HDMI_FRL_LINK_RATE_3GBPS:
1918 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 166667;
1919 		break;
1920 	case HDMI_FRL_LINK_RATE_6GBPS:
1921 	case HDMI_FRL_LINK_RATE_6GBPS_4LANE:
1922 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 333333;
1923 		break;
1924 	case HDMI_FRL_LINK_RATE_8GBPS:
1925 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 444444;
1926 		break;
1927 	case HDMI_FRL_LINK_RATE_10GBPS:
1928 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 555555;
1929 		break;
1930 	case HDMI_FRL_LINK_RATE_12GBPS:
1931 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 666667;
1932 		break;
1933 	case HDMI_FRL_LINK_RATE_16GBPS:
1934 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 888889;
1935 		break;
1936 	case HDMI_FRL_LINK_RATE_20GBPS:
1937 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 1111111;
1938 		break;
1939 	case HDMI_FRL_LINK_RATE_24GBPS:
1940 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 1333333;
1941 		break;
1942 	default:
1943 		break;
1944 	}
1945 
1946 	stream->phy_pix_clk = pipe_ctx->stream_res.pix_clk_params.requested_sym_clk;
1947 
1948 	memset(&stream->link->cur_link_settings, 0,
1949 			sizeof(struct dc_link_settings));
1950 
1951 	/* Find proper clock source in HDMI FRL mode for phy used for DCCG */
1952 	frl_phy_clock_source_id = hdmi_frl_find_matching_phypll(stream->link);
1953 
1954 	FRL_INFO("FRL LINK TRAINING:  LTS:P Start\n");
1955 	/* Setup FRL PHY, enable HDMI character clock and HPO link encoder */
1956 	core_dc->hwss.setup_hdmi_frl_link(stream->link,
1957 			(pipe_ctx->stream_res.hpo_frl_stream_enc->id - ENGINE_ID_HPO_0),
1958 			frl_phy_clock_source_id);
1959 
1960 	link_stat = hdmi_frl_perform_link_training_with_retries(stream->link);
1961 
1962 	if (core_dc->res_pool->dccg->funcs->set_valid_pixel_rate)
1963 		core_dc->res_pool->dccg->funcs->set_valid_pixel_rate(
1964 						core_dc->res_pool->dccg,
1965 						core_dc->clk_mgr->funcs->get_dtb_ref_clk_frequency(core_dc->clk_mgr),
1966 						pipe_ctx->stream_res.tg->inst,
1967 						(stream->timing.pix_clk_100hz / 10));
1968 
1969 	/* Enable FRL packet transmission */
1970 	if (link_stat == LINK_RESULT_SUCCESS) {
1971 		stream->link->hpo_frl_link_enc->funcs->enable_output(
1972 				stream->link->hpo_frl_link_enc);
1973 		if (stream->link->frl_flags.apply_vsdb_rcc_wa)
1974 			stream->link->hpo_frl_link_enc->funcs->apply_vsdb_rcc_wa(stream->link->hpo_frl_link_enc);
1975 		if (frl_poll_start)
1976 			hdmi_frl_poll_start(stream->link->ddc);
1977 
1978 		FRL_INFO("FRL LINK TRAINING:  LTS:P Success\n");
1979 
1980 		/* Set HDMISTREAMCLK source to DTBCLK0 and bypass DTO */
1981 		if (core_dc->res_pool->dccg->funcs->set_hdmistreamclk) {
1982 			core_dc->res_pool->dccg->funcs->set_hdmistreamclk(
1983 					core_dc->res_pool->dccg,
1984 					DTBCLK0,
1985 					pipe_ctx->stream_res.tg->inst);
1986 		}
1987 
1988 		pipe_ctx->stream_res.hpo_frl_stream_enc->funcs->hdmi_frl_enable(
1989 			pipe_ctx->stream_res.hpo_frl_stream_enc,
1990 			pipe_ctx->stream_res.tg->inst);
1991 	} else {
1992 		status = DC_FAIL_HDMI_FRL_LINK_TRAINING;
1993 		stream->link->frl_link_settings.frl_link_rate = 0;
1994 	}
1995 
1996 	return status;
1997 }
1998 
1999 static enum dc_status enable_link_dp(struct dc_state *state,
2000 				     struct pipe_ctx *pipe_ctx)
2001 {
2002 	struct dc_stream_state *stream = pipe_ctx->stream;
2003 	enum dc_status status;
2004 	bool skip_video_pattern;
2005 	struct dc_link *link = stream->link;
2006 	const struct dc_link_settings *link_settings =
2007 			&pipe_ctx->link_config.dp_link_settings;
2008 	bool fec_enable;
2009 	int i;
2010 	bool apply_seamless_boot_optimization = false;
2011 	uint32_t bl_oled_enable_delay = 50; // in ms
2012 	uint32_t post_oui_delay = 30; // 30ms
2013 	/* Reduce link bandwidth between failed link training attempts. */
2014 	bool do_fallback = false;
2015 	int lt_attempts = LINK_TRAINING_ATTEMPTS;
2016 
2017 	// Increase retry count if attempting DP1.x on FIXED_VS link
2018 	if (((link->chip_caps & AMD_EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK) == AMD_EXT_DISPLAY_PATH_CAPS__DP_FIXED_VS_EN) &&
2019 			link_dp_get_encoding_format(link_settings) == DP_8b_10b_ENCODING)
2020 		lt_attempts = 10;
2021 
2022 	// check for seamless boot
2023 	for (i = 0; i < state->stream_count; i++) {
2024 		if (state->streams[i]->apply_seamless_boot_optimization) {
2025 			apply_seamless_boot_optimization = true;
2026 			break;
2027 		}
2028 	}
2029 
2030 	/* Train with fallback when enabling DPIA link. Conventional links are
2031 	 * trained with fallback during sink detection.
2032 	 */
2033 	if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA &&
2034 			!link->dc->config.enable_dpia_pre_training)
2035 		do_fallback = true;
2036 
2037 	/*
2038 	 * Temporary w/a to get DP2.0 link rates to work with SST.
2039 	 * TODO DP2.0 - Workaround: Remove w/a if and when the issue is resolved.
2040 	 */
2041 	if (link_dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING &&
2042 			pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT &&
2043 			link->dc->debug.set_mst_en_for_sst) {
2044 		enable_mst_on_sink(link, true);
2045 	} else if (link->dpcd_caps.is_mst_capable &&
2046 		pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT) {
2047 		/* disable mst on sink */
2048 		enable_mst_on_sink(link, false);
2049 	}
2050 
2051 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP) {
2052 		/*in case it is not on*/
2053 		if (!link->dc->config.edp_no_power_sequencing)
2054 			link->dc->hwss.edp_power_control(link, true);
2055 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
2056 	}
2057 
2058 	if (link_dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING) {
2059 		/* TODO - DP2.0 HW: calculate 32 symbol clock for HPO encoder */
2060 	} else {
2061 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk =
2062 				link_settings->link_rate * LINK_RATE_REF_FREQ_IN_KHZ;
2063 		if (state->clk_mgr && !apply_seamless_boot_optimization)
2064 			state->clk_mgr->funcs->update_clocks(state->clk_mgr,
2065 					state, false);
2066 	}
2067 
2068 	// during mode switch we do DP_SET_POWER off then on, and OUI is lost
2069 	dpcd_set_source_specific_data(link);
2070 	if (link->dpcd_sink_ext_caps.raw != 0) {
2071 		post_oui_delay += link->panel_config.pps.extra_post_OUI_ms;
2072 		msleep(post_oui_delay);
2073 	}
2074 
2075 	// similarly, mode switch can cause loss of cable ID
2076 	dpcd_write_cable_id_to_dprx(link);
2077 
2078 	skip_video_pattern = true;
2079 
2080 	if (link_settings->link_rate == LINK_RATE_LOW)
2081 		skip_video_pattern = false;
2082 
2083 	if (stream->sink_patches.oled_optimize_display_on)
2084 		set_default_brightness_aux(link);
2085 
2086 	if (perform_link_training_with_retries(link_settings,
2087 					       skip_video_pattern,
2088 					       lt_attempts,
2089 					       pipe_ctx,
2090 					       pipe_ctx->stream->signal,
2091 					       do_fallback)) {
2092 		status = DC_OK;
2093 	} else {
2094 		status = DC_FAIL_DP_LINK_TRAINING;
2095 	}
2096 
2097 	if (link->preferred_training_settings.fec_enable)
2098 		fec_enable = *link->preferred_training_settings.fec_enable;
2099 	else
2100 		fec_enable = true;
2101 
2102 	if (link_dp_get_encoding_format(link_settings) == DP_8b_10b_ENCODING)
2103 		dp_set_fec_enable(link, &pipe_ctx->link_res, fec_enable);
2104 
2105 	// during mode set we do DP_SET_POWER off then on, aux writes are lost
2106 	if (link->dpcd_sink_ext_caps.bits.oled == 1 ||
2107 		link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1 ||
2108 		link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1) {
2109 		if (!stream->sink_patches.oled_optimize_display_on) {
2110 			set_default_brightness_aux(link);
2111 			if (link->dpcd_sink_ext_caps.bits.oled == 1)
2112 				msleep(bl_oled_enable_delay);
2113 			edp_backlight_enable_aux(link, true);
2114 		} else {
2115 			edp_backlight_enable_aux(link, true);
2116 		}
2117 	}
2118 
2119 	return status;
2120 }
2121 
2122 static enum dc_status enable_link_edp(
2123 		struct dc_state *state,
2124 		struct pipe_ctx *pipe_ctx)
2125 {
2126 	return enable_link_dp(state, pipe_ctx);
2127 }
2128 
2129 static void enable_link_lvds(struct pipe_ctx *pipe_ctx)
2130 {
2131 	struct dc_stream_state *stream = pipe_ctx->stream;
2132 	struct dc_link *link = stream->link;
2133 	struct dc *dc = stream->ctx->dc;
2134 
2135 	if (stream->phy_pix_clk == 0)
2136 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2137 
2138 	memset(&stream->link->cur_link_settings, 0,
2139 			sizeof(struct dc_link_settings));
2140 	dc->hwss.enable_lvds_link_output(
2141 			link,
2142 			&pipe_ctx->link_res,
2143 			pipe_ctx->clock_source->id,
2144 			stream->phy_pix_clk);
2145 
2146 }
2147 
2148 static enum dc_status enable_link_dp_mst(
2149 		struct dc_state *state,
2150 		struct pipe_ctx *pipe_ctx)
2151 {
2152 	struct dc_link *link = pipe_ctx->stream->link;
2153 	unsigned char mstm_cntl = 0;
2154 
2155 	/* sink signal type after MST branch is MST. Multiple MST sinks
2156 	 * share one link. Link DP PHY is enable or training only once.
2157 	 */
2158 	if (link->link_status.link_active)
2159 		return DC_OK;
2160 
2161 	/* clear payload table */
2162 	core_link_read_dpcd(link, DP_MSTM_CTRL, &mstm_cntl, 1);
2163 	if (mstm_cntl & DP_MST_EN)
2164 		dm_helpers_dp_mst_clear_payload_allocation_table(link->ctx, link);
2165 
2166 	/* to make sure the pending down rep can be processed
2167 	 * before enabling the link
2168 	 */
2169 	dm_helpers_dp_mst_poll_pending_down_reply(link->ctx, link);
2170 
2171 	/* set the sink to MST mode before enabling the link */
2172 	enable_mst_on_sink(link, true);
2173 
2174 	return enable_link_dp(state, pipe_ctx);
2175 }
2176 
2177 static enum dc_status enable_link_analog(struct pipe_ctx *pipe_ctx)
2178 {
2179 	struct dc_link *link = pipe_ctx->stream->link;
2180 
2181 	link->dc->hwss.enable_analog_link_output(
2182 		link, pipe_ctx->stream->timing.pix_clk_100hz);
2183 
2184 	return DC_OK;
2185 }
2186 
2187 static enum dc_status enable_link_virtual(struct pipe_ctx *pipe_ctx)
2188 {
2189 	struct dc_link *link = pipe_ctx->stream->link;
2190 
2191 	link->dc->hwss.enable_dp_link_output(link,
2192 			&pipe_ctx->link_res,
2193 			SIGNAL_TYPE_DISPLAY_PORT,
2194 			pipe_ctx->clock_source->id,
2195 			&pipe_ctx->link_config.dp_link_settings);
2196 	return DC_OK;
2197 }
2198 
2199 static enum dc_status enable_link(
2200 		struct dc_state *state,
2201 		struct pipe_ctx *pipe_ctx)
2202 {
2203 	enum dc_status status = DC_ERROR_UNEXPECTED;
2204 	struct dc_stream_state *stream = pipe_ctx->stream;
2205 	struct dc_link *link = NULL;
2206 
2207 	if (stream == NULL)
2208 		return DC_ERROR_UNEXPECTED;
2209 	link = stream->link;
2210 
2211 	/* There's some scenarios where driver is unloaded with display
2212 	 * still enabled. When driver is reloaded, it may cause a display
2213 	 * to not light up if there is a mismatch between old and new
2214 	 * link settings. Need to call disable first before enabling at
2215 	 * new link settings.
2216 	 */
2217 	if (link->link_status.link_active)
2218 		disable_link(link, &pipe_ctx->link_res, pipe_ctx->stream->signal);
2219 
2220 	switch (pipe_ctx->stream->signal) {
2221 	case SIGNAL_TYPE_DISPLAY_PORT:
2222 		status = enable_link_dp(state, pipe_ctx);
2223 		break;
2224 	case SIGNAL_TYPE_EDP:
2225 		status = enable_link_edp(state, pipe_ctx);
2226 		break;
2227 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
2228 		status = enable_link_dp_mst(state, pipe_ctx);
2229 		msleep(200);
2230 		break;
2231 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
2232 	case SIGNAL_TYPE_DVI_DUAL_LINK:
2233 	case SIGNAL_TYPE_HDMI_TYPE_A:
2234 		enable_link_hdmi(pipe_ctx);
2235 		status = DC_OK;
2236 		break;
2237 	case SIGNAL_TYPE_HDMI_FRL:
2238 		if (link->local_sink &&
2239 			link->local_sink->edid_caps.panel_patch.delay_hdmi_link_training &&
2240 			stream->timing.pix_clk_100hz == 6627500) {
2241 			msleep(link->local_sink->edid_caps.panel_patch.delay_hdmi_link_training);
2242 		}
2243 		status = enable_link_hdmi_frl(pipe_ctx);
2244 		break;
2245 	case SIGNAL_TYPE_LVDS:
2246 		enable_link_lvds(pipe_ctx);
2247 		status = DC_OK;
2248 		break;
2249 	case SIGNAL_TYPE_RGB:
2250 		status = enable_link_analog(pipe_ctx);
2251 		break;
2252 	case SIGNAL_TYPE_VIRTUAL:
2253 		status = enable_link_virtual(pipe_ctx);
2254 		break;
2255 	default:
2256 		break;
2257 	}
2258 
2259 	if (status == DC_OK) {
2260 		pipe_ctx->stream->link->link_status.link_active = true;
2261 	}
2262 
2263 	return status;
2264 }
2265 
2266 static bool allocate_usb4_bandwidth_for_stream(struct dc_stream_state *stream, int stream_bw)
2267 {
2268 	struct dc_link *link = stream->sink->link;
2269 	int req_bw = stream_bw;
2270 
2271 	DC_LOGGER_INIT(link->ctx->logger);
2272 
2273 	if (!link->dpia_bw_alloc_config.bw_alloc_enabled)
2274 		return false;
2275 
2276 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2277 		int sink_index = -1;
2278 		unsigned int i = 0;
2279 
2280 		for (i = 0; i < link->sink_count; i++) {
2281 			if (link->remote_sinks[i] == NULL)
2282 				continue;
2283 
2284 			if (stream->sink->sink_id != link->remote_sinks[i]->sink_id) {
2285 				DC_LOG_DEBUG("%s: add remote_sink=%s, request_bw=%d\n", __func__,
2286 					(const char *)(&link->remote_sinks[i]->edid_caps.display_name[0]),
2287 					link->dpia_bw_alloc_config.remote_sink_req_bw[i]);
2288 
2289 				req_bw += link->dpia_bw_alloc_config.remote_sink_req_bw[i];
2290 			} else
2291 				sink_index = i;
2292 		}
2293 
2294 		if (sink_index >= 0)
2295 			link->dpia_bw_alloc_config.remote_sink_req_bw[sink_index] = stream_bw;
2296 		else
2297 			DC_LOG_WARNING("%s: stream sink_id=%u not found in remote_sinks[]\n",
2298 				__func__, stream->sink->sink_id);
2299 
2300 		if (req_bw) {
2301 			link->dpia_bw_alloc_config.dp_overhead = link_dpia_get_dp_overhead(link);
2302 			req_bw += link->dpia_bw_alloc_config.dp_overhead;
2303 		} else
2304 			link->dpia_bw_alloc_config.dp_overhead = 0;
2305 	}
2306 
2307 	link_dp_dpia_allocate_usb4_bandwidth_for_stream(link, req_bw);
2308 
2309 	return true;
2310 }
2311 
2312 static bool allocate_usb4_bandwidth(struct dc_stream_state *stream)
2313 {
2314 	bool ret;
2315 
2316 	int bw = dc_bandwidth_in_kbps_from_timing(&stream->timing,
2317 			dc_link_get_highest_encoding_format(stream->sink->link));
2318 
2319 	ret = allocate_usb4_bandwidth_for_stream(stream, bw);
2320 
2321 	return ret;
2322 }
2323 
2324 static bool deallocate_usb4_bandwidth(struct dc_stream_state *stream)
2325 {
2326 	bool ret;
2327 
2328 	ret = allocate_usb4_bandwidth_for_stream(stream, 0);
2329 
2330 	return ret;
2331 }
2332 
2333 void link_set_dpms_off(struct pipe_ctx *pipe_ctx)
2334 {
2335 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
2336 	struct dc_stream_state *stream = pipe_ctx->stream;
2337 	struct dc_link *link = stream->sink->link;
2338 	struct vpg *vpg = pipe_ctx->stream_res.stream_enc->vpg;
2339 	enum dp_panel_mode panel_mode_dp = dp_get_panel_mode(link);
2340 
2341 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2342 
2343 	ASSERT(is_master_pipe_for_link(link, pipe_ctx));
2344 
2345 	if (dp_is_128b_132b_signal(pipe_ctx))
2346 		vpg = pipe_ctx->stream_res.hpo_dp_stream_enc->vpg;
2347 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
2348 		vpg = pipe_ctx->stream_res.hpo_frl_stream_enc->vpg;
2349 	if (dc_is_virtual_signal(pipe_ctx->stream->signal))
2350 		return;
2351 
2352 	if (pipe_ctx->stream->sink) {
2353 		if (pipe_ctx->stream->sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
2354 			pipe_ctx->stream->sink->sink_signal != SIGNAL_TYPE_NONE) {
2355 			DC_LOG_DC("%s pipe_ctx dispname=%s signal=%x link=%d sink_count=%d\n", __func__,
2356 			pipe_ctx->stream->sink->edid_caps.display_name,
2357 			pipe_ctx->stream->signal, link->link_index, link->sink_count);
2358 		}
2359 	}
2360 
2361 	link_wait_for_unlocked(link);
2362 
2363 	if (!pipe_ctx->stream->sink->edid_caps.panel_patch.skip_avmute) {
2364 		if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
2365 			set_avmute(pipe_ctx, true);
2366 	}
2367 
2368 	dc->hwss.disable_audio_stream(pipe_ctx);
2369 
2370 	update_psp_stream_config(pipe_ctx, true);
2371 	dc->hwss.blank_stream(pipe_ctx);
2372 
2373 	if (pipe_ctx->link_config.dp_tunnel_settings.should_use_dp_bw_allocation)
2374 		deallocate_usb4_bandwidth(pipe_ctx->stream);
2375 
2376 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
2377 		deallocate_mst_payload(pipe_ctx);
2378 	else if (dc_is_dp_sst_signal(pipe_ctx->stream->signal) &&
2379 			dp_is_128b_132b_signal(pipe_ctx))
2380 		update_sst_payload(pipe_ctx, false);
2381 
2382 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
2383 		struct ext_hdmi_settings settings = {0};
2384 		enum engine_id eng_id = pipe_ctx->stream_res.stream_enc->id;
2385 
2386 		unsigned short masked_chip_caps = link->chip_caps &
2387 				AMD_EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
2388 		//Need to inform that sink is going to use legacy HDMI mode.
2389 		write_scdc_data(
2390 			link->ddc,
2391 			165000,//vbios only handles 165Mhz.
2392 			false);
2393 		if (masked_chip_caps == AMD_EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
2394 			/* DP159, Retimer settings */
2395 			if (get_ext_hdmi_settings(stream->ctx->dc_bios->integrated_info, eng_id, &settings))
2396 				write_i2c_retimer_setting(link, false, false, &settings);
2397 			else
2398 				write_i2c_default_retimer_setting(link, false, false);
2399 		} else if (masked_chip_caps == AMD_EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
2400 			/* PI3EQX1204, Redriver settings */
2401 			write_i2c_redriver_setting(link, false);
2402 		}
2403 	}
2404 
2405 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT &&
2406 			!dp_is_128b_132b_signal(pipe_ctx)) {
2407 
2408 		/* In DP1.x SST mode, our encoder will go to TPS1
2409 		 * when link is on but stream is off.
2410 		 * Disabling link before stream will avoid exposing TPS1 pattern
2411 		 * during the disable sequence as it will confuse some receivers
2412 		 * state machine.
2413 		 * In DP2 or MST mode, our encoder will stay video active
2414 		 */
2415 		disable_link(pipe_ctx->stream->link, &pipe_ctx->link_res, pipe_ctx->stream->signal);
2416 		dc->hwss.disable_stream(pipe_ctx);
2417 	} else {
2418 		dc->hwss.disable_stream(pipe_ctx);
2419 		disable_link(pipe_ctx->stream->link, &pipe_ctx->link_res, pipe_ctx->stream->signal);
2420 	}
2421 	edp_set_panel_assr(link, pipe_ctx, &panel_mode_dp, false);
2422 
2423 	if (pipe_ctx->stream->timing.flags.DSC) {
2424 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
2425 			link_set_dsc_enable(pipe_ctx, false);
2426 	}
2427 	if (dp_is_128b_132b_signal(pipe_ctx)) {
2428 		if (pipe_ctx->stream_res.tg->funcs->set_out_mux)
2429 			pipe_ctx->stream_res.tg->funcs->set_out_mux(pipe_ctx->stream_res.tg, OUT_MUX_DIO);
2430 	}
2431 
2432 	if (vpg && vpg->funcs->vpg_powerdown)
2433 		vpg->funcs->vpg_powerdown(vpg);
2434 
2435 	/* for psp not exist case */
2436 	if (link->connector_signal == SIGNAL_TYPE_EDP && dc->debug.psp_disabled_wa) {
2437 		/* reset internal save state to default since eDP is  off */
2438 		enum dp_panel_mode panel_mode = dp_get_panel_mode(pipe_ctx->stream->link);
2439 		/* since current psp not loaded, we need to reset it to default */
2440 		link->panel_mode = panel_mode;
2441 	}
2442 }
2443 
2444 void link_set_dpms_on(
2445 		struct dc_state *state,
2446 		struct pipe_ctx *pipe_ctx)
2447 {
2448 	struct dc *dc = pipe_ctx->stream->ctx->dc;
2449 	struct dc_stream_state *stream = pipe_ctx->stream;
2450 	struct dc_link *link = stream->sink->link;
2451 	enum dc_status status;
2452 	struct link_encoder *link_enc = pipe_ctx->link_res.dio_link_enc;
2453 	enum otg_out_mux_dest otg_out_dest = OUT_MUX_DIO;
2454 	struct vpg *vpg = pipe_ctx->stream_res.stream_enc->vpg;
2455 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
2456 	bool apply_edp_fast_boot_optimization =
2457 		pipe_ctx->stream->apply_edp_fast_boot_optimization;
2458 
2459 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2460 
2461 	ASSERT(is_master_pipe_for_link(link, pipe_ctx));
2462 
2463 	if (dp_is_128b_132b_signal(pipe_ctx))
2464 		vpg = pipe_ctx->stream_res.hpo_dp_stream_enc->vpg;
2465 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
2466 		vpg = pipe_ctx->stream_res.hpo_frl_stream_enc->vpg;
2467 	if (dc_is_virtual_signal(pipe_ctx->stream->signal))
2468 		return;
2469 
2470 	if (pipe_ctx->stream->sink) {
2471 		if (pipe_ctx->stream->sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
2472 			pipe_ctx->stream->sink->sink_signal != SIGNAL_TYPE_NONE) {
2473 			DC_LOG_DC("%s pipe_ctx dispname=%s signal=%x link=%d sink_count=%d\n", __func__,
2474 			pipe_ctx->stream->sink->edid_caps.display_name,
2475 			pipe_ctx->stream->signal,
2476 			link->link_index,
2477 			link->sink_count);
2478 		}
2479 	}
2480 
2481 	link_wait_for_unlocked(stream->link);
2482 	if (!dc->config.unify_link_enc_assignment)
2483 		link_enc = link_enc_cfg_get_link_enc(link);
2484 	ASSERT(link_enc);
2485 
2486 	if (!dc_is_virtual_signal(pipe_ctx->stream->signal)
2487 			&& !dc_is_hdmi_frl_signal(pipe_ctx->stream->signal)
2488 			&& !dp_is_128b_132b_signal(pipe_ctx)) {
2489 		if (link_enc)
2490 			link_enc->funcs->setup(
2491 				link_enc,
2492 				pipe_ctx->stream->signal);
2493 	}
2494 
2495 	pipe_ctx->stream->link->link_state_valid = true;
2496 
2497 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
2498 		hdmi_frl_decide_link_settings(stream, &stream->link->frl_link_settings, &pipe_ctx->dsc_padding_params);
2499 
2500 	if (pipe_ctx->stream_res.tg->funcs->set_out_mux) {
2501 		if (dp_is_128b_132b_signal(pipe_ctx))
2502 			otg_out_dest = OUT_MUX_HPO_DP;
2503 		else if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
2504 			otg_out_dest = OUT_MUX_HPO_FRL;
2505 		else
2506 			otg_out_dest = OUT_MUX_DIO;
2507 		pipe_ctx->stream_res.tg->funcs->set_out_mux(pipe_ctx->stream_res.tg, otg_out_dest);
2508 	}
2509 
2510 	link_hwss->setup_stream_attribute(pipe_ctx);
2511 
2512 	pipe_ctx->stream->apply_edp_fast_boot_optimization = false;
2513 
2514 	// Enable VPG before building infoframe
2515 	if (vpg && vpg->funcs->vpg_poweron)
2516 		vpg->funcs->vpg_poweron(vpg);
2517 
2518 	resource_build_info_frame(pipe_ctx);
2519 	dc->hwss.update_info_frame(pipe_ctx);
2520 
2521 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
2522 		dp_trace_source_sequence(link, DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
2523 
2524 	/* Do not touch link on seamless boot optimization. */
2525 	if (pipe_ctx->stream->apply_seamless_boot_optimization) {
2526 		pipe_ctx->stream->dpms_off = false;
2527 
2528 		/* Still enable stream features & audio on seamless boot for DP external displays */
2529 		if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT) {
2530 			enable_stream_features(pipe_ctx);
2531 			dc->hwss.enable_audio_stream(pipe_ctx);
2532 		}
2533 
2534 		update_psp_stream_config(pipe_ctx, false);
2535 		return;
2536 	}
2537 
2538 	/* eDP lit up by bios already, no need to enable again. */
2539 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP &&
2540 				apply_edp_fast_boot_optimization &&
2541 				!pipe_ctx->stream->timing.flags.DSC &&
2542 				!pipe_ctx->next_odm_pipe) {
2543 		pipe_ctx->stream->dpms_off = false;
2544 		update_psp_stream_config(pipe_ctx, false);
2545 
2546 		if (link->is_dds) {
2547 			uint32_t post_oui_delay = 30; // 30ms
2548 
2549 			dpcd_set_source_specific_data(link);
2550 			msleep(post_oui_delay);
2551 		}
2552 
2553 		return;
2554 	}
2555 
2556 	if (pipe_ctx->stream->dpms_off)
2557 		return;
2558 
2559 	/* For Dp tunneling link, a pending HPD means that we have a race condition between processing
2560 	 * current link and processing the pending HPD. If we enable the link now, we may end up with a
2561 	 * link that is not actually connected to a sink. So we skip enabling the link in this case.
2562 	 */
2563 	if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA && link->is_hpd_pending) {
2564 		DC_LOG_DEBUG("%s, Link%d HPD is pending, not enable it.\n", __func__, link->link_index);
2565 		return;
2566 	}
2567 
2568 	/* Have to setup DSC before DIG FE and BE are connected (which happens before the
2569 	 * link training). This is to make sure the bandwidth sent to DIG BE won't be
2570 	 * bigger than what the link and/or DIG BE can handle. VBID[6]/CompressedStream_flag
2571 	 * will be automatically set at a later time when the video is enabled
2572 	 * (DP_VID_STREAM_EN = 1).
2573 	 */
2574 	if (pipe_ctx->stream->timing.flags.DSC) {
2575 		if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
2576 		    dc_is_virtual_signal(pipe_ctx->stream->signal))
2577 			link_set_dsc_enable(pipe_ctx, true);
2578 	}
2579 
2580 	if (link->replay_settings.config.replay_supported && !dc_is_embedded_signal(link->connector_signal))
2581 		dp_setup_replay(link, stream);
2582 
2583 	status = enable_link(state, pipe_ctx);
2584 
2585 	if (status != DC_OK) {
2586 		DC_LOG_WARNING("enabling link %u failed: %d\n",
2587 		pipe_ctx->stream->link->link_index,
2588 		status);
2589 
2590 		/* Abort stream enable *unless* the failure was due to
2591 		 * DP link training - some DP monitors will recover and
2592 		 * show the stream anyway. But MST displays can't proceed
2593 		 * without link training.
2594 		 */
2595 			if ((status != DC_FAIL_DP_LINK_TRAINING &&
2596 					status != DC_FAIL_HDMI_FRL_LINK_TRAINING) ||
2597 					pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2598 			if (false == stream->link->link_status.link_active)
2599 				disable_link(stream->link, &pipe_ctx->link_res,
2600 						pipe_ctx->stream->signal);
2601 			BREAK_TO_DEBUGGER();
2602 			return;
2603 		}
2604 	}
2605 
2606 	/* turn off otg test pattern if enable */
2607 	if (pipe_ctx->stream_res.tg->funcs->set_test_pattern)
2608 		pipe_ctx->stream_res.tg->funcs->set_test_pattern(pipe_ctx->stream_res.tg,
2609 				CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
2610 				COLOR_DEPTH_UNDEFINED);
2611 
2612 	/* This second call is needed to reconfigure the DIG
2613 	 * as a workaround for the incorrect value being applied
2614 	 * from transmitter control.
2615 	 */
2616 	if (!(dc_is_virtual_signal(pipe_ctx->stream->signal) ||
2617 			dc_is_hdmi_frl_signal(pipe_ctx->stream->signal) ||
2618 			dp_is_128b_132b_signal(pipe_ctx))) {
2619 
2620 			if (link_enc)
2621 				link_enc->funcs->setup(
2622 					link_enc,
2623 					pipe_ctx->stream->signal);
2624 
2625 		}
2626 
2627 	dc->hwss.enable_stream(pipe_ctx);
2628 
2629 	/* Set DPS PPS SDP (AKA "info frames") */
2630 	if (pipe_ctx->stream->timing.flags.DSC) {
2631 		if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
2632 				dc_is_virtual_signal(pipe_ctx->stream->signal)) {
2633 			dp_set_dsc_on_rx(pipe_ctx, true);
2634 			link_set_dsc_pps_packet(pipe_ctx, true, true);
2635 		}
2636 	}
2637 
2638 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
2639 		dp_set_hblank_reduction_on_rx(pipe_ctx);
2640 
2641 	if (pipe_ctx->link_config.dp_tunnel_settings.should_use_dp_bw_allocation)
2642 		allocate_usb4_bandwidth(pipe_ctx->stream);
2643 
2644 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
2645 		allocate_mst_payload(pipe_ctx);
2646 	else if (dc_is_dp_sst_signal(pipe_ctx->stream->signal) &&
2647 			dp_is_128b_132b_signal(pipe_ctx))
2648 		update_sst_payload(pipe_ctx, true);
2649 
2650 	/* Corruption was observed on systems with display mux when stream gets
2651 	 * enabled after the mux switch. Having a small delay between link
2652 	 * training and stream unblank resolves the corruption issue.
2653 	 * This is workaround.
2654 	 */
2655 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP &&
2656 			link->is_display_mux_present)
2657 		msleep(20);
2658 
2659 	dc->hwss.unblank_stream(pipe_ctx,
2660 		&pipe_ctx->stream->link->cur_link_settings);
2661 
2662 	if (stream->sink_patches.delay_ignore_msa > 0)
2663 		msleep(stream->sink_patches.delay_ignore_msa);
2664 
2665 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
2666 		enable_stream_features(pipe_ctx);
2667 	update_psp_stream_config(pipe_ctx, false);
2668 
2669 	dc->hwss.enable_audio_stream(pipe_ctx);
2670 
2671 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
2672 		set_avmute(pipe_ctx, false);
2673 	}
2674 }
2675