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