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