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