xref: /linux/drivers/gpu/drm/amd/display/dc/link/link_dpms.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
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_DEBUG("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_DEBUG("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_DEBUG("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_DEBUG("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_DEBUG("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_DEBUG("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_DEBUG("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_DEBUG("Set redriver failed");
528 	return success;
529 }
530 
531 static struct link_encoder *get_link_encoder(struct pipe_ctx *pipe_ctx)
532 {
533 	struct link_encoder *link_enc
534 			= pipe_ctx->stream->ctx->dc->config.unify_link_enc_assignment
535 			? pipe_ctx->link_res.dio_link_enc
536 			: link_enc_cfg_get_link_enc(pipe_ctx->stream->link);
537 
538 	ASSERT(link_enc);
539 	return link_enc;
540 }
541 
542 static void update_psp_stream_config(struct pipe_ctx *pipe_ctx, bool dpms_off)
543 {
544 	struct cp_psp *cp_psp = &pipe_ctx->stream->ctx->cp_psp;
545 	struct link_encoder *link_enc = get_link_encoder(pipe_ctx);
546 	struct cp_psp_stream_config config = {0};
547 	enum dp_panel_mode panel_mode =
548 			dp_get_panel_mode(pipe_ctx->stream->link);
549 
550 	if (cp_psp == NULL || cp_psp->funcs.update_stream_config == NULL)
551 		return;
552 	if (link_enc == NULL)
553 		return;
554 
555 	/* otg instance */
556 	config.otg_inst = (uint8_t) pipe_ctx->stream_res.tg->inst;
557 
558 	/* dig front end */
559 	config.dig_fe = (uint8_t) pipe_ctx->stream_res.stream_enc->stream_enc_inst;
560 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
561 		config.dig_fe = (uint8_t)pipe_ctx->stream_res.hpo_frl_stream_enc->stream_enc_inst;
562 
563 	/* stream encoder index */
564 	config.stream_enc_idx = (uint8_t)(pipe_ctx->stream_res.stream_enc->id - ENGINE_ID_DIGA);
565 	if (dp_is_128b_132b_signal(pipe_ctx))
566 		config.stream_enc_idx =
567 				(uint8_t)(pipe_ctx->stream_res.hpo_dp_stream_enc->id - ENGINE_ID_HPO_DP_0);
568 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
569 		config.stream_enc_idx =
570 				(uint8_t)(pipe_ctx->stream_res.hpo_frl_stream_enc->id - ENGINE_ID_HPO_0);
571 
572 	/* dig back end */
573 	config.dig_be = pipe_ctx->stream->link->link_enc_hw_inst;
574 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
575 		config.dig_be = (uint8_t)pipe_ctx->stream_res.hpo_frl_stream_enc->stream_enc_inst;
576 
577 	/* link encoder index */
578 	config.link_enc_idx = (uint8_t)(link_enc->transmitter - TRANSMITTER_UNIPHY_A);
579 	if (dp_is_128b_132b_signal(pipe_ctx))
580 		config.link_enc_idx = (uint8_t)pipe_ctx->link_res.hpo_dp_link_enc->inst;
581 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
582 		config.link_enc_idx = (uint8_t)pipe_ctx->stream->link->hpo_frl_link_enc->inst;
583 
584 	/* dio output index is dpia index for DPIA endpoint & dcio index by default */
585 	if (pipe_ctx->stream->link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA)
586 		config.dio_output_idx = (uint8_t)(pipe_ctx->stream->link->link_id.enum_id - ENUM_ID_1);
587 	else
588 		config.dio_output_idx = (uint8_t)(link_enc->transmitter - TRANSMITTER_UNIPHY_A);
589 
590 
591 	/* phy index */
592 	config.phy_idx = resource_transmitter_to_phy_idx(
593 			pipe_ctx->stream->link->dc, link_enc->transmitter);
594 	if (pipe_ctx->stream->link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA)
595 		/* USB4 DPIA doesn't use PHY in our soc, initialize it to 0 */
596 		config.phy_idx = 0;
597 
598 	/* stream properties */
599 	config.assr_enabled = (panel_mode == DP_PANEL_MODE_EDP) ? 1 : 0;
600 	config.mst_enabled = (pipe_ctx->stream->signal ==
601 			SIGNAL_TYPE_DISPLAY_PORT_MST) ? 1 : 0;
602 	config.frl_enabled = dc_is_hdmi_frl_signal(pipe_ctx->stream->signal) ? 1 : 0;
603 	config.dp2_enabled = dp_is_128b_132b_signal(pipe_ctx) ? 1 : 0;
604 	config.usb4_enabled = (pipe_ctx->stream->link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA) ?
605 			1 : 0;
606 	config.dpms_off = dpms_off;
607 
608 	/* dm stream context */
609 	config.dm_stream_ctx = pipe_ctx->stream->dm_stream_context;
610 
611 	cp_psp->funcs.update_stream_config(cp_psp->handle, &config);
612 }
613 
614 void link_wait_for_unlocked(struct dc_link *link)
615 {
616 	unsigned long long enter_timestamp;
617 	unsigned long long finish_timestamp;
618 	unsigned long long time_taken_in_ns;
619 	bool waited = false;
620 
621 	DC_LOGGER_INIT(link->ctx->logger);
622 
623 	enter_timestamp = dm_get_timestamp(link->ctx);
624 
625 	while (link->is_link_locked) { // busy wait
626 		if (!waited) {
627 			DC_LOG_DC("%s: 0x%p ...", __func__, link);
628 
629 			waited = true;
630 		}
631 
632 		udelay(1);
633 	}
634 
635 	if (!waited)
636 		return;
637 
638 	finish_timestamp = dm_get_timestamp(link->ctx);
639 	time_taken_in_ns = dm_get_elapse_time_in_ns(link->ctx,
640 							finish_timestamp, enter_timestamp);
641 
642 	DC_LOG_DC("%s: 0x%p took %llu ms.", __func__,
643 			link, div_u64(time_taken_in_ns, 1000000));
644 }
645 
646 static void set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
647 {
648 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
649 
650 	if (!dc_is_hdmi_signal(pipe_ctx->stream->signal))
651 		return;
652 
653 	if (pipe_ctx->stream->timing.flags.DSC)
654 		return;
655 
656 	dc->hwss.set_avmute(pipe_ctx, enable);
657 }
658 
659 static void enable_mst_on_sink(struct dc_link *link, bool enable)
660 {
661 	unsigned char mstmCntl = 0;
662 
663 	core_link_read_dpcd(link, DP_MSTM_CTRL, &mstmCntl, 1);
664 	if (enable)
665 		mstmCntl |= DP_MST_EN;
666 	else
667 		mstmCntl &= (~DP_MST_EN);
668 
669 	core_link_write_dpcd(link, DP_MSTM_CTRL, &mstmCntl, 1);
670 }
671 
672 static void dsc_optc_config_log(struct display_stream_compressor *dsc,
673 		struct dsc_optc_config *config)
674 {
675 	uint32_t precision = 1 << 28;
676 	uint32_t bytes_per_pixel_int = config->bytes_per_pixel / precision;
677 	uint32_t bytes_per_pixel_mod = config->bytes_per_pixel % precision;
678 	uint64_t ll_bytes_per_pix_fraq = bytes_per_pixel_mod;
679 	DC_LOGGER_INIT(dsc->ctx->logger);
680 
681 	/* 7 fractional digits decimal precision for bytes per pixel is enough because DSC
682 	 * bits per pixel precision is 1/16th of a pixel, which means bytes per pixel precision is
683 	 * 1/16/8 = 1/128 of a byte, or 0.0078125 decimal
684 	 */
685 	ll_bytes_per_pix_fraq *= 10000000;
686 	ll_bytes_per_pix_fraq /= precision;
687 
688 	DC_LOG_DSC("\tbytes_per_pixel 0x%08x (%d.%07d)",
689 			config->bytes_per_pixel, bytes_per_pixel_int, (uint32_t)ll_bytes_per_pix_fraq);
690 	DC_LOG_DSC("\tis_pixel_format_444 %d", config->is_pixel_format_444);
691 	DC_LOG_DSC("\tslice_width %d", config->slice_width);
692 }
693 
694 static bool dp_set_dsc_on_rx(struct pipe_ctx *pipe_ctx, bool enable)
695 {
696 	struct dc *dc = pipe_ctx->stream->ctx->dc;
697 	struct dc_stream_state *stream = pipe_ctx->stream;
698 	bool result = false;
699 
700 	if (dc_is_virtual_signal(stream->signal))
701 		result = true;
702 	else
703 		result = dm_helpers_dp_write_dsc_enable(dc->ctx, stream, enable);
704 	return result;
705 }
706 
707 static bool dp_set_hblank_reduction_on_rx(struct pipe_ctx *pipe_ctx)
708 {
709 	struct dc *dc = pipe_ctx->stream->ctx->dc;
710 	struct dc_stream_state *stream = pipe_ctx->stream;
711 	bool result = false;
712 
713 	if (dc_is_virtual_signal(stream->signal))
714 		result = true;
715 	else
716 		result = dm_helpers_dp_write_hblank_reduction(dc->ctx, stream);
717 	return result;
718 }
719 
720 
721 /* The stream with these settings can be sent (unblanked) only after DSC was enabled on RX first,
722  * i.e. after dp_enable_dsc_on_rx() had been called
723  */
724 void link_set_dsc_on_stream(struct pipe_ctx *pipe_ctx, bool enable)
725 {
726 	/* TODO: Move this to HWSS as this is hardware programming sequence not a
727 	 * link layer sequence
728 	 */
729 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
730 	struct dc *dc = pipe_ctx->stream->ctx->dc;
731 	struct dc_stream_state *stream = pipe_ctx->stream;
732 	struct pipe_ctx *odm_pipe;
733 	int opp_cnt = 1;
734 	struct dccg *dccg = dc->res_pool->dccg;
735 	/* It has been found that when DSCCLK is lower than 16Mhz, we will get DCN
736 	 * register access hung. When DSCCLk is based on refclk, DSCCLk is always a
737 	 * fixed value higher than 16Mhz so the issue doesn't occur. When DSCCLK is
738 	 * generated by DTO, DSCCLK would be based on 1/3 dispclk. For small timings
739 	 * with DSC such as 480p60Hz, the dispclk could be low enough to trigger
740 	 * this problem. We are implementing a workaround here to keep using dscclk
741 	 * based on fixed value refclk when timing is smaller than 3x16Mhz (i.e
742 	 * 48Mhz) pixel clock to avoid hitting this problem.
743 	 */
744 	bool should_use_dto_dscclk = (dccg->funcs->set_dto_dscclk != NULL) &&
745 			stream->timing.pix_clk_100hz > 480000;
746 	DC_LOGGER_INIT(dsc->ctx->logger);
747 
748 	for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe)
749 		opp_cnt++;
750 
751 	if (enable) {
752 		struct dsc_config dsc_cfg;
753 		struct dsc_optc_config dsc_optc_cfg = {0};
754 		enum optc_dsc_mode optc_dsc_mode;
755 
756 		/* Enable DSC hw block */
757 		dsc_cfg.pic_width = (stream->timing.h_addressable + pipe_ctx->dsc_padding_params.dsc_hactive_padding +
758 				stream->timing.h_border_left + stream->timing.h_border_right) / opp_cnt;
759 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top + stream->timing.v_border_bottom;
760 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
761 		dsc_cfg.color_depth = stream->timing.display_color_depth;
762 		dsc_cfg.is_odm = pipe_ctx->next_odm_pipe ? true : false;
763 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
764 		ASSERT(dsc_cfg.dc_dsc_cfg.num_slices_h % opp_cnt == 0);
765 		dsc_cfg.dc_dsc_cfg.num_slices_h /= opp_cnt;
766 		dsc_cfg.dsc_padding = 0;
767 
768 		if (should_use_dto_dscclk)
769 			dccg->funcs->set_dto_dscclk(dccg, dsc->inst, dsc_cfg.dc_dsc_cfg.num_slices_h);
770 		dsc->funcs->dsc_set_config(dsc, &dsc_cfg, &dsc_optc_cfg);
771 		dsc->funcs->dsc_enable(dsc, pipe_ctx->stream_res.opp->inst);
772 		for (odm_pipe = pipe_ctx->next_odm_pipe; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
773 			struct display_stream_compressor *odm_dsc = odm_pipe->stream_res.dsc;
774 
775 			if (should_use_dto_dscclk)
776 				dccg->funcs->set_dto_dscclk(dccg, odm_dsc->inst, dsc_cfg.dc_dsc_cfg.num_slices_h);
777 			odm_dsc->funcs->dsc_set_config(odm_dsc, &dsc_cfg, &dsc_optc_cfg);
778 			odm_dsc->funcs->dsc_enable(odm_dsc, odm_pipe->stream_res.opp->inst);
779 		}
780 		dsc_cfg.dc_dsc_cfg.num_slices_h *= opp_cnt;
781 		dsc_cfg.pic_width *= opp_cnt;
782 		dsc_cfg.dsc_padding = pipe_ctx->dsc_padding_params.dsc_hactive_padding;
783 
784 		optc_dsc_mode = dsc_optc_cfg.is_pixel_format_444 ? OPTC_DSC_ENABLED_444 : OPTC_DSC_ENABLED_NATIVE_SUBSAMPLED;
785 
786 		/* Enable DSC in encoder */
787 		if (dc_is_dp_signal(stream->signal) && !dp_is_128b_132b_signal(pipe_ctx)) {
788 			DC_LOG_DSC("Setting stream encoder DSC config for engine %d:", (int)pipe_ctx->stream_res.stream_enc->id);
789 			dsc_optc_config_log(dsc, &dsc_optc_cfg);
790 			if (pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_config)
791 				pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_config(pipe_ctx->stream_res.stream_enc,
792 										optc_dsc_mode,
793 										dsc_optc_cfg.bytes_per_pixel,
794 										dsc_optc_cfg.slice_width);
795 
796 			/* PPS SDP is set elsewhere because it has to be done after DIG FE is connected to DIG BE */
797 		}
798 		else if (dc_is_hdmi_frl_signal(stream->signal)) {
799 			uint8_t dsc_packed_pps[128];
800 			struct dc_crtc_timing patched_crtc_timing = stream->timing;
801 
802 			DC_LOG_DSC("Setting stream encoder DSC config for engine %d:", (int)pipe_ctx->stream_res.hpo_frl_stream_enc->id);
803 			dsc_optc_config_log(dsc, &dsc_optc_cfg);
804 
805 			/* if we are borrowing from hblank, h_addressable and  pic_width need to be adjusted */
806 			if (dc->debug.enable_hblank_borrow) {
807 				dsc_cfg.pic_width = stream->timing.h_addressable;
808 			}
809 
810 			dsc->funcs->dsc_get_packed_pps(dsc, &dsc_cfg, &dsc_packed_pps[0]);
811 			pipe_ctx->stream_res.hpo_frl_stream_enc->funcs->hdmi_frl_set_dsc_config(
812 					pipe_ctx->stream_res.hpo_frl_stream_enc, &patched_crtc_timing, &dsc_packed_pps[0]);
813 		}
814 
815 		/* Enable DSC in OPTC */
816 		DC_LOG_DSC("Setting optc DSC config for tg instance %d:", pipe_ctx->stream_res.tg->inst);
817 		dsc_optc_config_log(dsc, &dsc_optc_cfg);
818 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(pipe_ctx->stream_res.tg,
819 							optc_dsc_mode,
820 							dsc_optc_cfg.bytes_per_pixel,
821 							dsc_optc_cfg.slice_width);
822 	} else {
823 		/* disable DSC in OPTC */
824 		pipe_ctx->stream_res.tg->funcs->set_dsc_config(
825 				pipe_ctx->stream_res.tg,
826 				OPTC_DSC_DISABLED, 0, 0);
827 
828 		/* disable DSC in stream encoder */
829 		if (dc_is_dp_signal(stream->signal)) {
830 			if (dp_is_128b_132b_signal(pipe_ctx))
831 				pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_set_dsc_pps_info_packet(
832 										pipe_ctx->stream_res.hpo_dp_stream_enc,
833 										false,
834 										NULL,
835 										true);
836 			else {
837 				if (pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_config)
838 					pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_config(
839 							pipe_ctx->stream_res.stream_enc,
840 							OPTC_DSC_DISABLED, 0, 0);
841 				pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_pps_info_packet(
842 							pipe_ctx->stream_res.stream_enc, false, NULL, true);
843 			}
844 		}
845 		else if (dc_is_hdmi_frl_signal(stream->signal))
846 			pipe_ctx->stream_res.hpo_frl_stream_enc->funcs->hdmi_frl_set_dsc_config(
847 					pipe_ctx->stream_res.hpo_frl_stream_enc, &stream->timing, NULL);
848 
849 		/* disable DSC block */
850 		for (odm_pipe = pipe_ctx; odm_pipe; odm_pipe = odm_pipe->next_odm_pipe) {
851 			odm_pipe->stream_res.dsc->funcs->dsc_disconnect(odm_pipe->stream_res.dsc);
852 			/*
853 			 * TODO - dsc_disconnect is a double buffered register.
854 			 * by the time we call dsc_disable, dsc may still remain
855 			 * connected to OPP. In this case OPTC will no longer
856 			 * get correct pixel data because DSCC is off. However
857 			 * we also can't wait for the  disconnect pending
858 			 * complete, because this function can be called
859 			 * with/without OTG master lock acquired. When the lock
860 			 * is acquired we will never get pending complete until
861 			 * we release the lock later. So there is no easy way to
862 			 * solve this problem especially when the lock is
863 			 * acquired. DSC is a front end hw block it should be
864 			 * programmed as part of front end sequence, where the
865 			 * commit sequence without lock and update sequence
866 			 * with lock are completely separated. However because
867 			 * we are programming dsc as part of back end link
868 			 * programming sequence, we don't know if front end OPTC
869 			 * master lock is acquired. The back end should be
870 			 * agnostic to front end lock. DSC programming shouldn't
871 			 * belong to this sequence.
872 			 */
873 			odm_pipe->stream_res.dsc->funcs->dsc_disable(odm_pipe->stream_res.dsc);
874 			if (dccg->funcs->set_ref_dscclk)
875 				dccg->funcs->set_ref_dscclk(dccg, odm_pipe->stream_res.dsc->inst);
876 		}
877 	}
878 }
879 
880 /*
881  * For dynamic bpp change case, dsc is programmed with MASTER_UPDATE_LOCK enabled;
882  * hence PPS info packet update need to use frame update instead of immediate update.
883  * Added parameter immediate_update for this purpose.
884  * The decision to use frame update is hard-coded in function dp_update_dsc_config(),
885  * which is the only place where a "false" would be passed in for param immediate_update.
886  *
887  * immediate_update is only applicable when DSC is enabled.
888  */
889 bool link_set_dsc_pps_packet(struct pipe_ctx *pipe_ctx, bool enable, bool immediate_update)
890 {
891 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
892 	struct dc_stream_state *stream = pipe_ctx->stream;
893 
894 	if (!pipe_ctx->stream->timing.flags.DSC)
895 		return false;
896 
897 	if (!dsc)
898 		return false;
899 
900 	DC_LOGGER_INIT(dsc->ctx->logger);
901 
902 	if (enable) {
903 		struct dsc_config dsc_cfg;
904 		uint8_t dsc_packed_pps[128];
905 
906 		memset(&dsc_cfg, 0, sizeof(dsc_cfg));
907 		memset(dsc_packed_pps, 0, 128);
908 
909 		/* Enable DSC hw block */
910 		dsc_cfg.pic_width = stream->timing.h_addressable + stream->timing.h_border_left + stream->timing.h_border_right;
911 		dsc_cfg.pic_height = stream->timing.v_addressable + stream->timing.v_border_top + stream->timing.v_border_bottom;
912 		dsc_cfg.pixel_encoding = stream->timing.pixel_encoding;
913 		dsc_cfg.color_depth = stream->timing.display_color_depth;
914 		dsc_cfg.is_odm = pipe_ctx->next_odm_pipe ? true : false;
915 		dsc_cfg.dc_dsc_cfg = stream->timing.dsc_cfg;
916 		dsc_cfg.dsc_padding = pipe_ctx->dsc_padding_params.dsc_hactive_padding;
917 
918 		dsc->funcs->dsc_get_packed_pps(dsc, &dsc_cfg, &dsc_packed_pps[0]);
919 		memcpy(&stream->dsc_packed_pps[0], &dsc_packed_pps[0], sizeof(stream->dsc_packed_pps));
920 		if (dc_is_dp_signal(stream->signal)) {
921 			DC_LOG_DSC("Setting stream encoder DSC PPS SDP for engine %d\n", (int)pipe_ctx->stream_res.stream_enc->id);
922 			if (dp_is_128b_132b_signal(pipe_ctx))
923 				pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_set_dsc_pps_info_packet(
924 										pipe_ctx->stream_res.hpo_dp_stream_enc,
925 										true,
926 										&dsc_packed_pps[0],
927 										immediate_update);
928 			else
929 				pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_pps_info_packet(
930 						pipe_ctx->stream_res.stream_enc,
931 						true,
932 						&dsc_packed_pps[0],
933 						immediate_update);
934 		}
935 		else if (dc_is_hdmi_frl_signal(stream->signal)) {
936 			//TODO: bring HDMI FRL in line with DP
937 			DC_LOG_DSC("Setting stream encoder DSC PPS SDP for engine %d\n", (int)pipe_ctx->stream_res.hpo_frl_stream_enc->id);
938 			pipe_ctx->stream_res.hpo_frl_stream_enc->funcs->hdmi_frl_set_dsc_config(
939 					pipe_ctx->stream_res.hpo_frl_stream_enc, &stream->timing, &dsc_packed_pps[0]);
940 		}
941 	} else {
942 		/* disable DSC PPS in stream encoder */
943 		memset(&stream->dsc_packed_pps[0], 0, sizeof(stream->dsc_packed_pps));
944 		if (dc_is_dp_signal(stream->signal)) {
945 			if (dp_is_128b_132b_signal(pipe_ctx))
946 				pipe_ctx->stream_res.hpo_dp_stream_enc->funcs->dp_set_dsc_pps_info_packet(
947 										pipe_ctx->stream_res.hpo_dp_stream_enc,
948 										false,
949 										NULL,
950 										true);
951 			else
952 				pipe_ctx->stream_res.stream_enc->funcs->dp_set_dsc_pps_info_packet(
953 						pipe_ctx->stream_res.stream_enc, false, NULL, true);
954 		}
955 		else if (dc_is_hdmi_frl_signal(stream->signal))
956 			//TODO: bring HDMI FRL in line with DP
957 			pipe_ctx->stream_res.hpo_frl_stream_enc->funcs->hdmi_frl_set_dsc_config(
958 					pipe_ctx->stream_res.hpo_frl_stream_enc, &stream->timing, NULL);
959 	}
960 
961 	return true;
962 }
963 
964 bool link_set_dsc_enable(struct pipe_ctx *pipe_ctx, bool enable)
965 {
966 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
967 	bool result = false;
968 
969 	if (!pipe_ctx->stream->timing.flags.DSC)
970 		goto out;
971 	if (!dsc)
972 		goto out;
973 
974 	if (enable) {
975 		{
976 			link_set_dsc_on_stream(pipe_ctx, true);
977 			result = true;
978 		}
979 	} else {
980 		dp_set_dsc_on_rx(pipe_ctx, false);
981 		link_set_dsc_on_stream(pipe_ctx, false);
982 		result = true;
983 	}
984 out:
985 	return result;
986 }
987 
988 bool link_update_dsc_config(struct pipe_ctx *pipe_ctx)
989 {
990 	struct display_stream_compressor *dsc = pipe_ctx->stream_res.dsc;
991 
992 	if (!pipe_ctx->stream->timing.flags.DSC)
993 		return false;
994 	if (!dsc)
995 		return false;
996 
997 	link_set_dsc_on_stream(pipe_ctx, true);
998 	link_set_dsc_pps_packet(pipe_ctx, true, false);
999 	return true;
1000 }
1001 
1002 static void enable_stream_features(struct pipe_ctx *pipe_ctx)
1003 {
1004 	struct dc_stream_state *stream = pipe_ctx->stream;
1005 
1006 	if (pipe_ctx->stream->signal != SIGNAL_TYPE_DISPLAY_PORT_MST) {
1007 		struct dc_link *link = stream->link;
1008 		union down_spread_ctrl old_downspread;
1009 		union down_spread_ctrl new_downspread;
1010 
1011 		memset(&old_downspread, 0, sizeof(old_downspread));
1012 
1013 		core_link_read_dpcd(link, DP_DOWNSPREAD_CTRL,
1014 				&old_downspread.raw, sizeof(old_downspread));
1015 
1016 		new_downspread.raw = old_downspread.raw;
1017 
1018 		new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
1019 				(stream->ignore_msa_timing_param) ? 1 : 0;
1020 
1021 		if (new_downspread.raw != old_downspread.raw) {
1022 			core_link_write_dpcd(link, DP_DOWNSPREAD_CTRL,
1023 				&new_downspread.raw, sizeof(new_downspread));
1024 		}
1025 
1026 	} else {
1027 		dm_helpers_mst_enable_stream_features(stream);
1028 	}
1029 }
1030 
1031 static void log_vcp_x_y(const struct dc_link *link, struct fixed31_32 avg_time_slots_per_mtp)
1032 {
1033 	const uint32_t VCP_Y_PRECISION = 1000;
1034 	uint64_t vcp_x, vcp_y;
1035 	DC_LOGGER_INIT(link->ctx->logger);
1036 
1037 	// Add 0.5*(1/VCP_Y_PRECISION) to round up to decimal precision
1038 	avg_time_slots_per_mtp = dc_fixpt_add(
1039 			avg_time_slots_per_mtp,
1040 			dc_fixpt_from_fraction(
1041 				1,
1042 				2*VCP_Y_PRECISION));
1043 
1044 	vcp_x = dc_fixpt_floor(
1045 			avg_time_slots_per_mtp);
1046 	vcp_y = dc_fixpt_floor(
1047 			dc_fixpt_mul_int(
1048 				dc_fixpt_sub_int(
1049 					avg_time_slots_per_mtp,
1050 					dc_fixpt_floor(
1051 							avg_time_slots_per_mtp)),
1052 				VCP_Y_PRECISION));
1053 
1054 
1055 	if (link->type == dc_connection_mst_branch)
1056 		DC_LOG_DP2("MST Update Payload: set_throttled_vcp_size slot X.Y for MST stream "
1057 				"X: %llu "
1058 				"Y: %llu/%d",
1059 				vcp_x,
1060 				vcp_y,
1061 				VCP_Y_PRECISION);
1062 	else
1063 		DC_LOG_DP2("SST Update Payload: set_throttled_vcp_size slot X.Y for SST stream "
1064 				"X: %llu "
1065 				"Y: %llu/%d",
1066 				vcp_x,
1067 				vcp_y,
1068 				VCP_Y_PRECISION);
1069 }
1070 
1071 static struct fixed31_32 get_pbn_per_slot(struct dc_stream_state *stream)
1072 {
1073 	struct fixed31_32 mbytes_per_sec;
1074 	uint32_t link_rate_in_mbytes_per_sec = dp_link_bandwidth_kbps(stream->link,
1075 			&stream->link->cur_link_settings);
1076 	link_rate_in_mbytes_per_sec /= 8000; /* Kbits to MBytes */
1077 
1078 	mbytes_per_sec = dc_fixpt_from_int(link_rate_in_mbytes_per_sec);
1079 
1080 	return dc_fixpt_div_int(mbytes_per_sec, 54);
1081 }
1082 
1083 static struct fixed31_32 get_pbn_from_bw_in_kbps(uint64_t kbps)
1084 {
1085 	struct fixed31_32 peak_kbps;
1086 	uint32_t numerator = 0;
1087 	uint32_t denominator = 1;
1088 
1089 	/*
1090 	 * The 1.006 factor (margin 5300ppm + 300ppm ~ 0.6% as per spec) is not
1091 	 * required when determining PBN/time slot utilization on the link between
1092 	 * us and the branch, since that overhead is already accounted for in
1093 	 * the get_pbn_per_slot function.
1094 	 *
1095 	 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
1096 	 * common multiplier to render an integer PBN for all link rate/lane
1097 	 * counts combinations
1098 	 * calculate
1099 	 * peak_kbps *= (64/54)
1100 	 * peak_kbps /= (8 * 1000) convert to bytes
1101 	 */
1102 
1103 	numerator = 64;
1104 	denominator = 54 * 8 * 1000;
1105 	kbps *= numerator;
1106 	peak_kbps = dc_fixpt_from_fraction(kbps, denominator);
1107 
1108 	return peak_kbps;
1109 }
1110 
1111 static struct fixed31_32 get_pbn_from_timing(struct pipe_ctx *pipe_ctx)
1112 {
1113 	uint64_t kbps;
1114 	enum dc_link_encoding_format link_encoding;
1115 
1116 	if (dp_is_128b_132b_signal(pipe_ctx))
1117 		link_encoding = DC_LINK_ENCODING_DP_128b_132b;
1118 	else
1119 		link_encoding = DC_LINK_ENCODING_DP_8b_10b;
1120 
1121 	kbps = dc_bandwidth_in_kbps_from_timing(&pipe_ctx->stream->timing, link_encoding);
1122 	return get_pbn_from_bw_in_kbps(kbps);
1123 }
1124 
1125 
1126 // TODO - DP2.0 Link: Fix get_lane_status to handle LTTPR offset (SST and MST)
1127 static void get_lane_status(
1128 	struct dc_link *link,
1129 	uint32_t lane_count,
1130 	union lane_status *status,
1131 	union lane_align_status_updated *status_updated)
1132 {
1133 	unsigned int lane;
1134 	uint8_t dpcd_buf[3] = {0};
1135 
1136 	if (status == NULL || status_updated == NULL) {
1137 		return;
1138 	}
1139 
1140 	core_link_read_dpcd(
1141 			link,
1142 			DP_LANE0_1_STATUS,
1143 			dpcd_buf,
1144 			sizeof(dpcd_buf));
1145 
1146 	for (lane = 0; lane < lane_count; lane++) {
1147 		status[lane].raw = dp_get_nibble_at_index(&dpcd_buf[0], lane);
1148 	}
1149 
1150 	status_updated->raw = dpcd_buf[2];
1151 }
1152 
1153 static bool poll_for_allocation_change_trigger(struct dc_link *link)
1154 {
1155 	/*
1156 	 * wait for ACT handled
1157 	 */
1158 	int i;
1159 	const int act_retries = 30;
1160 	enum act_return_status result = ACT_FAILED;
1161 	enum dc_connection_type display_connected = (link->type != dc_connection_none);
1162 	union payload_table_update_status update_status = {0};
1163 	union lane_status dpcd_lane_status[LANE_COUNT_DP_MAX];
1164 	union lane_align_status_updated lane_status_updated;
1165 	DC_LOGGER_INIT(link->ctx->logger);
1166 
1167 	if (!display_connected || link->aux_access_disabled)
1168 		return true;
1169 	for (i = 0; i < act_retries; i++) {
1170 		get_lane_status(link, link->cur_link_settings.lane_count, dpcd_lane_status, &lane_status_updated);
1171 
1172 		if (!dp_is_cr_done(link->cur_link_settings.lane_count, dpcd_lane_status) ||
1173 				!dp_is_ch_eq_done(link->cur_link_settings.lane_count, dpcd_lane_status) ||
1174 				!dp_is_symbol_locked(link->cur_link_settings.lane_count, dpcd_lane_status) ||
1175 				!dp_is_interlane_aligned(lane_status_updated)) {
1176 			DC_LOG_ERROR("SST Update Payload: Link loss occurred while "
1177 					"polling for ACT handled.");
1178 			result = ACT_LINK_LOST;
1179 			break;
1180 		}
1181 		core_link_read_dpcd(
1182 				link,
1183 				DP_PAYLOAD_TABLE_UPDATE_STATUS,
1184 				&update_status.raw,
1185 				1);
1186 
1187 		if (update_status.bits.ACT_HANDLED == 1) {
1188 			DC_LOG_DP2("SST Update Payload: ACT handled by downstream.");
1189 			result = ACT_SUCCESS;
1190 			break;
1191 		}
1192 
1193 		fsleep(5000);
1194 	}
1195 
1196 	if (result == ACT_FAILED) {
1197 		DC_LOG_ERROR("SST Update Payload: ACT still not handled after retries, "
1198 				"continue on. Something is wrong with the branch.");
1199 	}
1200 
1201 	return (result == ACT_SUCCESS);
1202 }
1203 
1204 static void update_mst_stream_alloc_table(
1205 	struct dc_link *link,
1206 	struct stream_encoder *stream_enc,
1207 	struct hpo_dp_stream_encoder *hpo_dp_stream_enc, // TODO: Rename stream_enc to dio_stream_enc?
1208 	const struct dc_dp_mst_stream_allocation_table *proposed_table)
1209 {
1210 	struct link_mst_stream_allocation work_table[MAX_CONTROLLER_NUM] = { 0 };
1211 	struct link_mst_stream_allocation *dc_alloc;
1212 
1213 	int i;
1214 	int j;
1215 
1216 	/* if DRM proposed_table has more than one new payload */
1217 	ASSERT(proposed_table->stream_count -
1218 			link->mst_stream_alloc_table.stream_count < 2);
1219 
1220 	/* copy proposed_table to link, add stream encoder */
1221 	for (i = 0; i < proposed_table->stream_count; i++) {
1222 
1223 		for (j = 0; j < link->mst_stream_alloc_table.stream_count; j++) {
1224 			dc_alloc =
1225 			&link->mst_stream_alloc_table.stream_allocations[j];
1226 
1227 			if (dc_alloc->vcp_id ==
1228 				proposed_table->stream_allocations[i].vcp_id) {
1229 
1230 				work_table[i] = *dc_alloc;
1231 				work_table[i].slot_count = proposed_table->stream_allocations[i].slot_count;
1232 				break; /* exit j loop */
1233 			}
1234 		}
1235 
1236 		/* new vcp_id */
1237 		if (j == link->mst_stream_alloc_table.stream_count) {
1238 			work_table[i].vcp_id =
1239 				proposed_table->stream_allocations[i].vcp_id;
1240 			work_table[i].slot_count =
1241 				proposed_table->stream_allocations[i].slot_count;
1242 			work_table[i].stream_enc = stream_enc;
1243 			work_table[i].hpo_dp_stream_enc = hpo_dp_stream_enc;
1244 		}
1245 	}
1246 
1247 	/* update link->mst_stream_alloc_table with work_table */
1248 	link->mst_stream_alloc_table.stream_count =
1249 			proposed_table->stream_count;
1250 	for (i = 0; i < MAX_CONTROLLER_NUM; i++)
1251 		link->mst_stream_alloc_table.stream_allocations[i] =
1252 				work_table[i];
1253 }
1254 
1255 static void remove_stream_from_alloc_table(
1256 		struct dc_link *link,
1257 		struct stream_encoder *dio_stream_enc,
1258 		struct hpo_dp_stream_encoder *hpo_dp_stream_enc)
1259 {
1260 	int i = 0;
1261 	struct link_mst_stream_allocation_table *table =
1262 			&link->mst_stream_alloc_table;
1263 
1264 	if (hpo_dp_stream_enc) {
1265 		for (; i < table->stream_count; i++)
1266 			if (hpo_dp_stream_enc == table->stream_allocations[i].hpo_dp_stream_enc)
1267 				break;
1268 	} else {
1269 		for (; i < table->stream_count; i++)
1270 			if (dio_stream_enc == table->stream_allocations[i].stream_enc)
1271 				break;
1272 	}
1273 
1274 	if (i < table->stream_count) {
1275 		i++;
1276 		for (; i < table->stream_count; i++)
1277 			table->stream_allocations[i-1] = table->stream_allocations[i];
1278 		memset(&table->stream_allocations[table->stream_count-1], 0,
1279 				sizeof(struct link_mst_stream_allocation));
1280 		table->stream_count--;
1281 	}
1282 }
1283 
1284 static void print_mst_streams(struct dc_link *link)
1285 {
1286 	int i;
1287 
1288 	DC_LOGGER_INIT(link->ctx->logger);
1289 
1290 	DC_LOG_MST("%s stream_count: %d:\n",
1291 		   __func__,
1292 		   link->mst_stream_alloc_table.stream_count);
1293 
1294 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
1295 		DC_LOG_MST("stream_enc[%d]: %p\n", i,
1296 			   (void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc);
1297 		DC_LOG_MST("stream[%d].hpo_dp_stream_enc: %p\n", i,
1298 			   (void *) link->mst_stream_alloc_table.stream_allocations[i].hpo_dp_stream_enc);
1299 		DC_LOG_MST("stream[%d].vcp_id: %d\n", i,
1300 			   link->mst_stream_alloc_table.stream_allocations[i].vcp_id);
1301 		DC_LOG_MST("stream[%d].slot_count: %d\n", i,
1302 			   link->mst_stream_alloc_table.stream_allocations[i].slot_count);
1303 	}
1304 }
1305 
1306 static enum dc_status deallocate_mst_payload(struct pipe_ctx *pipe_ctx)
1307 {
1308 	struct dc_stream_state *stream = pipe_ctx->stream;
1309 	struct dc_link *link = stream->link;
1310 	struct dc_dp_mst_stream_allocation_table proposed_table = {0};
1311 	struct fixed31_32 avg_time_slots_per_mtp = dc_fixpt_from_int(0);
1312 	bool mst_mode = (link->type == dc_connection_mst_branch);
1313 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1314 	const struct dc_link_settings empty_link_settings = {0};
1315 	DC_LOGGER_INIT(link->ctx->logger);
1316 
1317 	/* deallocate_mst_payload is called before disable link. When mode or
1318 	 * disable/enable monitor, new stream is created which is not in link
1319 	 * stream[] yet. For this, payload is not allocated yet, so de-alloc
1320 	 * should not done. For new mode set, map_resources will get engine
1321 	 * for new stream, so stream_enc->id should be validated until here.
1322 	 */
1323 
1324 	/* slot X.Y */
1325 	if (link_hwss->ext.set_throttled_vcp_size)
1326 		link_hwss->ext.set_throttled_vcp_size(pipe_ctx, avg_time_slots_per_mtp);
1327 	if (link_hwss->ext.set_hblank_min_symbol_width)
1328 		link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1329 				&empty_link_settings,
1330 				avg_time_slots_per_mtp);
1331 
1332 	if (mst_mode) {
1333 		/* when link is in mst mode, reply on mst manager to remove
1334 		 * payload
1335 		 */
1336 		if (dm_helpers_dp_mst_write_payload_allocation_table(
1337 				stream->ctx,
1338 				stream,
1339 				&proposed_table,
1340 				false))
1341 			update_mst_stream_alloc_table(
1342 					link,
1343 					pipe_ctx->stream_res.stream_enc,
1344 					pipe_ctx->stream_res.hpo_dp_stream_enc,
1345 					&proposed_table);
1346 		else
1347 			DC_LOG_WARNING("Failed to update MST allocation table for idx %d\n",
1348 					pipe_ctx->pipe_idx);
1349 	} else {
1350 		/* when link is no longer in mst mode (mst hub unplugged),
1351 		 * remove payload with default dc logic
1352 		 */
1353 		remove_stream_from_alloc_table(link, pipe_ctx->stream_res.stream_enc,
1354 				pipe_ctx->stream_res.hpo_dp_stream_enc);
1355 	}
1356 
1357 	print_mst_streams(link);
1358 
1359 	/* update mst stream allocation table hardware state */
1360 	if (link_hwss->ext.update_stream_allocation_table == NULL ||
1361 			link_dp_get_encoding_format(&link->cur_link_settings) == DP_UNKNOWN_ENCODING) {
1362 		DC_LOG_DEBUG("Unknown encoding format\n");
1363 		return DC_ERROR_UNEXPECTED;
1364 	}
1365 
1366 	link_hwss->ext.update_stream_allocation_table(link, &pipe_ctx->link_res,
1367 			&link->mst_stream_alloc_table);
1368 
1369 	if (mst_mode)
1370 		dm_helpers_dp_mst_poll_for_allocation_change_trigger(
1371 			stream->ctx,
1372 			stream);
1373 
1374 	dm_helpers_dp_mst_update_mst_mgr_for_deallocation(
1375 			stream->ctx,
1376 			stream);
1377 
1378 	return DC_OK;
1379 }
1380 
1381 /* convert link_mst_stream_alloc_table to dm dp_mst_stream_alloc_table
1382  * because stream_encoder is not exposed to dm
1383  */
1384 static enum dc_status allocate_mst_payload(struct pipe_ctx *pipe_ctx)
1385 {
1386 	struct dc_stream_state *stream = pipe_ctx->stream;
1387 	struct dc_link *link = stream->link;
1388 	struct dc_dp_mst_stream_allocation_table proposed_table = {0};
1389 	struct fixed31_32 avg_time_slots_per_mtp;
1390 	struct fixed31_32 pbn;
1391 	struct fixed31_32 pbn_per_slot;
1392 	enum act_return_status ret;
1393 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1394 	DC_LOGGER_INIT(link->ctx->logger);
1395 
1396 	/* enable_link_dp_mst already check link->enabled_stream_count
1397 	 * and stream is in link->stream[]. This is called during set mode,
1398 	 * stream_enc is available.
1399 	 */
1400 
1401 	/* get calculate VC payload for stream: stream_alloc */
1402 	if (dm_helpers_dp_mst_write_payload_allocation_table(
1403 		stream->ctx,
1404 		stream,
1405 		&proposed_table,
1406 		true))
1407 		update_mst_stream_alloc_table(
1408 					link,
1409 					pipe_ctx->stream_res.stream_enc,
1410 					pipe_ctx->stream_res.hpo_dp_stream_enc,
1411 					&proposed_table);
1412 	else
1413 		DC_LOG_WARNING("Failed to update MST allocation table for idx %d\n",
1414 				pipe_ctx->pipe_idx);
1415 
1416 	print_mst_streams(link);
1417 
1418 	ASSERT(proposed_table.stream_count > 0);
1419 
1420 	/* program DP source TX for payload */
1421 	if (link_hwss->ext.update_stream_allocation_table == NULL ||
1422 			link_dp_get_encoding_format(&link->cur_link_settings) == DP_UNKNOWN_ENCODING) {
1423 		DC_LOG_ERROR("Failure: unknown encoding format\n");
1424 		return DC_ERROR_UNEXPECTED;
1425 	}
1426 
1427 	link_hwss->ext.update_stream_allocation_table(link,
1428 			&pipe_ctx->link_res,
1429 			&link->mst_stream_alloc_table);
1430 
1431 	/* send down message */
1432 	ret = dm_helpers_dp_mst_poll_for_allocation_change_trigger(
1433 			stream->ctx,
1434 			stream);
1435 
1436 	if (ret != ACT_LINK_LOST)
1437 		dm_helpers_dp_mst_send_payload_allocation(
1438 				stream->ctx,
1439 				stream);
1440 
1441 	/* slot X.Y for only current stream */
1442 	pbn_per_slot = get_pbn_per_slot(stream);
1443 	if (pbn_per_slot.value == 0) {
1444 		DC_LOG_ERROR("Failure: pbn_per_slot==0 not allowed. Cannot continue, returning DC_UNSUPPORTED_VALUE.\n");
1445 		return DC_UNSUPPORTED_VALUE;
1446 	}
1447 	pbn = get_pbn_from_timing(pipe_ctx);
1448 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
1449 
1450 	log_vcp_x_y(link, avg_time_slots_per_mtp);
1451 
1452 	if (link_hwss->ext.set_throttled_vcp_size)
1453 		link_hwss->ext.set_throttled_vcp_size(pipe_ctx, avg_time_slots_per_mtp);
1454 	if (link_hwss->ext.set_hblank_min_symbol_width)
1455 		link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1456 				&link->cur_link_settings,
1457 				avg_time_slots_per_mtp);
1458 
1459 	return DC_OK;
1460 }
1461 
1462 struct fixed31_32 link_calculate_sst_avg_time_slots_per_mtp(
1463 		const struct dc_stream_state *stream,
1464 		const struct dc_link *link)
1465 {
1466 	struct fixed31_32 link_bw_effective =
1467 			dc_fixpt_from_int(
1468 					dp_link_bandwidth_kbps(link, &link->cur_link_settings));
1469 	struct fixed31_32 timeslot_bw_effective =
1470 			dc_fixpt_div_int(link_bw_effective, MAX_MTP_SLOT_COUNT);
1471 	struct fixed31_32 timing_bw =
1472 			dc_fixpt_from_int(
1473 					dc_bandwidth_in_kbps_from_timing(&stream->timing,
1474 							dc_link_get_highest_encoding_format(link)));
1475 	struct fixed31_32 avg_time_slots_per_mtp =
1476 			dc_fixpt_div(timing_bw, timeslot_bw_effective);
1477 
1478 	return avg_time_slots_per_mtp;
1479 }
1480 
1481 
1482 static bool write_128b_132b_sst_payload_allocation_table(
1483 		const struct dc_stream_state *stream,
1484 		struct dc_link *link,
1485 		struct link_mst_stream_allocation_table *proposed_table,
1486 		bool allocate)
1487 {
1488 	const uint8_t vc_id = 1; /// VC ID always 1 for SST
1489 	const uint8_t start_time_slot = 0; /// Always start at time slot 0 for SST
1490 	bool result = false;
1491 	uint8_t req_slot_count = 0;
1492 	struct fixed31_32 avg_time_slots_per_mtp = { 0 };
1493 	union payload_table_update_status update_status = { 0 };
1494 	const uint32_t max_retries = 30;
1495 	uint32_t retries = 0;
1496 	enum dc_connection_type display_connected = (link->type != dc_connection_none);
1497 	DC_LOGGER_INIT(link->ctx->logger);
1498 
1499 	if (allocate)	{
1500 		avg_time_slots_per_mtp = link_calculate_sst_avg_time_slots_per_mtp(stream, link);
1501 		req_slot_count = (uint8_t)dc_fixpt_ceil(avg_time_slots_per_mtp);
1502 		/// Validation should filter out modes that exceed link BW
1503 		ASSERT(req_slot_count <= MAX_MTP_SLOT_COUNT);
1504 		if (req_slot_count > MAX_MTP_SLOT_COUNT)
1505 			return false;
1506 	} else {
1507 		/// Leave req_slot_count = 0 if allocate is false.
1508 	}
1509 
1510 	proposed_table->stream_count = 1; /// Always 1 stream for SST
1511 	proposed_table->stream_allocations[0].slot_count = req_slot_count;
1512 	proposed_table->stream_allocations[0].vcp_id = vc_id;
1513 
1514 	if (!display_connected || link->aux_access_disabled)
1515 		return true;
1516 
1517 	/// Write DPCD 2C0 = 1 to start updating
1518 	update_status.bits.VC_PAYLOAD_TABLE_UPDATED = 1;
1519 	core_link_write_dpcd(
1520 			link,
1521 			DP_PAYLOAD_TABLE_UPDATE_STATUS,
1522 			&update_status.raw,
1523 			1);
1524 
1525 	/// Program the changes in DPCD 1C0 - 1C2
1526 	ASSERT(vc_id == 1);
1527 	core_link_write_dpcd(
1528 			link,
1529 			DP_PAYLOAD_ALLOCATE_SET,
1530 			&vc_id,
1531 			1);
1532 
1533 	ASSERT(start_time_slot == 0);
1534 	core_link_write_dpcd(
1535 			link,
1536 			DP_PAYLOAD_ALLOCATE_START_TIME_SLOT,
1537 			&start_time_slot,
1538 			1);
1539 
1540 	core_link_write_dpcd(
1541 			link,
1542 			DP_PAYLOAD_ALLOCATE_TIME_SLOT_COUNT,
1543 			&req_slot_count,
1544 			1);
1545 
1546 	/// Poll till DPCD 2C0 read 1
1547 	/// Try for at least 150ms (30 retries, with 5ms delay after each attempt)
1548 
1549 	while (retries < max_retries) {
1550 		if (core_link_read_dpcd(
1551 				link,
1552 				DP_PAYLOAD_TABLE_UPDATE_STATUS,
1553 				&update_status.raw,
1554 				1) == DC_OK) {
1555 			if (update_status.bits.VC_PAYLOAD_TABLE_UPDATED == 1) {
1556 				DC_LOG_DP2("SST Update Payload: downstream payload table updated.");
1557 				result = true;
1558 				break;
1559 			}
1560 		} else {
1561 			union dpcd_rev dpcdRev = {0};
1562 
1563 			if (core_link_read_dpcd(
1564 					link,
1565 					DP_DPCD_REV,
1566 					&dpcdRev.raw,
1567 					1) != DC_OK) {
1568 				DC_LOG_ERROR("SST Update Payload: Unable to read DPCD revision "
1569 						"of sink while polling payload table "
1570 						"updated status bit.");
1571 				break;
1572 			}
1573 		}
1574 		retries++;
1575 		fsleep(5000);
1576 	}
1577 
1578 	if (!result && retries == max_retries) {
1579 		DC_LOG_ERROR("SST Update Payload: Payload table not updated after retries, "
1580 				"continue on. Something is wrong with the branch.");
1581 		// TODO - DP2.0 Payload: Read and log the payload table from downstream branch
1582 	}
1583 
1584 	return result;
1585 }
1586 
1587 /*
1588  * Payload allocation/deallocation for SST introduced in DP2.0
1589  */
1590 static enum dc_status update_sst_payload(struct pipe_ctx *pipe_ctx,
1591 						 bool allocate)
1592 {
1593 	struct dc_stream_state *stream = pipe_ctx->stream;
1594 	struct dc_link *link = stream->link;
1595 	struct link_mst_stream_allocation_table proposed_table = {0};
1596 	struct fixed31_32 avg_time_slots_per_mtp;
1597 	const struct dc_link_settings empty_link_settings = {0};
1598 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1599 	DC_LOGGER_INIT(link->ctx->logger);
1600 
1601 	/* slot X.Y for SST payload deallocate */
1602 	if (!allocate) {
1603 		avg_time_slots_per_mtp = dc_fixpt_from_int(0);
1604 
1605 		log_vcp_x_y(link, avg_time_slots_per_mtp);
1606 
1607 		if (link_hwss->ext.set_throttled_vcp_size)
1608 			link_hwss->ext.set_throttled_vcp_size(pipe_ctx,
1609 					avg_time_slots_per_mtp);
1610 		if (link_hwss->ext.set_hblank_min_symbol_width)
1611 			link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1612 					&empty_link_settings,
1613 					avg_time_slots_per_mtp);
1614 	}
1615 
1616 	/* calculate VC payload and update branch with new payload allocation table*/
1617 	if (!write_128b_132b_sst_payload_allocation_table(
1618 			stream,
1619 			link,
1620 			&proposed_table,
1621 			allocate)) {
1622 		DC_LOG_ERROR("SST Update Payload: Failed to update "
1623 						"allocation table for "
1624 						"pipe idx: %d\n",
1625 						pipe_ctx->pipe_idx);
1626 		return DC_FAIL_DP_PAYLOAD_ALLOCATION;
1627 	}
1628 
1629 	proposed_table.stream_allocations[0].hpo_dp_stream_enc = pipe_ctx->stream_res.hpo_dp_stream_enc;
1630 
1631 	ASSERT(proposed_table.stream_count == 1);
1632 
1633 	//TODO - DP2.0 Logging: Instead of hpo_dp_stream_enc pointer, log instance id
1634 	DC_LOG_DP2("SST Update Payload: hpo_dp_stream_enc: %p      "
1635 		"vcp_id: %d      "
1636 		"slot_count: %d\n",
1637 		(void *) proposed_table.stream_allocations[0].hpo_dp_stream_enc,
1638 		proposed_table.stream_allocations[0].vcp_id,
1639 		proposed_table.stream_allocations[0].slot_count);
1640 
1641 	/* program DP source TX for payload */
1642 	link_hwss->ext.update_stream_allocation_table(link, &pipe_ctx->link_res,
1643 			&proposed_table);
1644 
1645 	/* poll for ACT handled */
1646 	if (!poll_for_allocation_change_trigger(link)) {
1647 		// Failures will result in blackscreen and errors logged
1648 		BREAK_TO_DEBUGGER();
1649 	}
1650 
1651 	/* slot X.Y for SST payload allocate */
1652 	if (allocate && link_dp_get_encoding_format(&link->cur_link_settings) ==
1653 			DP_128b_132b_ENCODING) {
1654 		avg_time_slots_per_mtp = link_calculate_sst_avg_time_slots_per_mtp(stream, link);
1655 
1656 		log_vcp_x_y(link, avg_time_slots_per_mtp);
1657 
1658 		if (link_hwss->ext.set_throttled_vcp_size)
1659 			link_hwss->ext.set_throttled_vcp_size(pipe_ctx,
1660 					avg_time_slots_per_mtp);
1661 		if (link_hwss->ext.set_hblank_min_symbol_width)
1662 			link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1663 					&link->cur_link_settings,
1664 					avg_time_slots_per_mtp);
1665 	}
1666 
1667 	/* Always return DC_OK.
1668 	 * If part of sequence fails, log failure(s) and show blackscreen
1669 	 */
1670 	return DC_OK;
1671 }
1672 
1673 enum dc_status link_reduce_mst_payload(struct pipe_ctx *pipe_ctx, uint32_t bw_in_kbps)
1674 {
1675 	struct dc_stream_state *stream = pipe_ctx->stream;
1676 	struct dc_link *link = stream->link;
1677 	struct fixed31_32 avg_time_slots_per_mtp;
1678 	struct fixed31_32 pbn;
1679 	struct fixed31_32 pbn_per_slot;
1680 	struct dc_dp_mst_stream_allocation_table proposed_table = {0};
1681 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1682 	DC_LOGGER_INIT(link->ctx->logger);
1683 
1684 	/* decrease throttled vcp size */
1685 	pbn_per_slot = get_pbn_per_slot(stream);
1686 	pbn = get_pbn_from_bw_in_kbps(bw_in_kbps);
1687 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
1688 
1689 	if (link_hwss->ext.set_throttled_vcp_size)
1690 		link_hwss->ext.set_throttled_vcp_size(pipe_ctx, avg_time_slots_per_mtp);
1691 	if (link_hwss->ext.set_hblank_min_symbol_width)
1692 		link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1693 				&link->cur_link_settings,
1694 				avg_time_slots_per_mtp);
1695 
1696 	/* send ALLOCATE_PAYLOAD sideband message with updated pbn */
1697 	dm_helpers_dp_mst_send_payload_allocation(
1698 			stream->ctx,
1699 			stream);
1700 
1701 	/* notify immediate branch device table update */
1702 	if (dm_helpers_dp_mst_write_payload_allocation_table(
1703 			stream->ctx,
1704 			stream,
1705 			&proposed_table,
1706 			true)) {
1707 		/* update mst stream allocation table software state */
1708 		update_mst_stream_alloc_table(
1709 				link,
1710 				pipe_ctx->stream_res.stream_enc,
1711 				pipe_ctx->stream_res.hpo_dp_stream_enc,
1712 				&proposed_table);
1713 	} else {
1714 		DC_LOG_WARNING("Failed to update MST allocation table for idx %d\n",
1715 				pipe_ctx->pipe_idx);
1716 	}
1717 
1718 	print_mst_streams(link);
1719 
1720 	ASSERT(proposed_table.stream_count > 0);
1721 
1722 	/* update mst stream allocation table hardware state */
1723 	if (link_hwss->ext.update_stream_allocation_table == NULL ||
1724 			link_dp_get_encoding_format(&link->cur_link_settings) == DP_UNKNOWN_ENCODING) {
1725 		DC_LOG_ERROR("Failure: unknown encoding format\n");
1726 		return DC_ERROR_UNEXPECTED;
1727 	}
1728 
1729 	link_hwss->ext.update_stream_allocation_table(link, &pipe_ctx->link_res,
1730 			&link->mst_stream_alloc_table);
1731 
1732 	/* poll for immediate branch device ACT handled */
1733 	dm_helpers_dp_mst_poll_for_allocation_change_trigger(
1734 			stream->ctx,
1735 			stream);
1736 
1737 	return DC_OK;
1738 }
1739 
1740 enum dc_status link_increase_mst_payload(struct pipe_ctx *pipe_ctx, uint32_t bw_in_kbps)
1741 {
1742 	struct dc_stream_state *stream = pipe_ctx->stream;
1743 	struct dc_link *link = stream->link;
1744 	struct fixed31_32 avg_time_slots_per_mtp;
1745 	struct fixed31_32 pbn;
1746 	struct fixed31_32 pbn_per_slot;
1747 	struct dc_dp_mst_stream_allocation_table proposed_table = {0};
1748 	enum act_return_status ret;
1749 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1750 	DC_LOGGER_INIT(link->ctx->logger);
1751 
1752 	/* notify immediate branch device table update */
1753 	if (dm_helpers_dp_mst_write_payload_allocation_table(
1754 				stream->ctx,
1755 				stream,
1756 				&proposed_table,
1757 				true)) {
1758 		/* update mst stream allocation table software state */
1759 		update_mst_stream_alloc_table(
1760 				link,
1761 				pipe_ctx->stream_res.stream_enc,
1762 				pipe_ctx->stream_res.hpo_dp_stream_enc,
1763 				&proposed_table);
1764 	}
1765 
1766 	print_mst_streams(link);
1767 
1768 	ASSERT(proposed_table.stream_count > 0);
1769 
1770 	/* update mst stream allocation table hardware state */
1771 	if (link_hwss->ext.update_stream_allocation_table == NULL ||
1772 			link_dp_get_encoding_format(&link->cur_link_settings) == DP_UNKNOWN_ENCODING) {
1773 		DC_LOG_ERROR("Failure: unknown encoding format\n");
1774 		return DC_ERROR_UNEXPECTED;
1775 	}
1776 
1777 	link_hwss->ext.update_stream_allocation_table(link, &pipe_ctx->link_res,
1778 			&link->mst_stream_alloc_table);
1779 
1780 	/* poll for immediate branch device ACT handled */
1781 	ret = dm_helpers_dp_mst_poll_for_allocation_change_trigger(
1782 			stream->ctx,
1783 			stream);
1784 
1785 	if (ret != ACT_LINK_LOST) {
1786 		/* send ALLOCATE_PAYLOAD sideband message with updated pbn */
1787 		dm_helpers_dp_mst_send_payload_allocation(
1788 				stream->ctx,
1789 				stream);
1790 	}
1791 
1792 	/* increase throttled vcp size */
1793 	pbn = get_pbn_from_bw_in_kbps(bw_in_kbps);
1794 	pbn_per_slot = get_pbn_per_slot(stream);
1795 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
1796 
1797 	if (link_hwss->ext.set_throttled_vcp_size)
1798 		link_hwss->ext.set_throttled_vcp_size(pipe_ctx, avg_time_slots_per_mtp);
1799 	if (link_hwss->ext.set_hblank_min_symbol_width)
1800 		link_hwss->ext.set_hblank_min_symbol_width(pipe_ctx,
1801 				&link->cur_link_settings,
1802 				avg_time_slots_per_mtp);
1803 
1804 	return DC_OK;
1805 }
1806 
1807 static void disable_link_dp(struct dc_link *link,
1808 		const struct link_resource *link_res,
1809 		enum signal_type signal)
1810 {
1811 	struct dc_link_settings link_settings = link->cur_link_settings;
1812 
1813 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST &&
1814 			link->mst_stream_alloc_table.stream_count > 0)
1815 		/* disable MST link only when last vc payload is deallocated */
1816 		return;
1817 
1818 	dp_disable_link_phy(link, link_res, signal);
1819 
1820 	if (link->connector_signal == SIGNAL_TYPE_EDP) {
1821 		if (!link->skip_implict_edp_power_control)
1822 			link->dc->hwss.edp_power_control(link, false);
1823 	}
1824 
1825 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST && link->sink_count == 0)
1826 		/* set the sink to SST mode after disabling the link */
1827 		enable_mst_on_sink(link, false);
1828 
1829 	if (link_dp_get_encoding_format(&link_settings) ==
1830 			DP_8b_10b_ENCODING) {
1831 		dp_set_fec_enable(link, link_res, false);
1832 		dp_set_fec_ready(link, link_res, false);
1833 	}
1834 }
1835 
1836 static void disable_link(struct dc_link *link,
1837 		const struct link_resource *link_res,
1838 		enum signal_type signal)
1839 {
1840 	if (dc_is_dp_signal(signal)) {
1841 		disable_link_dp(link, link_res, signal);
1842 	} else if (signal == SIGNAL_TYPE_VIRTUAL) {
1843 		link->dc->hwss.disable_link_output(link, link_res, SIGNAL_TYPE_DISPLAY_PORT);
1844 	} else {
1845 		link->dc->hwss.disable_link_output(link, link_res, signal);
1846 	}
1847 
1848 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
1849 		/* MST disable link only when no stream use the link */
1850 		if (link->mst_stream_alloc_table.stream_count <= 0)
1851 			link->link_status.link_active = false;
1852 	} else {
1853 		link->link_status.link_active = false;
1854 	}
1855 }
1856 
1857 static void enable_link_hdmi(struct pipe_ctx *pipe_ctx)
1858 {
1859 	struct dc_stream_state *stream = pipe_ctx->stream;
1860 	struct dc_link *link = stream->link;
1861 	enum dc_color_depth display_color_depth;
1862 	enum engine_id eng_id;
1863 	struct ext_hdmi_settings settings = {0};
1864 	bool is_over_340mhz = false;
1865 	bool is_vga_mode = (stream->timing.h_addressable == 640)
1866 			&& (stream->timing.v_addressable == 480);
1867 	struct dc *dc = pipe_ctx->stream->ctx->dc;
1868 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
1869 
1870 	if (stream->phy_pix_clk == 0)
1871 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
1872 	if (stream->phy_pix_clk > 340000)
1873 		is_over_340mhz = true;
1874 	if (dc_is_tmds_signal(stream->signal) && stream->phy_pix_clk > 6000000UL) {
1875 		ASSERT(false);
1876 		return;
1877 	}
1878 
1879 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
1880 		unsigned short masked_chip_caps = pipe_ctx->stream->link->chip_caps &
1881 				AMD_EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
1882 		if (masked_chip_caps == AMD_EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
1883 			/* DP159, Retimer settings */
1884 			eng_id = pipe_ctx->stream_res.stream_enc->id;
1885 
1886 			if (get_ext_hdmi_settings(stream->ctx->dc_bios->integrated_info, eng_id, &settings)) {
1887 				write_i2c_retimer_setting(link, is_vga_mode, is_over_340mhz, &settings);
1888 			} else {
1889 				write_i2c_default_retimer_setting(link, is_vga_mode, is_over_340mhz);
1890 			}
1891 		} else if (masked_chip_caps == AMD_EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
1892 			/* PI3EQX1204, Redriver settings */
1893 			write_i2c_redriver_setting(link, is_over_340mhz);
1894 		}
1895 	}
1896 
1897 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
1898 		write_scdc_data(
1899 			stream->link->ddc,
1900 			stream->phy_pix_clk,
1901 			(stream->timing.flags.LTE_340MCSC_SCRAMBLE != 0));
1902 
1903 	if (dc->debug.enable_hdmi_idcc) {
1904 		union hdmi_idcc_source_id source_id;
1905 
1906 		source_id.raw = 0xff;
1907 		write_idcc_data(stream->link->ddc, HDMI_IDCC_SCOPE_WRITE,
1908 				&source_id.raw, 0, 1);
1909 	}
1910 	memset(&stream->link->cur_link_settings, 0,
1911 			sizeof(struct dc_link_settings));
1912 
1913 	display_color_depth = stream->timing.display_color_depth;
1914 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
1915 		display_color_depth = COLOR_DEPTH_888;
1916 
1917 	/* We need to enable stream encoder for TMDS first to apply 1/4 TMDS
1918 	 * character clock in case that beyond 340MHz.
1919 	 */
1920 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal) || dc_is_dvi_signal(pipe_ctx->stream->signal))
1921 		link_hwss->setup_stream_encoder(pipe_ctx);
1922 
1923 	dc->hwss.enable_tmds_link_output(
1924 			link,
1925 			&pipe_ctx->link_res,
1926 			pipe_ctx->stream->signal,
1927 			pipe_ctx->clock_source->id,
1928 			display_color_depth,
1929 			stream->phy_pix_clk);
1930 
1931 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
1932 		read_scdc_data(link->ddc);
1933 }
1934 
1935 static enum dc_status enable_link_hdmi_frl(struct pipe_ctx *pipe_ctx)
1936 {
1937 	enum link_result link_stat = LINK_RESULT_UNKNOWN;
1938 	enum dc_status status = DC_OK;
1939 	struct dc_stream_state *stream = pipe_ctx->stream;
1940 	struct dc *core_dc = pipe_ctx->stream->ctx->dc;
1941 	enum clock_source_id frl_phy_clock_source_id;
1942 	bool frl_poll_start = true;
1943 
1944 	DC_LOGGER_INIT(stream->ctx->logger);
1945 
1946 	if ((!stream->link->link_enc) ||
1947 			(!stream->link->hpo_frl_link_enc) ||
1948 			(!core_dc->res_pool->dccg->funcs->enable_hdmicharclk) ||
1949 			(!(pipe_ctx->stream_res.hpo_frl_stream_enc)))
1950 		return DC_ERROR_UNEXPECTED;
1951 
1952 	/* get link settings for video mode timing */
1953 	hdmi_frl_decide_link_settings(stream, &stream->link->frl_link_settings, &pipe_ctx->dsc_padding_params);
1954 
1955 	switch (stream->link->frl_link_settings.frl_link_rate) {
1956 	case HDMI_FRL_LINK_RATE_3GBPS:
1957 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 166667;
1958 		break;
1959 	case HDMI_FRL_LINK_RATE_6GBPS:
1960 	case HDMI_FRL_LINK_RATE_6GBPS_4LANE:
1961 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 333333;
1962 		break;
1963 	case HDMI_FRL_LINK_RATE_8GBPS:
1964 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 444444;
1965 		break;
1966 	case HDMI_FRL_LINK_RATE_10GBPS:
1967 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 555555;
1968 		break;
1969 	case HDMI_FRL_LINK_RATE_12GBPS:
1970 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 666667;
1971 		break;
1972 	case HDMI_FRL_LINK_RATE_16GBPS:
1973 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 888889;
1974 		break;
1975 	case HDMI_FRL_LINK_RATE_20GBPS:
1976 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 1111111;
1977 		break;
1978 	case HDMI_FRL_LINK_RATE_24GBPS:
1979 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk = 1333333;
1980 		break;
1981 	default:
1982 		break;
1983 	}
1984 
1985 	stream->phy_pix_clk = pipe_ctx->stream_res.pix_clk_params.requested_sym_clk;
1986 
1987 	memset(&stream->link->cur_link_settings, 0,
1988 			sizeof(struct dc_link_settings));
1989 
1990 	/* Find proper clock source in HDMI FRL mode for phy used for DCCG */
1991 	frl_phy_clock_source_id = hdmi_frl_find_matching_phypll(stream->link);
1992 
1993 	FRL_INFO("FRL LINK TRAINING:  LTS:P Start\n");
1994 	/* Setup FRL PHY, enable HDMI character clock and HPO link encoder */
1995 	core_dc->hwss.setup_hdmi_frl_link(stream->link,
1996 			(pipe_ctx->stream_res.hpo_frl_stream_enc->id - ENGINE_ID_HPO_0),
1997 			frl_phy_clock_source_id);
1998 
1999 	link_stat = hdmi_frl_perform_link_training_with_retries(stream->link);
2000 
2001 	if (core_dc->res_pool->dccg->funcs->set_valid_pixel_rate)
2002 		core_dc->res_pool->dccg->funcs->set_valid_pixel_rate(
2003 						core_dc->res_pool->dccg,
2004 						core_dc->clk_mgr->funcs->get_dtb_ref_clk_frequency(core_dc->clk_mgr),
2005 						pipe_ctx->stream_res.tg->inst,
2006 						(stream->timing.pix_clk_100hz / 10));
2007 
2008 	/* Enable FRL packet transmission */
2009 	if (link_stat == LINK_RESULT_SUCCESS) {
2010 		stream->link->hpo_frl_link_enc->funcs->enable_output(
2011 				stream->link->hpo_frl_link_enc);
2012 		if (stream->link->frl_flags.apply_vsdb_rcc_wa)
2013 			stream->link->hpo_frl_link_enc->funcs->apply_vsdb_rcc_wa(stream->link->hpo_frl_link_enc);
2014 		if (frl_poll_start)
2015 			hdmi_frl_poll_start(stream->link->ddc);
2016 
2017 		FRL_INFO("FRL LINK TRAINING:  LTS:P Success\n");
2018 
2019 		/* Set HDMISTREAMCLK source to DTBCLK0 and bypass DTO */
2020 		if (core_dc->res_pool->dccg->funcs->set_hdmistreamclk) {
2021 			core_dc->res_pool->dccg->funcs->set_hdmistreamclk(
2022 					core_dc->res_pool->dccg,
2023 					DTBCLK0,
2024 					pipe_ctx->stream_res.tg->inst);
2025 		}
2026 
2027 		pipe_ctx->stream_res.hpo_frl_stream_enc->funcs->hdmi_frl_enable(
2028 			pipe_ctx->stream_res.hpo_frl_stream_enc,
2029 			pipe_ctx->stream_res.tg->inst);
2030 	} else {
2031 		status = DC_FAIL_HDMI_FRL_LINK_TRAINING;
2032 		stream->link->frl_link_settings.frl_link_rate = 0;
2033 	}
2034 
2035 	return status;
2036 }
2037 
2038 static enum dc_status enable_link_dp(struct dc_state *state,
2039 				     struct pipe_ctx *pipe_ctx)
2040 {
2041 	struct dc_stream_state *stream = pipe_ctx->stream;
2042 	enum dc_status status;
2043 	bool skip_video_pattern;
2044 	struct dc_link *link = stream->link;
2045 	const struct dc_link_settings *link_settings =
2046 			&pipe_ctx->link_config.dp_link_settings;
2047 	bool fec_enable;
2048 	int i;
2049 	bool apply_seamless_boot_optimization = false;
2050 	uint32_t bl_oled_enable_delay = 50; // in ms
2051 	uint32_t post_oui_delay = 30; // 30ms
2052 	/* Reduce link bandwidth between failed link training attempts. */
2053 	bool do_fallback = false;
2054 	int lt_attempts = LINK_TRAINING_ATTEMPTS;
2055 
2056 	// Increase retry count if attempting DP1.x on FIXED_VS link
2057 	if (((link->chip_caps & AMD_EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK) == AMD_EXT_DISPLAY_PATH_CAPS__DP_FIXED_VS_EN) &&
2058 			link_dp_get_encoding_format(link_settings) == DP_8b_10b_ENCODING)
2059 		lt_attempts = 10;
2060 
2061 	// check for seamless boot
2062 	for (i = 0; i < state->stream_count; i++) {
2063 		if (state->streams[i]->apply_seamless_boot_optimization) {
2064 			apply_seamless_boot_optimization = true;
2065 			break;
2066 		}
2067 	}
2068 
2069 	/* Train with fallback when enabling DPIA link. Conventional links are
2070 	 * trained with fallback during sink detection.
2071 	 */
2072 	if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA &&
2073 			!link->dc->config.enable_dpia_pre_training)
2074 		do_fallback = true;
2075 
2076 	/*
2077 	 * Temporary w/a to get DP2.0 link rates to work with SST.
2078 	 * TODO DP2.0 - Workaround: Remove w/a if and when the issue is resolved.
2079 	 */
2080 	if (link_dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING &&
2081 			pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT &&
2082 			link->dc->debug.set_mst_en_for_sst) {
2083 		enable_mst_on_sink(link, true);
2084 	} else if (link->dpcd_caps.is_mst_capable &&
2085 		pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT) {
2086 		/* disable mst on sink */
2087 		enable_mst_on_sink(link, false);
2088 	}
2089 
2090 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP) {
2091 		/*in case it is not on*/
2092 		if (!link->dc->config.edp_no_power_sequencing)
2093 			link->dc->hwss.edp_power_control(link, true);
2094 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
2095 	}
2096 
2097 	if (link_dp_get_encoding_format(link_settings) == DP_128b_132b_ENCODING) {
2098 		/* TODO - DP2.0 HW: calculate 32 symbol clock for HPO encoder */
2099 	} else {
2100 		pipe_ctx->stream_res.pix_clk_params.requested_sym_clk =
2101 				link_settings->link_rate * LINK_RATE_REF_FREQ_IN_KHZ;
2102 		if (state->clk_mgr && !apply_seamless_boot_optimization)
2103 			state->clk_mgr->funcs->update_clocks(state->clk_mgr,
2104 					state, false);
2105 	}
2106 
2107 	// during mode switch we do DP_SET_POWER off then on, and OUI is lost
2108 	dpcd_set_source_specific_data(link);
2109 	if (link->dpcd_sink_ext_caps.raw != 0) {
2110 		post_oui_delay += link->panel_config.pps.extra_post_OUI_ms;
2111 		msleep(post_oui_delay);
2112 	}
2113 
2114 	// similarly, mode switch can cause loss of cable ID
2115 	dpcd_write_cable_id_to_dprx(link);
2116 
2117 	skip_video_pattern = true;
2118 
2119 	if (link_settings->link_rate == LINK_RATE_LOW)
2120 		skip_video_pattern = false;
2121 
2122 	if (stream->sink_patches.oled_optimize_display_on)
2123 		set_default_brightness_aux(link);
2124 
2125 	if (perform_link_training_with_retries(link_settings,
2126 					       skip_video_pattern,
2127 					       lt_attempts,
2128 					       pipe_ctx,
2129 					       pipe_ctx->stream->signal,
2130 					       do_fallback)) {
2131 		status = DC_OK;
2132 	} else {
2133 		status = DC_FAIL_DP_LINK_TRAINING;
2134 	}
2135 
2136 	if (link->preferred_training_settings.fec_enable)
2137 		fec_enable = *link->preferred_training_settings.fec_enable;
2138 	else
2139 		fec_enable = true;
2140 
2141 	if (link_dp_get_encoding_format(link_settings) == DP_8b_10b_ENCODING)
2142 		dp_set_fec_enable(link, &pipe_ctx->link_res, fec_enable);
2143 
2144 	// during mode set we do DP_SET_POWER off then on, aux writes are lost
2145 	if (link->dpcd_sink_ext_caps.bits.oled == 1 ||
2146 		link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1 ||
2147 		link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1) {
2148 		if (!stream->sink_patches.oled_optimize_display_on) {
2149 			set_default_brightness_aux(link);
2150 			if (link->dpcd_sink_ext_caps.bits.oled == 1)
2151 				msleep(bl_oled_enable_delay);
2152 			edp_backlight_enable_aux(link, true);
2153 		} else {
2154 			edp_backlight_enable_aux(link, true);
2155 		}
2156 	}
2157 
2158 	return status;
2159 }
2160 
2161 static enum dc_status enable_link_edp(
2162 		struct dc_state *state,
2163 		struct pipe_ctx *pipe_ctx)
2164 {
2165 	return enable_link_dp(state, pipe_ctx);
2166 }
2167 
2168 static void enable_link_lvds(struct pipe_ctx *pipe_ctx)
2169 {
2170 	struct dc_stream_state *stream = pipe_ctx->stream;
2171 	struct dc_link *link = stream->link;
2172 	struct dc *dc = stream->ctx->dc;
2173 
2174 	if (stream->phy_pix_clk == 0)
2175 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2176 
2177 	memset(&stream->link->cur_link_settings, 0,
2178 			sizeof(struct dc_link_settings));
2179 	dc->hwss.enable_lvds_link_output(
2180 			link,
2181 			&pipe_ctx->link_res,
2182 			pipe_ctx->clock_source->id,
2183 			stream->phy_pix_clk);
2184 
2185 }
2186 
2187 static enum dc_status enable_link_dp_mst(
2188 		struct dc_state *state,
2189 		struct pipe_ctx *pipe_ctx)
2190 {
2191 	struct dc_link *link = pipe_ctx->stream->link;
2192 	unsigned char mstm_cntl = 0;
2193 
2194 	/* sink signal type after MST branch is MST. Multiple MST sinks
2195 	 * share one link. Link DP PHY is enable or training only once.
2196 	 */
2197 	if (link->link_status.link_active)
2198 		return DC_OK;
2199 
2200 	/* clear payload table */
2201 	core_link_read_dpcd(link, DP_MSTM_CTRL, &mstm_cntl, 1);
2202 	if (mstm_cntl & DP_MST_EN)
2203 		dm_helpers_dp_mst_clear_payload_allocation_table(link->ctx, link);
2204 
2205 	/* to make sure the pending down rep can be processed
2206 	 * before enabling the link
2207 	 */
2208 	dm_helpers_dp_mst_poll_pending_down_reply(link->ctx, link);
2209 
2210 	/* set the sink to MST mode before enabling the link */
2211 	enable_mst_on_sink(link, true);
2212 
2213 	return enable_link_dp(state, pipe_ctx);
2214 }
2215 
2216 static enum dc_status enable_link_analog(struct pipe_ctx *pipe_ctx)
2217 {
2218 	struct dc_link *link = pipe_ctx->stream->link;
2219 
2220 	link->dc->hwss.enable_analog_link_output(
2221 		link, pipe_ctx->stream->timing.pix_clk_100hz);
2222 
2223 	return DC_OK;
2224 }
2225 
2226 static enum dc_status enable_link_virtual(struct pipe_ctx *pipe_ctx)
2227 {
2228 	struct dc_link *link = pipe_ctx->stream->link;
2229 
2230 	link->dc->hwss.enable_dp_link_output(link,
2231 			&pipe_ctx->link_res,
2232 			SIGNAL_TYPE_DISPLAY_PORT,
2233 			pipe_ctx->clock_source->id,
2234 			&pipe_ctx->link_config.dp_link_settings);
2235 	return DC_OK;
2236 }
2237 
2238 static enum dc_status enable_link(
2239 		struct dc_state *state,
2240 		struct pipe_ctx *pipe_ctx)
2241 {
2242 	enum dc_status status = DC_ERROR_UNEXPECTED;
2243 	struct dc_stream_state *stream = pipe_ctx->stream;
2244 	struct dc_link *link = NULL;
2245 
2246 	if (stream == NULL)
2247 		return DC_ERROR_UNEXPECTED;
2248 	link = stream->link;
2249 
2250 	/* There's some scenarios where driver is unloaded with display
2251 	 * still enabled. When driver is reloaded, it may cause a display
2252 	 * to not light up if there is a mismatch between old and new
2253 	 * link settings. Need to call disable first before enabling at
2254 	 * new link settings.
2255 	 */
2256 	if (link->link_status.link_active)
2257 		disable_link(link, &pipe_ctx->link_res, pipe_ctx->stream->signal);
2258 
2259 	switch (pipe_ctx->stream->signal) {
2260 	case SIGNAL_TYPE_DISPLAY_PORT:
2261 		status = enable_link_dp(state, pipe_ctx);
2262 		break;
2263 	case SIGNAL_TYPE_EDP:
2264 		status = enable_link_edp(state, pipe_ctx);
2265 		break;
2266 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
2267 		status = enable_link_dp_mst(state, pipe_ctx);
2268 		msleep(200);
2269 		break;
2270 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
2271 	case SIGNAL_TYPE_DVI_DUAL_LINK:
2272 	case SIGNAL_TYPE_HDMI_TYPE_A:
2273 		enable_link_hdmi(pipe_ctx);
2274 		status = DC_OK;
2275 		break;
2276 	case SIGNAL_TYPE_HDMI_FRL:
2277 		if (link->local_sink &&
2278 			link->local_sink->edid_caps.panel_patch.delay_hdmi_link_training &&
2279 			stream->timing.pix_clk_100hz == 6627500) {
2280 			msleep(link->local_sink->edid_caps.panel_patch.delay_hdmi_link_training);
2281 		}
2282 		status = enable_link_hdmi_frl(pipe_ctx);
2283 		break;
2284 	case SIGNAL_TYPE_LVDS:
2285 		enable_link_lvds(pipe_ctx);
2286 		status = DC_OK;
2287 		break;
2288 	case SIGNAL_TYPE_RGB:
2289 		status = enable_link_analog(pipe_ctx);
2290 		break;
2291 	case SIGNAL_TYPE_VIRTUAL:
2292 		status = enable_link_virtual(pipe_ctx);
2293 		break;
2294 	default:
2295 		break;
2296 	}
2297 
2298 	if (status == DC_OK) {
2299 		pipe_ctx->stream->link->link_status.link_active = true;
2300 	}
2301 
2302 	return status;
2303 }
2304 
2305 static bool allocate_usb4_bandwidth_for_stream(struct dc_stream_state *stream, int stream_bw)
2306 {
2307 	ASSERT(stream->sink);
2308 
2309 	struct dc_link *link = stream->sink->link;
2310 	int req_bw = stream_bw;
2311 
2312 	DC_LOGGER_INIT(link->ctx->logger);
2313 
2314 	if (!link->dpia_bw_alloc_config.bw_alloc_enabled)
2315 		return false;
2316 
2317 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2318 		int sink_index = -1;
2319 		unsigned int i = 0;
2320 
2321 		for (i = 0; i < link->sink_count; i++) {
2322 			if (link->remote_sinks[i] == NULL)
2323 				continue;
2324 
2325 			if (stream->sink->sink_id != link->remote_sinks[i]->sink_id) {
2326 				DC_LOG_DEBUG("%s: add remote_sink=%s, request_bw=%d\n", __func__,
2327 					(const char *)(&link->remote_sinks[i]->edid_caps.display_name[0]),
2328 					link->dpia_bw_alloc_config.remote_sink_req_bw[i]);
2329 
2330 				req_bw += link->dpia_bw_alloc_config.remote_sink_req_bw[i];
2331 			} else
2332 				sink_index = i;
2333 		}
2334 
2335 		if (sink_index >= 0)
2336 			link->dpia_bw_alloc_config.remote_sink_req_bw[sink_index] = stream_bw;
2337 		else
2338 			DC_LOG_WARNING("%s: stream sink_id=%u not found in remote_sinks[]\n",
2339 				__func__, stream->sink->sink_id);
2340 
2341 		if (req_bw) {
2342 			link->dpia_bw_alloc_config.dp_overhead = link_dpia_get_dp_overhead(link);
2343 			req_bw += link->dpia_bw_alloc_config.dp_overhead;
2344 		} else
2345 			link->dpia_bw_alloc_config.dp_overhead = 0;
2346 	}
2347 
2348 	link_dp_dpia_allocate_usb4_bandwidth_for_stream(link, req_bw);
2349 
2350 	return true;
2351 }
2352 
2353 static bool allocate_usb4_bandwidth(struct dc_stream_state *stream)
2354 {
2355 	ASSERT(stream->sink);
2356 
2357 	bool ret;
2358 
2359 	int bw = dc_bandwidth_in_kbps_from_timing(&stream->timing,
2360 			dc_link_get_highest_encoding_format(stream->sink->link));
2361 
2362 	ret = allocate_usb4_bandwidth_for_stream(stream, bw);
2363 
2364 	return ret;
2365 }
2366 
2367 static bool deallocate_usb4_bandwidth(struct dc_stream_state *stream)
2368 {
2369 	bool ret;
2370 
2371 	ret = allocate_usb4_bandwidth_for_stream(stream, 0);
2372 
2373 	return ret;
2374 }
2375 
2376 static struct vpg *get_vpg(struct pipe_ctx *pipe_ctx)
2377 {
2378 	if (dc_is_hdmi_frl_signal(pipe_ctx->stream->signal))
2379 		return pipe_ctx->stream_res.hpo_frl_stream_enc->vpg;
2380 	else if (dp_is_128b_132b_signal(pipe_ctx))
2381 		return pipe_ctx->stream_res.hpo_dp_stream_enc->vpg;
2382 	else
2383 		return pipe_ctx->stream_res.stream_enc->vpg;
2384 }
2385 
2386 enum dc_status link_set_dpms_off(struct pipe_ctx *pipe_ctx)
2387 {
2388 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2389 	struct dc_stream_state *stream = pipe_ctx->stream;
2390 	struct dc *dc = stream->ctx->dc;
2391 	struct dc_link *link = stream->link;
2392 	struct vpg *vpg = get_vpg(pipe_ctx);
2393 	enum dp_panel_mode panel_mode_dp = dp_get_panel_mode(link);
2394 
2395 	ASSERT(is_master_pipe_for_link(link, pipe_ctx));
2396 
2397 	if (dc_is_virtual_signal(stream->signal))
2398 		/* No real hardware to program for virtual signals. */
2399 		return DC_DPMS_SKIPPED_HANDSHAKE;
2400 
2401 	if (stream->sink) {
2402 		if (stream->sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
2403 			stream->sink->sink_signal != SIGNAL_TYPE_NONE) {
2404 			DC_LOG_DC("%s pipe_ctx dispname=%s signal=%x link=%d sink_count=%d\n", __func__,
2405 			stream->sink->edid_caps.display_name,
2406 			stream->signal, link->link_index, link->sink_count);
2407 		}
2408 	}
2409 
2410 	link_wait_for_unlocked(link);
2411 
2412 	if (stream->sink && !stream->sink->edid_caps.panel_patch.skip_avmute) {
2413 		if (dc_is_hdmi_signal(stream->signal))
2414 			set_avmute(pipe_ctx, true);
2415 	}
2416 
2417 	dc->hwss.disable_audio_stream(pipe_ctx);
2418 
2419 	update_psp_stream_config(pipe_ctx, true);
2420 	dc->hwss.blank_stream(pipe_ctx);
2421 
2422 	if (pipe_ctx->link_config.dp_tunnel_settings.should_use_dp_bw_allocation)
2423 		deallocate_usb4_bandwidth(stream);
2424 
2425 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
2426 		deallocate_mst_payload(pipe_ctx);
2427 	else if (dc_is_dp_sst_signal(stream->signal) &&
2428 			dp_is_128b_132b_signal(pipe_ctx))
2429 		update_sst_payload(pipe_ctx, false);
2430 
2431 	if (dc_is_hdmi_signal(stream->signal)) {
2432 		struct ext_hdmi_settings settings = {0};
2433 		enum engine_id eng_id = pipe_ctx->stream_res.stream_enc->id;
2434 
2435 		unsigned short masked_chip_caps = link->chip_caps &
2436 				AMD_EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
2437 		//Need to inform that sink is going to use legacy HDMI mode.
2438 		write_scdc_data(
2439 			link->ddc,
2440 			165000,//vbios only handles 165Mhz.
2441 			false);
2442 		if (masked_chip_caps == AMD_EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
2443 			/* DP159, Retimer settings */
2444 			if (get_ext_hdmi_settings(stream->ctx->dc_bios->integrated_info, eng_id, &settings))
2445 				write_i2c_retimer_setting(link, false, false, &settings);
2446 			else
2447 				write_i2c_default_retimer_setting(link, false, false);
2448 		} else if (masked_chip_caps == AMD_EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
2449 			/* PI3EQX1204, Redriver settings */
2450 			write_i2c_redriver_setting(link, false);
2451 		}
2452 	}
2453 
2454 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT &&
2455 			!dp_is_128b_132b_signal(pipe_ctx)) {
2456 
2457 		/* In DP1.x SST mode, our encoder will go to TPS1
2458 		 * when link is on but stream is off.
2459 		 * Disabling link before stream will avoid exposing TPS1 pattern
2460 		 * during the disable sequence as it will confuse some receivers
2461 		 * state machine.
2462 		 * In DP2 or MST mode, our encoder will stay video active
2463 		 */
2464 		disable_link(link, &pipe_ctx->link_res, stream->signal);
2465 		dc->hwss.disable_stream(pipe_ctx);
2466 	} else {
2467 		dc->hwss.disable_stream(pipe_ctx);
2468 		disable_link(link, &pipe_ctx->link_res, stream->signal);
2469 	}
2470 	edp_set_panel_assr(link, pipe_ctx, &panel_mode_dp, false);
2471 
2472 	if (stream->timing.flags.DSC) {
2473 		if (dc_is_dp_signal(stream->signal))
2474 			link_set_dsc_enable(pipe_ctx, false);
2475 		else if (dc_is_hdmi_frl_signal(stream->signal))
2476 			link_set_dsc_on_stream(pipe_ctx, false);
2477 	}
2478 	if (dp_is_128b_132b_signal(pipe_ctx)) {
2479 		if (pipe_ctx->stream_res.tg->funcs->set_out_mux)
2480 			pipe_ctx->stream_res.tg->funcs->set_out_mux(pipe_ctx->stream_res.tg, OUT_MUX_DIO);
2481 	}
2482 
2483 	if (vpg && vpg->funcs->vpg_powerdown)
2484 		vpg->funcs->vpg_powerdown(vpg);
2485 
2486 	/* for psp not exist case */
2487 	if (link->connector_signal == SIGNAL_TYPE_EDP && dc->debug.psp_disabled_wa) {
2488 		/* reset internal save state to default since eDP is  off */
2489 		enum dp_panel_mode panel_mode = dp_get_panel_mode(link);
2490 		/* since current psp not loaded, we need to reset it to default */
2491 		link->panel_mode = panel_mode;
2492 	}
2493 
2494 	return DC_DPMS_SUCCESS;
2495 }
2496 
2497 static enum dc_status link_set_dpms_on_pre_enable_link(
2498 		struct dc_state *state,
2499 		struct pipe_ctx *pipe_ctx
2500 )
2501 {
2502 	// Used conditionally in ifdef'ed diagnostic builds
2503 	(void) state;
2504 
2505 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2506 	struct dc_stream_state *stream = pipe_ctx->stream;
2507 	struct dc *dc = stream->ctx->dc;
2508 	struct dc_link *link = stream->link;
2509 
2510 	enum otg_out_mux_dest otg_out_dest = OUT_MUX_DIO;
2511 	struct vpg *vpg = get_vpg(pipe_ctx);
2512 	const struct link_hwss *link_hwss = get_link_hwss(link, &pipe_ctx->link_res);
2513 	bool apply_edp_fast_boot_optimization =
2514 		stream->apply_edp_fast_boot_optimization;
2515 
2516 	ASSERT(is_master_pipe_for_link(link, pipe_ctx));
2517 
2518 	if (dc_is_virtual_signal(stream->signal))
2519 		/* No real hardware to program for virtual signals. */
2520 		return DC_DPMS_SKIPPED_HANDSHAKE;
2521 
2522 	if (stream->sink) {
2523 		if (stream->sink->sink_signal != SIGNAL_TYPE_VIRTUAL &&
2524 			stream->sink->sink_signal != SIGNAL_TYPE_NONE) {
2525 			DC_LOG_DC("%s pipe_ctx dispname=%s signal=%x link=%d sink_count=%d\n", __func__,
2526 			stream->sink->edid_caps.display_name,
2527 			stream->signal,
2528 			link->link_index,
2529 			link->sink_count);
2530 		}
2531 	}
2532 
2533 	link_wait_for_unlocked(link);
2534 
2535 	if (!dc_is_virtual_signal(stream->signal)
2536 			&& !dc_is_hdmi_frl_signal(stream->signal)
2537 			&& !dp_is_128b_132b_signal(pipe_ctx)) {
2538 		struct link_encoder *link_enc = get_link_encoder(pipe_ctx);
2539 
2540 		if (link_enc)
2541 			link_enc->funcs->setup(
2542 				link_enc,
2543 				stream->signal);
2544 	}
2545 
2546 	link->link_state_valid = true;
2547 
2548 	if (dc_is_hdmi_frl_signal(stream->signal))
2549 		hdmi_frl_decide_link_settings(stream, &link->frl_link_settings, &pipe_ctx->dsc_padding_params);
2550 
2551 	if (pipe_ctx->stream_res.tg->funcs->set_out_mux) {
2552 		if (dp_is_128b_132b_signal(pipe_ctx))
2553 			otg_out_dest = OUT_MUX_HPO_DP;
2554 		else if (dc_is_hdmi_frl_signal(stream->signal))
2555 			otg_out_dest = OUT_MUX_HPO_FRL;
2556 		else
2557 			otg_out_dest = OUT_MUX_DIO;
2558 		pipe_ctx->stream_res.tg->funcs->set_out_mux(pipe_ctx->stream_res.tg, otg_out_dest);
2559 	}
2560 
2561 	link_hwss->setup_stream_attribute(pipe_ctx);
2562 
2563 	stream->apply_edp_fast_boot_optimization = false;
2564 
2565 	// Enable VPG before building infoframe
2566 	if (vpg && vpg->funcs->vpg_poweron)
2567 		vpg->funcs->vpg_poweron(vpg);
2568 
2569 	resource_build_info_frame(pipe_ctx);
2570 	dc->hwss.update_info_frame(pipe_ctx);
2571 
2572 	if (dc_is_dp_signal(stream->signal))
2573 		dp_trace_source_sequence(link, DPCD_SOURCE_SEQ_AFTER_UPDATE_INFO_FRAME);
2574 
2575 	/* Do not touch link on seamless boot optimization. */
2576 	if (stream->apply_seamless_boot_optimization) {
2577 		stream->dpms_off = false;
2578 
2579 		/* Still enable stream features & audio on seamless boot for DP external displays */
2580 		if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT) {
2581 			enable_stream_features(pipe_ctx);
2582 			dc->hwss.enable_audio_stream(pipe_ctx);
2583 		}
2584 
2585 		update_psp_stream_config(pipe_ctx, false);
2586 
2587 		/* Seamless boot: hardware already enabled by BIOS; skip link training. */
2588 		return DC_DPMS_SKIPPED_HANDSHAKE;
2589 	}
2590 
2591 	/* eDP lit up by bios already, no need to enable again. */
2592 	if (stream->signal == SIGNAL_TYPE_EDP &&
2593 				apply_edp_fast_boot_optimization &&
2594 				!stream->timing.flags.DSC &&
2595 				!pipe_ctx->next_odm_pipe) {
2596 		stream->dpms_off = false;
2597 		update_psp_stream_config(pipe_ctx, false);
2598 
2599 		if (link->is_dds) {
2600 			uint32_t post_oui_delay = 30; // 30ms
2601 
2602 			dpcd_set_source_specific_data(link);
2603 			msleep(post_oui_delay);
2604 		}
2605 
2606 		/* eDP already lit by BIOS; skip standard enable steps. */
2607 		return DC_DPMS_SKIPPED_HANDSHAKE;
2608 	}
2609 
2610 	if (stream->dpms_off)
2611 		/*
2612 		 * Stream is configured as DPMS-off; skip link enable.
2613 		 * Hardware will NOT be in a fully enabled state after this early exit.
2614 		 */
2615 		return DC_DPMS_FAILED_INCOMPLETE;
2616 
2617 	/* For Dp tunneling link, a pending HPD means that we have a race condition between processing
2618 	 * current link and processing the pending HPD. If we enable the link now, we may end up with a
2619 	 * link that is not actually connected to a sink. So we skip enabling the link in this case.
2620 	 */
2621 	if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA && link->is_hpd_pending) {
2622 		DC_LOG_DEBUG("%s, Link%d HPD is pending, not enable it.\n", __func__, link->link_index);
2623 		/*
2624 		 * Pending HPD on USB4 DPIA link: skip enable to avoid race condition.
2625 		 * Hardware will NOT be in a fully enabled state after this early exit.
2626 		 */
2627 		return DC_DPMS_FAILED_INCOMPLETE;
2628 	}
2629 
2630 	/* Have to setup DSC before DIG FE and BE are connected (which happens before the
2631 	 * link training). This is to make sure the bandwidth sent to DIG BE won't be
2632 	 * bigger than what the link and/or DIG BE can handle. VBID[6]/CompressedStream_flag
2633 	 * will be automatically set at a later time when the video is enabled
2634 	 * (DP_VID_STREAM_EN = 1).
2635 	 */
2636 	if (stream->timing.flags.DSC) {
2637 		if (dc_is_dp_signal(stream->signal) ||
2638 			dc_is_virtual_signal(stream->signal))
2639 			link_set_dsc_enable(pipe_ctx, true);
2640 	}
2641 
2642 	if (link->replay_settings.config.replay_supported && !dc_is_embedded_signal(link->connector_signal))
2643 		dp_setup_replay(link, stream);
2644 
2645 	return DC_DPMS_SUCCESS;
2646 }
2647 
2648 static enum dc_status link_set_dpms_on_enable_link(
2649 		struct dc_state *state,
2650 		struct pipe_ctx *pipe_ctx
2651 )
2652 {
2653 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2654 	struct dc_stream_state *stream = pipe_ctx->stream;
2655 	struct dc_link *link = stream->link;
2656 	const enum dc_status status = enable_link(state, pipe_ctx);
2657 
2658 	if (status == DC_OK)
2659 		return DC_DPMS_SUCCESS;
2660 
2661 	DC_LOG_WARNING("enabling link %u failed: %d\n", link->link_index, status);
2662 
2663 	/* Abort stream enable *unless* the failure was due to
2664 	 * DP link training - some DP monitors will recover and
2665 	 * show the stream anyway. But MST displays can't proceed
2666 	 * without link training.
2667 	 */
2668 	switch (status) {
2669 	case DC_FAIL_DP_LINK_TRAINING:
2670 	case DC_FAIL_HDMI_FRL_LINK_TRAINING:
2671 		if (stream->signal != SIGNAL_TYPE_DISPLAY_PORT_MST)
2672 			return DC_DPMS_SUCCESS;
2673 		break;
2674 
2675 	default:
2676 		break;
2677 	}
2678 
2679 	if (!link->link_status.link_active)
2680 		disable_link(link, &pipe_ctx->link_res, stream->signal);
2681 
2682 	/*
2683 	 * Link enable failed; do NOT set skip_remaining so that post_enable_link
2684 	 * still runs and leaves hardware in a consistent state.
2685 	 */
2686 	return DC_DPMS_FAILED_HANDSHAKE;
2687 }
2688 
2689 static enum dc_status link_set_dpms_on_post_enable_link(
2690 		struct dc_state *state,
2691 		struct pipe_ctx *pipe_ctx
2692 )
2693 {
2694 	// Used conditionally in ifdef'ed diagnostic builds
2695 	(void) state;
2696 
2697 	struct dc_stream_state *stream = pipe_ctx->stream;
2698 	struct dc_link *link = stream->link;
2699 	struct hw_sequencer_funcs *hwss = &stream->ctx->dc->hwss;
2700 
2701 	if (stream->timing.flags.DSC && dc_is_hdmi_frl_signal(stream->signal))
2702 		//TODO: bring HDMI FRL in line with DP
2703 		link_set_dsc_on_stream(pipe_ctx, true);
2704 
2705 	/* turn off otg test pattern if enable */
2706 	if (pipe_ctx->stream_res.tg->funcs->set_test_pattern)
2707 		pipe_ctx->stream_res.tg->funcs->set_test_pattern(pipe_ctx->stream_res.tg,
2708 				CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
2709 				COLOR_DEPTH_UNDEFINED);
2710 
2711 	/* This second call is needed to reconfigure the DIG
2712 	 * as a workaround for the incorrect value being applied
2713 	 * from transmitter control.
2714 	 */
2715 	if (!(dc_is_virtual_signal(stream->signal) ||
2716 			dc_is_hdmi_frl_signal(stream->signal) ||
2717 			dp_is_128b_132b_signal(pipe_ctx))) {
2718 		struct link_encoder *link_enc = get_link_encoder(pipe_ctx);
2719 
2720 		if (link_enc)
2721 			link_enc->funcs->setup(
2722 					link_enc,
2723 					stream->signal);
2724 	}
2725 
2726 	hwss->enable_stream(pipe_ctx);
2727 
2728 	/* Set DPS PPS SDP (AKA "info frames") */
2729 	if (stream->timing.flags.DSC) {
2730 		if (dc_is_dp_signal(stream->signal) ||
2731 				dc_is_virtual_signal(stream->signal)) {
2732 			dp_set_dsc_on_rx(pipe_ctx, true);
2733 			link_set_dsc_pps_packet(pipe_ctx, true, true);
2734 		}
2735 	}
2736 
2737 	if (dc_is_dp_signal(stream->signal))
2738 		dp_set_hblank_reduction_on_rx(pipe_ctx);
2739 
2740 	if (pipe_ctx->link_config.dp_tunnel_settings.should_use_dp_bw_allocation)
2741 		allocate_usb4_bandwidth(stream);
2742 
2743 	if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
2744 		allocate_mst_payload(pipe_ctx);
2745 	else if (dc_is_dp_sst_signal(stream->signal) &&
2746 			dp_is_128b_132b_signal(pipe_ctx))
2747 		update_sst_payload(pipe_ctx, true);
2748 
2749 	/* Corruption was observed on systems with display mux when stream gets
2750 	 * enabled after the mux switch. Having a small delay between link
2751 	 * training and stream unblank resolves the corruption issue.
2752 	 * This is workaround.
2753 	 */
2754 	if (stream->signal == SIGNAL_TYPE_EDP &&
2755 			link->is_display_mux_present)
2756 		msleep(20);
2757 
2758 	hwss->unblank_stream(pipe_ctx,
2759 		&link->cur_link_settings);
2760 
2761 	if (stream->sink_patches.delay_ignore_msa > 0)
2762 		msleep(stream->sink_patches.delay_ignore_msa);
2763 
2764 	if (dc_is_dp_signal(stream->signal))
2765 		enable_stream_features(pipe_ctx);
2766 	update_psp_stream_config(pipe_ctx, false);
2767 
2768 	hwss->enable_audio_stream(pipe_ctx);
2769 
2770 	if (dc_is_hdmi_signal(stream->signal))
2771 		set_avmute(pipe_ctx, false);
2772 
2773 	return DC_DPMS_SUCCESS;
2774 }
2775 
2776 enum dc_status link_set_dpms_on(
2777 		struct dc_state *state,
2778 		struct pipe_ctx *pipe_ctx
2779 )
2780 {
2781 	enum dc_status result = DC_DPMS_SUCCESS;
2782 
2783 	typedef enum dc_status (*step)(struct dc_state *, struct pipe_ctx *);
2784 	const step steps[] = {
2785 		link_set_dpms_on_pre_enable_link,
2786 		link_set_dpms_on_enable_link,
2787 		link_set_dpms_on_post_enable_link,
2788 	};
2789 
2790 	for (size_t i = 0; i < ARRAY_SIZE(steps); i++) {
2791 		const enum dc_status step_result = steps[i](state, pipe_ctx);
2792 
2793 		switch (step_result) {
2794 		case DC_DPMS_SUCCESS:
2795 		case DC_DPMS_FAILED_HANDSHAKE:
2796 			// Enum is ordered from "best" to "worst" results
2797 			result = max(result, step_result);
2798 			break;
2799 
2800 		case DC_DPMS_SKIPPED_HANDSHAKE:
2801 			return step_result;
2802 
2803 		case DC_DPMS_FAILED_INCOMPLETE:
2804 			ASSERT(false);
2805 			return step_result;
2806 
2807 		default:
2808 			ASSERT(false);
2809 			return step_result;
2810 		}
2811 	}
2812 
2813 	return result;
2814 }
2815