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