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