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