xref: /linux/drivers/gpu/drm/amd/display/dc/link/protocols/link_dp_capability.c (revision 5f2b6c5f6b692c696a232d12c43b8e41c0d393b9)
1 /*
2  * Copyright 2022 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 /* FILE POLICY AND INTENDED USAGE:
27  * This file implements dp specific link capability retrieval sequence. It is
28  * responsible for retrieving, parsing, overriding, deciding capability obtained
29  * from dp link. Link capability consists of encoders, DPRXs, cables, retimers,
30  * usb and all other possible backend capabilities. Other components should
31  * include this header file in order to access link capability. Accessing link
32  * capability by dereferencing dc_link outside dp_link_capability is not a
33  * recommended method as it makes the component dependent on the underlying data
34  * structure used to represent link capability instead of function interfaces.
35  */
36 
37 #include "link_dp_capability.h"
38 #include "link_ddc.h"
39 #include "link_dpcd.h"
40 #include "link_dp_dpia.h"
41 #include "link_dp_phy.h"
42 #include "link_edp_panel_control.h"
43 #include "link_dp_irq_handler.h"
44 #include "link/accessories/link_dp_trace.h"
45 #include "link/link_detection.h"
46 #include "link/link_validation.h"
47 #include "link_dp_training.h"
48 #include "atomfirmware.h"
49 #include "resource.h"
50 #include "link_enc_cfg.h"
51 #include "dc_dmub_srv.h"
52 #include "gpio_service_interface.h"
53 
54 #define DC_TRACE_LEVEL_MESSAGE(...) /* do nothing */
55 
56 #define DC_LOGGER \
57 	link->ctx->logger
58 
59 #ifndef MAX
60 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
61 #endif
62 #ifndef MIN
63 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
64 #endif
65 
66 struct dp_lt_fallback_entry {
67 	enum dc_lane_count lane_count;
68 	enum dc_link_rate link_rate;
69 };
70 
71 static const struct dp_lt_fallback_entry dp_lt_fallbacks[] = {
72 		/* This link training fallback array is ordered by
73 		 * link bandwidth from highest to lowest.
74 		 * DP specs makes it a normative policy to always
75 		 * choose the next highest link bandwidth during
76 		 * link training fallback.
77 		 */
78 		{LANE_COUNT_FOUR, LINK_RATE_UHBR20},
79 		{LANE_COUNT_FOUR, LINK_RATE_UHBR13_5},
80 		{LANE_COUNT_TWO, LINK_RATE_UHBR20},
81 		{LANE_COUNT_FOUR, LINK_RATE_UHBR10},
82 		{LANE_COUNT_TWO, LINK_RATE_UHBR13_5},
83 		{LANE_COUNT_FOUR, LINK_RATE_HIGH3},
84 		{LANE_COUNT_ONE, LINK_RATE_UHBR20},
85 		{LANE_COUNT_TWO, LINK_RATE_UHBR10},
86 		{LANE_COUNT_FOUR, LINK_RATE_HIGH2},
87 		{LANE_COUNT_ONE, LINK_RATE_UHBR13_5},
88 		{LANE_COUNT_TWO, LINK_RATE_HIGH3},
89 		{LANE_COUNT_ONE, LINK_RATE_UHBR10},
90 		{LANE_COUNT_TWO, LINK_RATE_HIGH2},
91 		{LANE_COUNT_FOUR, LINK_RATE_HIGH},
92 		{LANE_COUNT_ONE, LINK_RATE_HIGH3},
93 		{LANE_COUNT_FOUR, LINK_RATE_LOW},
94 		{LANE_COUNT_ONE, LINK_RATE_HIGH2},
95 		{LANE_COUNT_TWO, LINK_RATE_HIGH},
96 		{LANE_COUNT_TWO, LINK_RATE_LOW},
97 		{LANE_COUNT_ONE, LINK_RATE_HIGH},
98 		{LANE_COUNT_ONE, LINK_RATE_LOW},
99 };
100 
101 static const struct dc_link_settings fail_safe_link_settings = {
102 		.lane_count = LANE_COUNT_ONE,
103 		.link_rate = LINK_RATE_LOW,
104 		.link_spread = LINK_SPREAD_DISABLED,
105 };
106 
is_dp_active_dongle(const struct dc_link * link)107 bool is_dp_active_dongle(const struct dc_link *link)
108 {
109 	return (link->dpcd_caps.dongle_type >= DISPLAY_DONGLE_DP_VGA_CONVERTER) &&
110 				(link->dpcd_caps.dongle_type <= DISPLAY_DONGLE_DP_HDMI_CONVERTER);
111 }
112 
is_dp_branch_device(const struct dc_link * link)113 bool is_dp_branch_device(const struct dc_link *link)
114 {
115 	return link->dpcd_caps.is_branch_dev;
116 }
117 
translate_dpcd_max_bpc(enum dpcd_downstream_port_max_bpc bpc)118 static int translate_dpcd_max_bpc(enum dpcd_downstream_port_max_bpc bpc)
119 {
120 	switch (bpc) {
121 	case DOWN_STREAM_MAX_8BPC:
122 		return 8;
123 	case DOWN_STREAM_MAX_10BPC:
124 		return 10;
125 	case DOWN_STREAM_MAX_12BPC:
126 		return 12;
127 	case DOWN_STREAM_MAX_16BPC:
128 		return 16;
129 	default:
130 		break;
131 	}
132 
133 	return -1;
134 }
135 
dp_parse_lttpr_repeater_count(uint8_t lttpr_repeater_count)136 uint8_t dp_parse_lttpr_repeater_count(uint8_t lttpr_repeater_count)
137 {
138 	switch (lttpr_repeater_count) {
139 	case 0x80: // 1 lttpr repeater
140 		return 1;
141 	case 0x40: // 2 lttpr repeaters
142 		return 2;
143 	case 0x20: // 3 lttpr repeaters
144 		return 3;
145 	case 0x10: // 4 lttpr repeaters
146 		return 4;
147 	case 0x08: // 5 lttpr repeaters
148 		return 5;
149 	case 0x04: // 6 lttpr repeaters
150 		return 6;
151 	case 0x02: // 7 lttpr repeaters
152 		return 7;
153 	case 0x01: // 8 lttpr repeaters
154 		return 8;
155 	default:
156 		break;
157 	}
158 	return 0; // invalid value
159 }
160 
dp_get_closest_lttpr_offset(uint8_t lttpr_count)161 uint32_t dp_get_closest_lttpr_offset(uint8_t lttpr_count)
162 {
163 	/* Calculate offset for LTTPR closest to DPTX which is highest in the chain
164 	 * Offset is 0 for single LTTPR cases as base LTTPR DPCD addresses target LTTPR 1
165 	 */
166 	return DP_REPEATER_CONFIGURATION_AND_STATUS_SIZE * (lttpr_count - 1);
167 }
168 
link_bw_kbps_from_raw_frl_link_rate_data(uint8_t bw)169 uint32_t link_bw_kbps_from_raw_frl_link_rate_data(uint8_t bw)
170 {
171 	switch (bw) {
172 	case 0b001:
173 		return 9000000;
174 	case 0b010:
175 		return 18000000;
176 	case 0b011:
177 		return 24000000;
178 	case 0b100:
179 		return 32000000;
180 	case 0b101:
181 		return 40000000;
182 	case 0b110:
183 		return 48000000;
184 	}
185 
186 	return 0;
187 }
188 
linkRateInKHzToLinkRateMultiplier(uint32_t link_rate_in_khz)189 static enum dc_link_rate linkRateInKHzToLinkRateMultiplier(uint32_t link_rate_in_khz)
190 {
191 	enum dc_link_rate link_rate;
192 	// LinkRate is normally stored as a multiplier of 0.27 Gbps per lane. Do the translation.
193 	switch (link_rate_in_khz) {
194 	case 1620000:
195 		link_rate = LINK_RATE_LOW;	// Rate_1 (RBR)	- 1.62 Gbps/Lane
196 		break;
197 	case 2160000:
198 		link_rate = LINK_RATE_RATE_2;	// Rate_2	- 2.16 Gbps/Lane
199 		break;
200 	case 2430000:
201 		link_rate = LINK_RATE_RATE_3;	// Rate_3	- 2.43 Gbps/Lane
202 		break;
203 	case 2700000:
204 		link_rate = LINK_RATE_HIGH;	// Rate_4 (HBR)	- 2.70 Gbps/Lane
205 		break;
206 	case 3240000:
207 		link_rate = LINK_RATE_RBR2;	// Rate_5 (RBR2)- 3.24 Gbps/Lane
208 		break;
209 	case 4320000:
210 		link_rate = LINK_RATE_RATE_6;	// Rate_6	- 4.32 Gbps/Lane
211 		break;
212 	case 5400000:
213 		link_rate = LINK_RATE_HIGH2;	// Rate_7 (HBR2)- 5.40 Gbps/Lane
214 		break;
215 	case 6750000:
216 		link_rate = LINK_RATE_RATE_8;	// Rate_8	- 6.75 Gbps/Lane
217 		break;
218 	case 8100000:
219 		link_rate = LINK_RATE_HIGH3;	// Rate_9 (HBR3)- 8.10 Gbps/Lane
220 		break;
221 	case 10000000:
222 		link_rate = LINK_RATE_UHBR10;	// UHBR10 - 10.0 Gbps/Lane
223 		break;
224 	case 13500000:
225 		link_rate = LINK_RATE_UHBR13_5;	// UHBR13.5 - 13.5 Gbps/Lane
226 		break;
227 	case 20000000:
228 		link_rate = LINK_RATE_UHBR20;	// UHBR20 - 20.0 Gbps/Lane
229 		break;
230 
231 	default:
232 		link_rate = LINK_RATE_UNKNOWN;
233 		break;
234 	}
235 	return link_rate;
236 }
237 
intersect_cable_id(union dp_cable_id * a,union dp_cable_id * b)238 static union dp_cable_id intersect_cable_id(
239 		union dp_cable_id *a, union dp_cable_id *b)
240 {
241 	union dp_cable_id out;
242 
243 	out.bits.UHBR10_20_CAPABILITY = MIN(a->bits.UHBR10_20_CAPABILITY,
244 			b->bits.UHBR10_20_CAPABILITY);
245 	out.bits.UHBR13_5_CAPABILITY = MIN(a->bits.UHBR13_5_CAPABILITY,
246 			b->bits.UHBR13_5_CAPABILITY);
247 	out.bits.CABLE_TYPE = MAX(a->bits.CABLE_TYPE, b->bits.CABLE_TYPE);
248 
249 	return out;
250 }
251 
252 /*
253  * Return PCON's post FRL link training supported BW if its non-zero, otherwise return max_supported_frl_bw.
254  */
intersect_frl_link_bw_support(const uint32_t max_supported_frl_bw_in_kbps,const union hdmi_encoded_link_bw hdmi_encoded_link_bw)255 static uint32_t intersect_frl_link_bw_support(
256 	const uint32_t max_supported_frl_bw_in_kbps,
257 	const union hdmi_encoded_link_bw hdmi_encoded_link_bw)
258 {
259 	uint32_t supported_bw_in_kbps = max_supported_frl_bw_in_kbps;
260 
261 	/* Skip checking FRL_MODE bit, as certain PCON will clear
262 	 * it despite supporting the link BW indicated in the other bits.
263 	 */
264 	if (hdmi_encoded_link_bw.bits.BW_48Gbps)
265 		supported_bw_in_kbps = 48000000;
266 	else if (hdmi_encoded_link_bw.bits.BW_40Gbps)
267 		supported_bw_in_kbps = 40000000;
268 	else if (hdmi_encoded_link_bw.bits.BW_32Gbps)
269 		supported_bw_in_kbps = 32000000;
270 	else if (hdmi_encoded_link_bw.bits.BW_24Gbps)
271 		supported_bw_in_kbps = 24000000;
272 	else if (hdmi_encoded_link_bw.bits.BW_18Gbps)
273 		supported_bw_in_kbps = 18000000;
274 	else if (hdmi_encoded_link_bw.bits.BW_9Gbps)
275 		supported_bw_in_kbps = 9000000;
276 	else if (hdmi_encoded_link_bw.bits.FRL_LINK_TRAINING_FINISHED)
277 		supported_bw_in_kbps = 0; /* This case should only get hit in regulated autonomous mode. */
278 
279 	return supported_bw_in_kbps;
280 }
281 
get_clock_source_id(struct dc_link * link)282 static enum clock_source_id get_clock_source_id(struct dc_link *link)
283 {
284 	enum clock_source_id dp_cs_id = CLOCK_SOURCE_ID_UNDEFINED;
285 	struct clock_source *dp_cs = link->dc->res_pool->dp_clock_source;
286 
287 	if (dp_cs != NULL) {
288 		dp_cs_id = dp_cs->id;
289 	} else {
290 		/*
291 		 * dp clock source is not initialized for some reason.
292 		 * Should not happen, CLOCK_SOURCE_ID_EXTERNAL will be used
293 		 */
294 		ASSERT(dp_cs);
295 	}
296 
297 	return dp_cs_id;
298 }
299 
dp_wa_power_up_0010FA(struct dc_link * link,uint8_t * dpcd_data,int length)300 static void dp_wa_power_up_0010FA(struct dc_link *link, uint8_t *dpcd_data,
301 		int length)
302 {
303 	int retry = 0;
304 
305 	if (!link->dpcd_caps.dpcd_rev.raw) {
306 		do {
307 			dpcd_write_rx_power_ctrl(link, true);
308 			core_link_read_dpcd(link, DP_DPCD_REV,
309 							dpcd_data, length);
310 			link->dpcd_caps.dpcd_rev.raw = dpcd_data[
311 				DP_DPCD_REV -
312 				DP_DPCD_REV];
313 		} while (retry++ < 4 && !link->dpcd_caps.dpcd_rev.raw);
314 	}
315 
316 	if (link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_VGA_CONVERTER) {
317 		switch (link->dpcd_caps.branch_dev_id) {
318 		/* 0010FA active dongles (DP-VGA, DP-DLDVI converters) power down
319 		 * all internal circuits including AUX communication preventing
320 		 * reading DPCD table and EDID (spec violation).
321 		 * Encoder will skip DP RX power down on disable_output to
322 		 * keep receiver powered all the time.*/
323 		case DP_BRANCH_DEVICE_ID_0010FA:
324 		case DP_BRANCH_DEVICE_ID_0080E1:
325 		case DP_BRANCH_DEVICE_ID_00E04C:
326 			link->wa_flags.dp_keep_receiver_powered = true;
327 			break;
328 
329 		/* TODO: May need work around for other dongles. */
330 		default:
331 			link->wa_flags.dp_keep_receiver_powered = false;
332 			break;
333 		}
334 	} else
335 		link->wa_flags.dp_keep_receiver_powered = false;
336 }
337 
dp_is_fec_supported(const struct dc_link * link)338 bool dp_is_fec_supported(const struct dc_link *link)
339 {
340 	/* TODO - use asic cap instead of link_enc->features
341 	 * we no longer know which link enc to use for this link before commit
342 	 */
343 	struct resource_context *res_ctx = &link->dc->current_state->res_ctx;
344 	struct resource_pool *res_pool = link->dc->res_pool;
345 	struct link_encoder *link_enc = get_temp_dio_link_enc(res_ctx, res_pool, link);
346 
347 	if (!link->dc->config.unify_link_enc_assignment)
348 		link_enc = link_enc_cfg_get_link_enc(link);
349 	ASSERT(link_enc);
350 
351 	return (dc_is_dp_signal(link->connector_signal) && link_enc &&
352 			link_enc->features.fec_supported &&
353 			link->dpcd_caps.fec_cap.bits.FEC_CAPABLE);
354 }
355 
dp_should_enable_fec(const struct dc_link * link)356 bool dp_should_enable_fec(const struct dc_link *link)
357 {
358 	bool force_disable = false;
359 
360 	if (link->fec_state == dc_link_fec_enabled)
361 		force_disable = false;
362 	else if (link->connector_signal != SIGNAL_TYPE_DISPLAY_PORT_MST &&
363 			link->local_sink &&
364 			link->local_sink->edid_caps.panel_patch.disable_fec)
365 		force_disable = true;
366 	else if (link->connector_signal == SIGNAL_TYPE_EDP
367 			&& (link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.
368 			 dsc_support.DSC_SUPPORT == false
369 				|| link->panel_config.dsc.disable_dsc_edp
370 				|| !link->dc->caps.edp_dsc_support))
371 		force_disable = true;
372 
373 	return !force_disable && dp_is_fec_supported(link);
374 }
375 
dp_is_128b_132b_signal(struct pipe_ctx * pipe_ctx)376 bool dp_is_128b_132b_signal(struct pipe_ctx *pipe_ctx)
377 {
378 	/* If this assert is hit then we have a link encoder dynamic management issue */
379 	ASSERT(pipe_ctx->stream_res.hpo_dp_stream_enc ? pipe_ctx->link_res.hpo_dp_link_enc != NULL : true);
380 	return (pipe_ctx->stream_res.hpo_dp_stream_enc &&
381 			pipe_ctx->link_res.hpo_dp_link_enc &&
382 			dc_is_dp_signal(pipe_ctx->stream->signal));
383 }
384 
dp_is_lttpr_present(struct dc_link * link)385 bool dp_is_lttpr_present(struct dc_link *link)
386 {
387 	/* Some sink devices report invalid LTTPR revision, so don't validate against that cap */
388 	uint32_t lttpr_count = dp_parse_lttpr_repeater_count(link->dpcd_caps.lttpr_caps.phy_repeater_cnt);
389 	bool is_lttpr_present = (lttpr_count > 0 &&
390 			link->dpcd_caps.lttpr_caps.max_lane_count > 0 &&
391 			link->dpcd_caps.lttpr_caps.max_lane_count <= 4);
392 
393 	if (lttpr_count > 0 && !is_lttpr_present)
394 		DC_LOG_ERROR("LTTPR count is nonzero but invalid lane count reported. Assuming no LTTPR present.\n");
395 
396 	return is_lttpr_present;
397 }
398 
399 /* in DP compliance test, DPR-120 may have
400  * a random value in its MAX_LINK_BW dpcd field.
401  * We map it to the maximum supported link rate that
402  * is smaller than MAX_LINK_BW in this case.
403  */
get_link_rate_from_max_link_bw(uint8_t max_link_bw)404 static enum dc_link_rate get_link_rate_from_max_link_bw(
405 		 uint8_t max_link_bw)
406 {
407 	enum dc_link_rate link_rate;
408 
409 	if (max_link_bw >= LINK_RATE_HIGH3) {
410 		link_rate = LINK_RATE_HIGH3;
411 	} else if (max_link_bw < LINK_RATE_HIGH3
412 			&& max_link_bw >= LINK_RATE_HIGH2) {
413 		link_rate = LINK_RATE_HIGH2;
414 	} else if (max_link_bw < LINK_RATE_HIGH2
415 			&& max_link_bw >= LINK_RATE_HIGH) {
416 		link_rate = LINK_RATE_HIGH;
417 	} else if (max_link_bw < LINK_RATE_HIGH
418 			&& max_link_bw >= LINK_RATE_LOW) {
419 		link_rate = LINK_RATE_LOW;
420 	} else {
421 		link_rate = LINK_RATE_UNKNOWN;
422 	}
423 
424 	return link_rate;
425 }
426 
get_lttpr_max_link_rate(struct dc_link * link)427 static enum dc_link_rate get_lttpr_max_link_rate(struct dc_link *link)
428 {
429 
430 	enum dc_link_rate lttpr_max_link_rate = LINK_RATE_UNKNOWN;
431 
432 	switch (link->dpcd_caps.lttpr_caps.max_link_rate) {
433 	case LINK_RATE_LOW:
434 	case LINK_RATE_HIGH:
435 	case LINK_RATE_HIGH2:
436 	case LINK_RATE_HIGH3:
437 		lttpr_max_link_rate = link->dpcd_caps.lttpr_caps.max_link_rate;
438 		break;
439 	}
440 
441 	if (link->dpcd_caps.lttpr_caps.supported_128b_132b_rates.bits.UHBR20)
442 		lttpr_max_link_rate = LINK_RATE_UHBR20;
443 	else if (link->dpcd_caps.lttpr_caps.supported_128b_132b_rates.bits.UHBR13_5)
444 		lttpr_max_link_rate = LINK_RATE_UHBR13_5;
445 	else if (link->dpcd_caps.lttpr_caps.supported_128b_132b_rates.bits.UHBR10)
446 		lttpr_max_link_rate = LINK_RATE_UHBR10;
447 
448 	return lttpr_max_link_rate;
449 }
450 
get_cable_max_link_rate(struct dc_link * link)451 static enum dc_link_rate get_cable_max_link_rate(struct dc_link *link)
452 {
453 	enum dc_link_rate cable_max_link_rate = LINK_RATE_UNKNOWN;
454 
455 	if (link->dpcd_caps.cable_id.bits.UHBR10_20_CAPABILITY & DP_UHBR20) {
456 		cable_max_link_rate = LINK_RATE_UHBR20;
457 	} else if (link->dpcd_caps.cable_id.bits.UHBR13_5_CAPABILITY) {
458 		cable_max_link_rate = LINK_RATE_UHBR13_5;
459 	} else if (link->dpcd_caps.cable_id.bits.UHBR10_20_CAPABILITY & DP_UHBR10) {
460 		// allow DP40 cables to do UHBR13.5 for passive or unknown cable type
461 		if (link->dpcd_caps.cable_id.bits.CABLE_TYPE < 2) {
462 			cable_max_link_rate = LINK_RATE_UHBR13_5;
463 		} else {
464 			cable_max_link_rate = LINK_RATE_UHBR10;
465 		}
466 	}
467 
468 	return cable_max_link_rate;
469 }
470 
reached_minimum_lane_count(enum dc_lane_count lane_count)471 static inline bool reached_minimum_lane_count(enum dc_lane_count lane_count)
472 {
473 	return lane_count <= LANE_COUNT_ONE;
474 }
475 
reached_minimum_link_rate(enum dc_link_rate link_rate)476 static inline bool reached_minimum_link_rate(enum dc_link_rate link_rate)
477 {
478 	return link_rate <= LINK_RATE_LOW;
479 }
480 
reduce_lane_count(enum dc_lane_count lane_count)481 static enum dc_lane_count reduce_lane_count(enum dc_lane_count lane_count)
482 {
483 	switch (lane_count) {
484 	case LANE_COUNT_FOUR:
485 		return LANE_COUNT_TWO;
486 	case LANE_COUNT_TWO:
487 		return LANE_COUNT_ONE;
488 	case LANE_COUNT_ONE:
489 		return LANE_COUNT_UNKNOWN;
490 	default:
491 		return LANE_COUNT_UNKNOWN;
492 	}
493 }
494 
reduce_link_rate(const struct dc_link * link,enum dc_link_rate link_rate)495 static enum dc_link_rate reduce_link_rate(const struct dc_link *link, enum dc_link_rate link_rate)
496 {
497 	// NEEDSWORK: provide some details about why this function never returns some of the
498 	// obscure link rates such as 4.32 Gbps or 3.24 Gbps and if such behavior is intended.
499 	//
500 
501 	switch (link_rate) {
502 	case LINK_RATE_UHBR20:
503 		return LINK_RATE_UHBR13_5;
504 	case LINK_RATE_UHBR13_5:
505 		return LINK_RATE_UHBR10;
506 	case LINK_RATE_UHBR10:
507 		return LINK_RATE_HIGH3;
508 	case LINK_RATE_HIGH3:
509 		if (link->connector_signal == SIGNAL_TYPE_EDP && link->dc->debug.support_eDP1_5)
510 			return LINK_RATE_RATE_8;
511 		return LINK_RATE_HIGH2;
512 	case LINK_RATE_RATE_8:
513 		return LINK_RATE_HIGH2;
514 	case LINK_RATE_HIGH2:
515 		return LINK_RATE_HIGH;
516 	case LINK_RATE_RATE_6:
517 	case LINK_RATE_RBR2:
518 		return LINK_RATE_HIGH;
519 	case LINK_RATE_HIGH:
520 		return LINK_RATE_LOW;
521 	case LINK_RATE_RATE_3:
522 	case LINK_RATE_RATE_2:
523 		return LINK_RATE_LOW;
524 	case LINK_RATE_LOW:
525 	default:
526 		return LINK_RATE_UNKNOWN;
527 	}
528 }
529 
increase_lane_count(enum dc_lane_count lane_count)530 static enum dc_lane_count increase_lane_count(enum dc_lane_count lane_count)
531 {
532 	switch (lane_count) {
533 	case LANE_COUNT_ONE:
534 		return LANE_COUNT_TWO;
535 	case LANE_COUNT_TWO:
536 		return LANE_COUNT_FOUR;
537 	default:
538 		return LANE_COUNT_UNKNOWN;
539 	}
540 }
541 
increase_link_rate(struct dc_link * link,enum dc_link_rate link_rate)542 static enum dc_link_rate increase_link_rate(struct dc_link *link,
543 		enum dc_link_rate link_rate)
544 {
545 	switch (link_rate) {
546 	case LINK_RATE_LOW:
547 		return LINK_RATE_HIGH;
548 	case LINK_RATE_HIGH:
549 		return LINK_RATE_HIGH2;
550 	case LINK_RATE_HIGH2:
551 		return LINK_RATE_HIGH3;
552 	case LINK_RATE_HIGH3:
553 		return LINK_RATE_UHBR10;
554 	case LINK_RATE_UHBR10:
555 		/* upto DP2.x specs UHBR13.5 is the only link rate that could be
556 		 * not supported by DPRX when higher link rate is supported.
557 		 * so we treat it as a special case for code simplicity. When we
558 		 * have new specs with more link rates like this, we should
559 		 * consider a more generic solution to handle discrete link
560 		 * rate capabilities.
561 		 */
562 		return link->dpcd_caps.dp_128b_132b_supported_link_rates.bits.UHBR13_5 ?
563 				LINK_RATE_UHBR13_5 : LINK_RATE_UHBR20;
564 	case LINK_RATE_UHBR13_5:
565 		return LINK_RATE_UHBR20;
566 	default:
567 		return LINK_RATE_UNKNOWN;
568 	}
569 }
570 
increase_edp_link_rate(struct dc_link * link,struct dc_link_settings * current_link_setting)571 static void increase_edp_link_rate(struct dc_link *link,
572 		struct dc_link_settings *current_link_setting)
573 {
574 	if (current_link_setting->use_link_rate_set) {
575 		if (current_link_setting->link_rate_set < link->dpcd_caps.edp_supported_link_rates_count) {
576 			current_link_setting->link_rate_set++;
577 			current_link_setting->link_rate =
578 				link->dpcd_caps.edp_supported_link_rates[current_link_setting->link_rate_set];
579 		} else {
580 			current_link_setting->use_link_rate_set = false;
581 			current_link_setting->link_rate = LINK_RATE_UHBR10;
582 		}
583 	} else {
584 		current_link_setting->link_rate = increase_link_rate(link, current_link_setting->link_rate);
585 	}
586 }
587 
decide_fallback_link_setting_max_bw_policy(struct dc_link * link,const struct dc_link_settings * max,struct dc_link_settings * cur,enum link_training_result training_result)588 static bool decide_fallback_link_setting_max_bw_policy(
589 		struct dc_link *link,
590 		const struct dc_link_settings *max,
591 		struct dc_link_settings *cur,
592 		enum link_training_result training_result)
593 {
594 	uint32_t cur_idx = 0, next_idx;
595 	bool found = false;
596 
597 	if (training_result == LINK_TRAINING_ABORT)
598 		return false;
599 
600 	while (cur_idx < ARRAY_SIZE(dp_lt_fallbacks))
601 		/* find current index */
602 		if (dp_lt_fallbacks[cur_idx].lane_count == cur->lane_count &&
603 				dp_lt_fallbacks[cur_idx].link_rate == cur->link_rate)
604 			break;
605 		else
606 			cur_idx++;
607 
608 	next_idx = cur_idx + 1;
609 
610 	while (next_idx < ARRAY_SIZE(dp_lt_fallbacks))
611 		/* find next index */
612 		if (dp_lt_fallbacks[next_idx].lane_count > max->lane_count ||
613 				dp_lt_fallbacks[next_idx].link_rate > max->link_rate)
614 			next_idx++;
615 		else if (dp_lt_fallbacks[next_idx].link_rate == LINK_RATE_UHBR13_5 &&
616 				link->dpcd_caps.dp_128b_132b_supported_link_rates.bits.UHBR13_5 == 0)
617 			/* upto DP2.x specs UHBR13.5 is the only link rate that
618 			 * could be not supported by DPRX when higher link rate
619 			 * is supported. so we treat it as a special case for
620 			 * code simplicity. When we have new specs with more
621 			 * link rates like this, we should consider a more
622 			 * generic solution to handle discrete link rate
623 			 * capabilities.
624 			 */
625 			next_idx++;
626 		else
627 			break;
628 
629 	if (next_idx < ARRAY_SIZE(dp_lt_fallbacks)) {
630 		cur->lane_count = dp_lt_fallbacks[next_idx].lane_count;
631 		cur->link_rate = dp_lt_fallbacks[next_idx].link_rate;
632 		found = true;
633 	}
634 
635 	return found;
636 }
637 
638 /*
639  * function: set link rate and lane count fallback based
640  * on current link setting and last link training result
641  * return value:
642  *			true - link setting could be set
643  *			false - has reached minimum setting
644  *					and no further fallback could be done
645  */
decide_fallback_link_setting(struct dc_link * link,struct dc_link_settings * max,struct dc_link_settings * cur,enum link_training_result training_result)646 bool decide_fallback_link_setting(
647 		struct dc_link *link,
648 		struct dc_link_settings *max,
649 		struct dc_link_settings *cur,
650 		enum link_training_result training_result)
651 {
652 	if (link_dp_get_encoding_format(max) == DP_128b_132b_ENCODING ||
653 			link->dc->debug.force_dp2_lt_fallback_method)
654 		return decide_fallback_link_setting_max_bw_policy(link, max,
655 				cur, training_result);
656 
657 	switch (training_result) {
658 	case LINK_TRAINING_CR_FAIL_LANE0:
659 	case LINK_TRAINING_CR_FAIL_LANE1:
660 	case LINK_TRAINING_CR_FAIL_LANE23:
661 	case LINK_TRAINING_LQA_FAIL:
662 	{
663 		if (!reached_minimum_link_rate(cur->link_rate)) {
664 			cur->link_rate = reduce_link_rate(link, cur->link_rate);
665 		} else if (!reached_minimum_lane_count(cur->lane_count)) {
666 			cur->link_rate = max->link_rate;
667 			if (training_result == LINK_TRAINING_CR_FAIL_LANE0)
668 				return false;
669 			else if (training_result == LINK_TRAINING_CR_FAIL_LANE1)
670 				cur->lane_count = LANE_COUNT_ONE;
671 			else if (training_result == LINK_TRAINING_CR_FAIL_LANE23)
672 				cur->lane_count = LANE_COUNT_TWO;
673 			else
674 				cur->lane_count = reduce_lane_count(cur->lane_count);
675 		} else {
676 			return false;
677 		}
678 		break;
679 	}
680 	case LINK_TRAINING_EQ_FAIL_EQ:
681 	case LINK_TRAINING_EQ_FAIL_CR_PARTIAL:
682 	{
683 		if (!reached_minimum_lane_count(cur->lane_count)) {
684 			cur->lane_count = reduce_lane_count(cur->lane_count);
685 		} else if (!reached_minimum_link_rate(cur->link_rate)) {
686 			cur->link_rate = reduce_link_rate(link, cur->link_rate);
687 			/* Reduce max link rate to avoid potential infinite loop.
688 			 * Needed so that any subsequent CR_FAIL fallback can't
689 			 * re-set the link rate higher than the link rate from
690 			 * the latest EQ_FAIL fallback.
691 			 */
692 			max->link_rate = cur->link_rate;
693 			cur->lane_count = max->lane_count;
694 		} else {
695 			return false;
696 		}
697 		break;
698 	}
699 	case LINK_TRAINING_EQ_FAIL_CR:
700 	{
701 		if (!reached_minimum_link_rate(cur->link_rate)) {
702 			cur->link_rate = reduce_link_rate(link, cur->link_rate);
703 			/* Reduce max link rate to avoid potential infinite loop.
704 			 * Needed so that any subsequent CR_FAIL fallback can't
705 			 * re-set the link rate higher than the link rate from
706 			 * the latest EQ_FAIL fallback.
707 			 */
708 			max->link_rate = cur->link_rate;
709 			cur->lane_count = max->lane_count;
710 		} else {
711 			return false;
712 		}
713 		break;
714 	}
715 	default:
716 		return false;
717 	}
718 	return true;
719 }
decide_dp_link_settings(struct dc_link * link,struct dc_link_settings * link_setting,uint32_t req_bw)720 static bool decide_dp_link_settings(struct dc_link *link, struct dc_link_settings *link_setting, uint32_t req_bw)
721 {
722 	struct dc_link_settings initial_link_setting = {
723 		LANE_COUNT_ONE, LINK_RATE_LOW, LINK_SPREAD_DISABLED, false, 0};
724 	struct dc_link_settings current_link_setting =
725 			initial_link_setting;
726 	uint32_t link_bw;
727 
728 	if (req_bw > dp_link_bandwidth_kbps(link, &link->verified_link_cap))
729 		return false;
730 
731 	/* search for the minimum link setting that:
732 	 * 1. is supported according to the link training result
733 	 * 2. could support the b/w requested by the timing
734 	 */
735 	while (current_link_setting.link_rate <=
736 			link->verified_link_cap.link_rate) {
737 		link_bw = dp_link_bandwidth_kbps(
738 				link,
739 				&current_link_setting);
740 		if (req_bw <= link_bw) {
741 			*link_setting = current_link_setting;
742 			return true;
743 		}
744 
745 		if (current_link_setting.lane_count <
746 				link->verified_link_cap.lane_count) {
747 			current_link_setting.lane_count =
748 					increase_lane_count(
749 							current_link_setting.lane_count);
750 		} else {
751 			current_link_setting.link_rate =
752 					increase_link_rate(link,
753 							current_link_setting.link_rate);
754 			current_link_setting.lane_count =
755 					initial_link_setting.lane_count;
756 		}
757 	}
758 
759 	return false;
760 }
761 
edp_decide_link_settings(struct dc_link * link,struct dc_link_settings * link_setting,uint32_t req_bw)762 bool edp_decide_link_settings(struct dc_link *link,
763 		struct dc_link_settings *link_setting, uint32_t req_bw)
764 {
765 	struct dc_link_settings initial_link_setting;
766 	struct dc_link_settings current_link_setting;
767 	uint32_t link_bw;
768 
769 	/*
770 	 * edp_supported_link_rates_count is only valid for eDP v1.4 or higher.
771 	 * Per VESA eDP spec, "The DPCD revision for eDP v1.4 is 13h"
772 	 */
773 	if (!edp_is_ilr_optimization_enabled(link)) {
774 		*link_setting = link->verified_link_cap;
775 		return true;
776 	}
777 
778 	memset(&initial_link_setting, 0, sizeof(initial_link_setting));
779 	initial_link_setting.lane_count = LANE_COUNT_ONE;
780 	initial_link_setting.link_rate = link->dpcd_caps.edp_supported_link_rates[0];
781 	initial_link_setting.link_spread = LINK_SPREAD_DISABLED;
782 	initial_link_setting.use_link_rate_set = true;
783 	initial_link_setting.link_rate_set = 0;
784 	current_link_setting = initial_link_setting;
785 
786 	/* search for the minimum link setting that:
787 	 * 1. is supported according to the link training result
788 	 * 2. could support the b/w requested by the timing
789 	 */
790 	while (current_link_setting.link_rate <=
791 			link->verified_link_cap.link_rate) {
792 		link_bw = dp_link_bandwidth_kbps(
793 				link,
794 				&current_link_setting);
795 		if (req_bw <= link_bw) {
796 			*link_setting = current_link_setting;
797 			return true;
798 		}
799 
800 		if (current_link_setting.lane_count <
801 				link->verified_link_cap.lane_count) {
802 			current_link_setting.lane_count =
803 					increase_lane_count(
804 							current_link_setting.lane_count);
805 		} else {
806 			increase_edp_link_rate(link, &current_link_setting);
807 		}
808 	}
809 	return false;
810 }
811 
decide_edp_link_settings_with_dsc(struct dc_link * link,struct dc_link_settings * link_setting,uint32_t req_bw,enum dc_link_rate max_link_rate)812 bool decide_edp_link_settings_with_dsc(struct dc_link *link,
813 		struct dc_link_settings *link_setting,
814 		uint32_t req_bw,
815 		enum dc_link_rate max_link_rate)
816 {
817 	struct dc_link_settings initial_link_setting;
818 	struct dc_link_settings current_link_setting;
819 	uint32_t link_bw;
820 
821 	unsigned int policy = 0;
822 
823 	policy = link->panel_config.dsc.force_dsc_edp_policy;
824 	if (max_link_rate == LINK_RATE_UNKNOWN)
825 		max_link_rate = link->verified_link_cap.link_rate;
826 	/*
827 	 * edp_supported_link_rates_count is only valid for eDP v1.4 or higher.
828 	 * Per VESA eDP spec, "The DPCD revision for eDP v1.4 is 13h"
829 	 */
830 	if (!edp_is_ilr_optimization_enabled(link)) {
831 		/* for DSC enabled case, we search for minimum lane count */
832 		memset(&initial_link_setting, 0, sizeof(initial_link_setting));
833 		initial_link_setting.lane_count = LANE_COUNT_ONE;
834 		initial_link_setting.link_rate = LINK_RATE_LOW;
835 		initial_link_setting.link_spread = LINK_SPREAD_DISABLED;
836 		initial_link_setting.use_link_rate_set = false;
837 		initial_link_setting.link_rate_set = 0;
838 		current_link_setting = initial_link_setting;
839 		if (req_bw > dp_link_bandwidth_kbps(link, &link->verified_link_cap))
840 			return false;
841 
842 		/* search for the minimum link setting that:
843 		 * 1. is supported according to the link training result
844 		 * 2. could support the b/w requested by the timing
845 		 */
846 		while (current_link_setting.link_rate <=
847 				max_link_rate) {
848 			link_bw = dp_link_bandwidth_kbps(
849 					link,
850 					&current_link_setting);
851 			if (req_bw <= link_bw) {
852 				*link_setting = current_link_setting;
853 				return true;
854 			}
855 			if (policy) {
856 				/* minimize lane */
857 				if (current_link_setting.link_rate < max_link_rate) {
858 					increase_edp_link_rate(link, &current_link_setting);
859 				} else {
860 					if (current_link_setting.lane_count <
861 									link->verified_link_cap.lane_count) {
862 						current_link_setting.lane_count =
863 								increase_lane_count(
864 										current_link_setting.lane_count);
865 						current_link_setting.link_rate = initial_link_setting.link_rate;
866 					} else
867 						break;
868 				}
869 			} else {
870 				/* minimize link rate */
871 				if (current_link_setting.lane_count <
872 						link->verified_link_cap.lane_count) {
873 					current_link_setting.lane_count =
874 							increase_lane_count(
875 									current_link_setting.lane_count);
876 				} else {
877 					increase_edp_link_rate(link, &current_link_setting);
878 					current_link_setting.lane_count =
879 							initial_link_setting.lane_count;
880 				}
881 			}
882 		}
883 		return false;
884 	}
885 
886 	/* if optimize edp link is supported */
887 	memset(&initial_link_setting, 0, sizeof(initial_link_setting));
888 	initial_link_setting.lane_count = LANE_COUNT_ONE;
889 	initial_link_setting.link_rate = link->dpcd_caps.edp_supported_link_rates[0];
890 	initial_link_setting.link_spread = LINK_SPREAD_DISABLED;
891 	initial_link_setting.use_link_rate_set = true;
892 	initial_link_setting.link_rate_set = 0;
893 	current_link_setting = initial_link_setting;
894 
895 	/* search for the minimum link setting that:
896 	 * 1. is supported according to the link training result
897 	 * 2. could support the b/w requested by the timing
898 	 */
899 	while (current_link_setting.link_rate <=
900 			max_link_rate) {
901 		link_bw = dp_link_bandwidth_kbps(
902 				link,
903 				&current_link_setting);
904 		if (req_bw <= link_bw) {
905 			*link_setting = current_link_setting;
906 			return true;
907 		}
908 		if (policy) {
909 			/* minimize lane */
910 			if (current_link_setting.link_rate < max_link_rate) {
911 				increase_edp_link_rate(link, &current_link_setting);
912 			} else {
913 				if (current_link_setting.lane_count < link->verified_link_cap.lane_count) {
914 					current_link_setting.lane_count =
915 							increase_lane_count(
916 									current_link_setting.lane_count);
917 					current_link_setting.link_rate_set = initial_link_setting.link_rate_set;
918 					current_link_setting.use_link_rate_set = initial_link_setting.use_link_rate_set;
919 					current_link_setting.link_rate =
920 						link->dpcd_caps.edp_supported_link_rates[current_link_setting.link_rate_set];
921 				} else
922 					break;
923 			}
924 		} else {
925 			/* minimize link rate */
926 			if (current_link_setting.lane_count <
927 					link->verified_link_cap.lane_count) {
928 				current_link_setting.lane_count =
929 						increase_lane_count(
930 								current_link_setting.lane_count);
931 			} else {
932 				increase_edp_link_rate(link, &current_link_setting);
933 				if (current_link_setting.link_rate == LINK_RATE_UNKNOWN)
934 					break;
935 			}
936 		}
937 	}
938 	return false;
939 }
940 
decide_mst_link_settings(const struct dc_link * link,struct dc_link_settings * link_setting)941 static bool decide_mst_link_settings(const struct dc_link *link, struct dc_link_settings *link_setting)
942 {
943 	*link_setting = link->verified_link_cap;
944 	return true;
945 }
946 
link_decide_link_settings(struct dc_stream_state * stream,struct dc_link_settings * link_setting)947 bool link_decide_link_settings(struct dc_stream_state *stream,
948 	struct dc_link_settings *link_setting)
949 {
950 	struct dc_link *link = stream->link;
951 	uint32_t req_bw = dc_bandwidth_in_kbps_from_timing(&stream->timing, dc_link_get_highest_encoding_format(link));
952 
953 	memset(link_setting, 0, sizeof(*link_setting));
954 
955 	if (dc_is_dp_signal(stream->signal)  &&
956 			link->preferred_link_setting.lane_count != LANE_COUNT_UNKNOWN &&
957 			link->preferred_link_setting.link_rate != LINK_RATE_UNKNOWN) {
958 		/* if preferred is specified through AMDDP, use it, if it's enough
959 		 * to drive the mode
960 		 */
961 		*link_setting = link->preferred_link_setting;
962 	} else if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
963 		/* MST doesn't perform link training for now
964 		 * TODO: add MST specific link training routine
965 		 */
966 		decide_mst_link_settings(link, link_setting);
967 	} else if (stream->signal == SIGNAL_TYPE_VIRTUAL) {
968 		link_setting->lane_count = LANE_COUNT_FOUR;
969 		link_setting->link_rate = LINK_RATE_HIGH3;
970 	} else if (link->connector_signal == SIGNAL_TYPE_EDP) {
971 		/* enable edp link optimization for DSC eDP case */
972 		if (stream->timing.flags.DSC) {
973 			enum dc_link_rate max_link_rate = LINK_RATE_UNKNOWN;
974 
975 			if (link->panel_config.dsc.force_dsc_edp_policy) {
976 				/* calculate link max link rate cap*/
977 				struct dc_link_settings tmp_link_setting;
978 				struct dc_crtc_timing tmp_timing = stream->timing;
979 				uint32_t orig_req_bw;
980 
981 				tmp_link_setting.link_rate = LINK_RATE_UNKNOWN;
982 				tmp_timing.flags.DSC = 0;
983 				orig_req_bw = dc_bandwidth_in_kbps_from_timing(&tmp_timing,
984 						dc_link_get_highest_encoding_format(link));
985 				edp_decide_link_settings(link, &tmp_link_setting, orig_req_bw);
986 				max_link_rate = tmp_link_setting.link_rate;
987 			}
988 			decide_edp_link_settings_with_dsc(link, link_setting, req_bw, max_link_rate);
989 		} else {
990 			edp_decide_link_settings(link, link_setting, req_bw);
991 		}
992 	} else {
993 		decide_dp_link_settings(link, link_setting, req_bw);
994 	}
995 
996 	return link_setting->lane_count != LANE_COUNT_UNKNOWN &&
997 			link_setting->link_rate != LINK_RATE_UNKNOWN;
998 }
999 
link_dp_get_encoding_format(const struct dc_link_settings * link_settings)1000 enum dp_link_encoding link_dp_get_encoding_format(const struct dc_link_settings *link_settings)
1001 {
1002 	if ((link_settings->link_rate >= LINK_RATE_LOW) &&
1003 			(link_settings->link_rate <= LINK_RATE_HIGH3))
1004 		return DP_8b_10b_ENCODING;
1005 	else if ((link_settings->link_rate >= LINK_RATE_UHBR10) &&
1006 			(link_settings->link_rate <= LINK_RATE_UHBR20))
1007 		return DP_128b_132b_ENCODING;
1008 	return DP_UNKNOWN_ENCODING;
1009 }
1010 
mst_decide_link_encoding_format(const struct dc_link * link)1011 enum dp_link_encoding mst_decide_link_encoding_format(const struct dc_link *link)
1012 {
1013 	struct dc_link_settings link_settings = {0};
1014 
1015 	if (!dc_is_dp_signal(link->connector_signal))
1016 		return DP_UNKNOWN_ENCODING;
1017 
1018 	if (link->preferred_link_setting.lane_count !=
1019 			LANE_COUNT_UNKNOWN &&
1020 			link->preferred_link_setting.link_rate !=
1021 					LINK_RATE_UNKNOWN) {
1022 		link_settings = link->preferred_link_setting;
1023 	} else {
1024 		decide_mst_link_settings(link, &link_settings);
1025 	}
1026 
1027 	return link_dp_get_encoding_format(&link_settings);
1028 }
1029 
read_dp_device_vendor_id(struct dc_link * link)1030 static void read_dp_device_vendor_id(struct dc_link *link)
1031 {
1032 	struct dp_device_vendor_id dp_id = {0};
1033 
1034 	/* read IEEE branch device id */
1035 	core_link_read_dpcd(
1036 		link,
1037 		DP_BRANCH_OUI,
1038 		(uint8_t *)&dp_id,
1039 		sizeof(dp_id));
1040 
1041 	link->dpcd_caps.branch_dev_id =
1042 		(dp_id.ieee_oui[0] << 16) +
1043 		(dp_id.ieee_oui[1] << 8) +
1044 		dp_id.ieee_oui[2];
1045 
1046 	memmove(
1047 		link->dpcd_caps.branch_dev_name,
1048 		dp_id.ieee_device_id,
1049 		sizeof(dp_id.ieee_device_id));
1050 }
1051 
wake_up_aux_channel(struct dc_link * link)1052 static enum dc_status wake_up_aux_channel(struct dc_link *link)
1053 {
1054 	enum dc_status status = DC_ERROR_UNEXPECTED;
1055 	uint32_t aux_channel_retry_cnt = 0;
1056 	uint8_t dpcd_power_state = '\0';
1057 
1058 	while (status != DC_OK && aux_channel_retry_cnt < 10) {
1059 		status = core_link_read_dpcd(link, DP_SET_POWER,
1060 				&dpcd_power_state, sizeof(dpcd_power_state));
1061 
1062 		/* Delay 1 ms if AUX CH is in power down state. Based on spec
1063 		 * section 2.3.1.2, if AUX CH may be powered down due to
1064 		 * write to DPCD 600h = 2. Sink AUX CH is monitoring differential
1065 		 * signal and may need up to 1 ms before being able to reply.
1066 		 */
1067 		if (status != DC_OK || dpcd_power_state == DP_SET_POWER_D3) {
1068 			fsleep(1000);
1069 			aux_channel_retry_cnt++;
1070 		}
1071 	}
1072 
1073 	if (status != DC_OK) {
1074 		dpcd_power_state = DP_SET_POWER_D0;
1075 		status = core_link_write_dpcd(
1076 				link,
1077 				DP_SET_POWER,
1078 				&dpcd_power_state,
1079 				sizeof(dpcd_power_state));
1080 
1081 		dpcd_power_state = DP_SET_POWER_D3;
1082 		status = core_link_write_dpcd(
1083 				link,
1084 				DP_SET_POWER,
1085 				&dpcd_power_state,
1086 				sizeof(dpcd_power_state));
1087 		DC_LOG_DC("%s: Failed to power up sink\n", __func__);
1088 		return DC_ERROR_UNEXPECTED;
1089 	}
1090 
1091 	return DC_OK;
1092 }
1093 
read_and_intersect_post_frl_lt_status(struct dc_link * link)1094 static void read_and_intersect_post_frl_lt_status(
1095 	struct dc_link *link)
1096 {
1097 	union autonomous_mode_and_frl_link_status autonomous_mode_caps = {0};
1098 	union hdmi_tx_link_status hdmi_tx_link_status = {0};
1099 	union hdmi_encoded_link_bw hdmi_encoded_link_bw = {0};
1100 
1101 	/* Check if dongle supports regulated autonomous mode. */
1102 	core_link_read_dpcd(link, DP_REGULATED_AUTONOMOUS_MODE_SUPPORTED_AND_HDMI_LINK_TRAINING_STATUS,
1103 		&autonomous_mode_caps.raw, sizeof(autonomous_mode_caps));
1104 
1105 	link->dpcd_caps.dongle_caps.dp_hdmi_regulated_autonomous_mode_support =
1106 			autonomous_mode_caps.bits.REGULATED_AUTONOMOUS_MODE_SUPPORTED;
1107 
1108 	if (link->dpcd_caps.dongle_caps.dp_hdmi_regulated_autonomous_mode_support) {
1109 		DC_LOG_DC("%s: PCON supports regulated autonomous mode.\n", __func__);
1110 
1111 		core_link_read_dpcd(link, DP_PCON_HDMI_TX_LINK_STATUS,
1112 				&hdmi_tx_link_status.raw, sizeof(hdmi_tx_link_status));
1113 	}
1114 
1115 	// Intersect reported max link bw support with the supported link rate post FRL link training
1116 	if (core_link_read_dpcd(link, DP_PCON_HDMI_POST_FRL_STATUS,
1117 			&hdmi_encoded_link_bw.raw, sizeof(hdmi_encoded_link_bw)) == DC_OK) {
1118 
1119 		if (link->dpcd_caps.dongle_caps.dp_hdmi_regulated_autonomous_mode_support &&
1120 				(!hdmi_tx_link_status.bits.HDMI_TX_READY_STATUS ||
1121 						!hdmi_encoded_link_bw.bits.FRL_LINK_TRAINING_FINISHED)) {
1122 			DC_LOG_WARNING("%s: PCON TX link training has not finished.\n", __func__);
1123 
1124 			/* Link training not finished, ignore values from this DPCD reg. */
1125 			return;
1126 		}
1127 
1128 		link->dpcd_caps.dongle_caps.dp_hdmi_frl_max_link_bw_in_kbps = intersect_frl_link_bw_support(
1129 				link->dpcd_caps.dongle_caps.dp_hdmi_frl_max_link_bw_in_kbps,
1130 				hdmi_encoded_link_bw);
1131 		DC_LOG_DC("%s: pcon frl link bw = %u\n", __func__,
1132 			link->dpcd_caps.dongle_caps.dp_hdmi_frl_max_link_bw_in_kbps);
1133 	}
1134 }
1135 
get_active_converter_info(uint8_t data,struct dc_link * link)1136 static void get_active_converter_info(
1137 	uint8_t data, struct dc_link *link)
1138 {
1139 	union dp_downstream_port_present ds_port = { .byte = data };
1140 	memset(&link->dpcd_caps.dongle_caps, 0, sizeof(link->dpcd_caps.dongle_caps));
1141 
1142 	/* decode converter info*/
1143 	if (!ds_port.fields.PORT_PRESENT) {
1144 		link->dpcd_caps.dongle_type = DISPLAY_DONGLE_NONE;
1145 		set_dongle_type(link->ddc,
1146 				link->dpcd_caps.dongle_type);
1147 		link->dpcd_caps.is_branch_dev = false;
1148 		return;
1149 	}
1150 
1151 	/* DPCD 0x5 bit 0 = 1, it indicate it's branch device */
1152 	link->dpcd_caps.is_branch_dev = ds_port.fields.PORT_PRESENT;
1153 
1154 	switch (ds_port.fields.PORT_TYPE) {
1155 	case DOWNSTREAM_VGA:
1156 		link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_VGA_CONVERTER;
1157 		break;
1158 	case DOWNSTREAM_DVI_HDMI_DP_PLUS_PLUS:
1159 		/* At this point we don't know is it DVI or HDMI or DP++,
1160 		 * assume DVI.*/
1161 		link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_DVI_CONVERTER;
1162 		break;
1163 	default:
1164 		link->dpcd_caps.dongle_type = DISPLAY_DONGLE_NONE;
1165 		break;
1166 	}
1167 
1168 	if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_11) {
1169 		uint8_t det_caps[16] = {0}; /* CTS 4.2.2.7 expects source to read Detailed Capabilities Info : 00080h-0008F.*/
1170 		union dwnstream_port_caps_byte0 *port_caps =
1171 			(union dwnstream_port_caps_byte0 *)det_caps;
1172 		if (core_link_read_dpcd(link, DP_DOWNSTREAM_PORT_0,
1173 				det_caps, sizeof(det_caps)) == DC_OK) {
1174 
1175 			switch (port_caps->bits.DWN_STRM_PORTX_TYPE) {
1176 			/*Handle DP case as DONGLE_NONE*/
1177 			case DOWN_STREAM_DETAILED_DP:
1178 				link->dpcd_caps.dongle_type = DISPLAY_DONGLE_NONE;
1179 				break;
1180 			case DOWN_STREAM_DETAILED_VGA:
1181 				link->dpcd_caps.dongle_type =
1182 					DISPLAY_DONGLE_DP_VGA_CONVERTER;
1183 				break;
1184 			case DOWN_STREAM_DETAILED_DVI:
1185 				link->dpcd_caps.dongle_type =
1186 					DISPLAY_DONGLE_DP_DVI_CONVERTER;
1187 				break;
1188 			case DOWN_STREAM_DETAILED_HDMI:
1189 			case DOWN_STREAM_DETAILED_DP_PLUS_PLUS:
1190 				/*Handle DP++ active converter case, process DP++ case as HDMI case according DP1.4 spec*/
1191 				link->dpcd_caps.dongle_type =
1192 					DISPLAY_DONGLE_DP_HDMI_CONVERTER;
1193 
1194 				link->dpcd_caps.dongle_caps.dongle_type = link->dpcd_caps.dongle_type;
1195 				if (ds_port.fields.DETAILED_CAPS) {
1196 
1197 					union dwnstream_port_caps_byte3_hdmi
1198 						hdmi_caps = {.raw = det_caps[3] };
1199 					union dwnstream_port_caps_byte2
1200 						hdmi_color_caps = {.raw = det_caps[2] };
1201 					link->dpcd_caps.dongle_caps.dp_hdmi_max_pixel_clk_in_khz =
1202 						det_caps[1] * 2500;
1203 
1204 					link->dpcd_caps.dongle_caps.is_dp_hdmi_s3d_converter =
1205 						hdmi_caps.bits.FRAME_SEQ_TO_FRAME_PACK;
1206 					/*YCBCR capability only for HDMI case*/
1207 					if (port_caps->bits.DWN_STRM_PORTX_TYPE
1208 							== DOWN_STREAM_DETAILED_HDMI) {
1209 						link->dpcd_caps.dongle_caps.is_dp_hdmi_ycbcr422_pass_through =
1210 								hdmi_caps.bits.YCrCr422_PASS_THROUGH;
1211 						link->dpcd_caps.dongle_caps.is_dp_hdmi_ycbcr420_pass_through =
1212 								hdmi_caps.bits.YCrCr420_PASS_THROUGH;
1213 						link->dpcd_caps.dongle_caps.is_dp_hdmi_ycbcr422_converter =
1214 								hdmi_caps.bits.YCrCr422_CONVERSION;
1215 						link->dpcd_caps.dongle_caps.is_dp_hdmi_ycbcr420_converter =
1216 								hdmi_caps.bits.YCrCr420_CONVERSION;
1217 					}
1218 
1219 					link->dpcd_caps.dongle_caps.dp_hdmi_max_bpc =
1220 						translate_dpcd_max_bpc(
1221 							hdmi_color_caps.bits.MAX_BITS_PER_COLOR_COMPONENT);
1222 
1223 					if (link->dc->caps.dp_hdmi21_pcon_support) {
1224 
1225 						link->dpcd_caps.dongle_caps.dp_hdmi_frl_max_link_bw_in_kbps =
1226 								link_bw_kbps_from_raw_frl_link_rate_data(
1227 										hdmi_color_caps.bits.MAX_ENCODED_LINK_BW_SUPPORT);
1228 
1229 						read_and_intersect_post_frl_lt_status(link);
1230 
1231 						if (link->dpcd_caps.dongle_caps.dp_hdmi_frl_max_link_bw_in_kbps > 0)
1232 							link->dpcd_caps.dongle_caps.extendedCapValid = true;
1233 					}
1234 
1235 					if (link->dpcd_caps.dongle_caps.dp_hdmi_max_pixel_clk_in_khz != 0)
1236 						link->dpcd_caps.dongle_caps.extendedCapValid = true;
1237 				}
1238 
1239 				break;
1240 			}
1241 		}
1242 	}
1243 
1244 	set_dongle_type(link->ddc, link->dpcd_caps.dongle_type);
1245 
1246 	{
1247 		struct dp_sink_hw_fw_revision dp_hw_fw_revision = {0};
1248 
1249 		core_link_read_dpcd(
1250 			link,
1251 			DP_BRANCH_REVISION_START,
1252 			(uint8_t *)&dp_hw_fw_revision,
1253 			sizeof(dp_hw_fw_revision));
1254 
1255 		link->dpcd_caps.branch_hw_revision =
1256 			dp_hw_fw_revision.ieee_hw_rev;
1257 
1258 		memmove(
1259 			link->dpcd_caps.branch_fw_revision,
1260 			dp_hw_fw_revision.ieee_fw_rev,
1261 			sizeof(dp_hw_fw_revision.ieee_fw_rev));
1262 	}
1263 
1264 	core_link_read_dpcd(
1265 		link,
1266 		DP_BRANCH_VENDOR_SPECIFIC_START,
1267 		(uint8_t *)link->dpcd_caps.branch_vendor_specific_data,
1268 		sizeof(link->dpcd_caps.branch_vendor_specific_data));
1269 
1270 	if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_14 &&
1271 			link->dpcd_caps.dongle_type != DISPLAY_DONGLE_NONE) {
1272 		union dp_dfp_cap_ext dfp_cap_ext;
1273 		memset(&dfp_cap_ext, '\0', sizeof (dfp_cap_ext));
1274 		core_link_read_dpcd(
1275 				link,
1276 				DP_DFP_CAPABILITY_EXTENSION_SUPPORT,
1277 				dfp_cap_ext.raw,
1278 				sizeof(dfp_cap_ext.raw));
1279 		link->dpcd_caps.dongle_caps.dfp_cap_ext.supported = dfp_cap_ext.fields.supported;
1280 		link->dpcd_caps.dongle_caps.dfp_cap_ext.max_pixel_rate_in_mps =
1281 				dfp_cap_ext.fields.max_pixel_rate_in_mps[0] +
1282 				(dfp_cap_ext.fields.max_pixel_rate_in_mps[1] << 8);
1283 		link->dpcd_caps.dongle_caps.dfp_cap_ext.max_video_h_active_width =
1284 				dfp_cap_ext.fields.max_video_h_active_width[0] +
1285 				(dfp_cap_ext.fields.max_video_h_active_width[1] << 8);
1286 		link->dpcd_caps.dongle_caps.dfp_cap_ext.max_video_v_active_height =
1287 				dfp_cap_ext.fields.max_video_v_active_height[0] +
1288 				(dfp_cap_ext.fields.max_video_v_active_height[1] << 8);
1289 		link->dpcd_caps.dongle_caps.dfp_cap_ext.encoding_format_caps =
1290 				dfp_cap_ext.fields.encoding_format_caps;
1291 		link->dpcd_caps.dongle_caps.dfp_cap_ext.rgb_color_depth_caps =
1292 				dfp_cap_ext.fields.rgb_color_depth_caps;
1293 		link->dpcd_caps.dongle_caps.dfp_cap_ext.ycbcr444_color_depth_caps =
1294 				dfp_cap_ext.fields.ycbcr444_color_depth_caps;
1295 		link->dpcd_caps.dongle_caps.dfp_cap_ext.ycbcr422_color_depth_caps =
1296 				dfp_cap_ext.fields.ycbcr422_color_depth_caps;
1297 		link->dpcd_caps.dongle_caps.dfp_cap_ext.ycbcr420_color_depth_caps =
1298 				dfp_cap_ext.fields.ycbcr420_color_depth_caps;
1299 		DC_LOG_DP2("DFP capability extension is read at link %d", link->link_index);
1300 		DC_LOG_DP2("\tdfp_cap_ext.supported = %s", link->dpcd_caps.dongle_caps.dfp_cap_ext.supported ? "true" : "false");
1301 		DC_LOG_DP2("\tdfp_cap_ext.max_pixel_rate_in_mps = %d", link->dpcd_caps.dongle_caps.dfp_cap_ext.max_pixel_rate_in_mps);
1302 		DC_LOG_DP2("\tdfp_cap_ext.max_video_h_active_width = %d", link->dpcd_caps.dongle_caps.dfp_cap_ext.max_video_h_active_width);
1303 		DC_LOG_DP2("\tdfp_cap_ext.max_video_v_active_height = %d", link->dpcd_caps.dongle_caps.dfp_cap_ext.max_video_v_active_height);
1304 	}
1305 }
1306 
apply_usbc_combo_phy_reset_wa(struct dc_link * link,struct dc_link_settings * link_settings)1307 static void apply_usbc_combo_phy_reset_wa(struct dc_link *link,
1308 		struct dc_link_settings *link_settings)
1309 {
1310 	/* Temporary Renoir-specific workaround PHY will sometimes be in bad
1311 	 * state on hotplugging display from certain USB-C dongle, so add extra
1312 	 * cycle of enabling and disabling the PHY before first link training.
1313 	 */
1314 	struct link_resource link_res = {0};
1315 	enum clock_source_id dp_cs_id = get_clock_source_id(link);
1316 
1317 	dp_enable_link_phy(link, &link_res, link->connector_signal,
1318 			dp_cs_id, link_settings);
1319 	dp_disable_link_phy(link, &link_res, link->connector_signal);
1320 }
1321 
dp_overwrite_extended_receiver_cap(struct dc_link * link)1322 bool dp_overwrite_extended_receiver_cap(struct dc_link *link)
1323 {
1324 	uint8_t dpcd_data[16] = {0};
1325 	uint32_t read_dpcd_retry_cnt = 3;
1326 	enum dc_status status = DC_ERROR_UNEXPECTED;
1327 	union dp_downstream_port_present ds_port = { 0 };
1328 	union down_stream_port_count down_strm_port_count;
1329 	union edp_configuration_cap edp_config_cap;
1330 
1331 	int i;
1332 
1333 	for (i = 0; i < read_dpcd_retry_cnt; i++) {
1334 		status = core_link_read_dpcd(
1335 				link,
1336 				DP_DPCD_REV,
1337 				dpcd_data,
1338 				sizeof(dpcd_data));
1339 		if (status == DC_OK)
1340 			break;
1341 	}
1342 
1343 	link->dpcd_caps.dpcd_rev.raw =
1344 		dpcd_data[DP_DPCD_REV - DP_DPCD_REV];
1345 
1346 	if (dpcd_data[DP_MAX_LANE_COUNT - DP_DPCD_REV] == 0)
1347 		return false;
1348 
1349 	ds_port.byte = dpcd_data[DP_DOWNSTREAMPORT_PRESENT -
1350 			DP_DPCD_REV];
1351 
1352 	get_active_converter_info(ds_port.byte, link);
1353 
1354 	down_strm_port_count.raw = dpcd_data[DP_DOWN_STREAM_PORT_COUNT -
1355 			DP_DPCD_REV];
1356 
1357 	link->dpcd_caps.allow_invalid_MSA_timing_param =
1358 		down_strm_port_count.bits.IGNORE_MSA_TIMING_PARAM;
1359 
1360 	link->dpcd_caps.max_ln_count.raw = dpcd_data[
1361 		DP_MAX_LANE_COUNT - DP_DPCD_REV];
1362 
1363 	link->dpcd_caps.max_down_spread.raw = dpcd_data[
1364 		DP_MAX_DOWNSPREAD - DP_DPCD_REV];
1365 
1366 	link->reported_link_cap.lane_count =
1367 		link->dpcd_caps.max_ln_count.bits.MAX_LANE_COUNT;
1368 	link->reported_link_cap.link_rate = dpcd_data[
1369 		DP_MAX_LINK_RATE - DP_DPCD_REV];
1370 	link->reported_link_cap.link_spread =
1371 		link->dpcd_caps.max_down_spread.bits.MAX_DOWN_SPREAD ?
1372 		LINK_SPREAD_05_DOWNSPREAD_30KHZ : LINK_SPREAD_DISABLED;
1373 
1374 	edp_config_cap.raw = dpcd_data[
1375 		DP_EDP_CONFIGURATION_CAP - DP_DPCD_REV];
1376 	link->dpcd_caps.panel_mode_edp =
1377 		edp_config_cap.bits.ALT_SCRAMBLER_RESET;
1378 	link->dpcd_caps.dpcd_display_control_capable =
1379 		edp_config_cap.bits.DPCD_DISPLAY_CONTROL_CAPABLE;
1380 
1381 	return true;
1382 }
1383 
dpcd_set_source_specific_data(struct dc_link * link)1384 void dpcd_set_source_specific_data(struct dc_link *link)
1385 {
1386 	if (!link->dc->vendor_signature.is_valid) {
1387 		enum dc_status __maybe_unused result_write_min_hblank = DC_NOT_SUPPORTED;
1388 		struct dpcd_amd_signature amd_signature = {0};
1389 		struct dpcd_amd_device_id amd_device_id = {0};
1390 
1391 		amd_device_id.device_id_byte1 =
1392 				(uint8_t)(link->ctx->asic_id.chip_id);
1393 		amd_device_id.device_id_byte2 =
1394 				(uint8_t)(link->ctx->asic_id.chip_id >> 8);
1395 		amd_device_id.dce_version =
1396 				(uint8_t)(link->ctx->dce_version);
1397 		amd_device_id.dal_version_byte1 = 0x0; // needed? where to get?
1398 		amd_device_id.dal_version_byte2 = 0x0; // needed? where to get?
1399 
1400 		core_link_read_dpcd(link, DP_SOURCE_OUI,
1401 				(uint8_t *)(&amd_signature),
1402 				sizeof(amd_signature));
1403 
1404 		if (!((amd_signature.AMD_IEEE_TxSignature_byte1 == 0x0) &&
1405 			(amd_signature.AMD_IEEE_TxSignature_byte2 == 0x0) &&
1406 			(amd_signature.AMD_IEEE_TxSignature_byte3 == 0x1A))) {
1407 
1408 			amd_signature.AMD_IEEE_TxSignature_byte1 = 0x0;
1409 			amd_signature.AMD_IEEE_TxSignature_byte2 = 0x0;
1410 			amd_signature.AMD_IEEE_TxSignature_byte3 = 0x1A;
1411 
1412 			core_link_write_dpcd(link, DP_SOURCE_OUI,
1413 				(uint8_t *)(&amd_signature),
1414 				sizeof(amd_signature));
1415 		}
1416 
1417 		core_link_write_dpcd(link, DP_SOURCE_OUI+0x03,
1418 				(uint8_t *)(&amd_device_id),
1419 				sizeof(amd_device_id));
1420 
1421 		if (link->ctx->dce_version >= DCN_VERSION_2_0 &&
1422 			link->dc->caps.min_horizontal_blanking_period != 0) {
1423 
1424 			uint8_t hblank_size = (uint8_t)link->dc->caps.min_horizontal_blanking_period;
1425 
1426 			result_write_min_hblank = core_link_write_dpcd(link,
1427 				DP_SOURCE_MINIMUM_HBLANK_SUPPORTED, (uint8_t *)(&hblank_size),
1428 				sizeof(hblank_size));
1429 		}
1430 		DC_TRACE_LEVEL_MESSAGE(DAL_TRACE_LEVEL_INFORMATION,
1431 							WPP_BIT_FLAG_DC_DETECTION_DP_CAPS,
1432 							"result=%u link_index=%u enum dce_version=%d DPCD=0x%04X min_hblank=%u branch_dev_id=0x%x branch_dev_name='%c%c%c%c%c%c'",
1433 							result_write_min_hblank,
1434 							link->link_index,
1435 							link->ctx->dce_version,
1436 							DP_SOURCE_MINIMUM_HBLANK_SUPPORTED,
1437 							link->dc->caps.min_horizontal_blanking_period,
1438 							link->dpcd_caps.branch_dev_id,
1439 							link->dpcd_caps.branch_dev_name[0],
1440 							link->dpcd_caps.branch_dev_name[1],
1441 							link->dpcd_caps.branch_dev_name[2],
1442 							link->dpcd_caps.branch_dev_name[3],
1443 							link->dpcd_caps.branch_dev_name[4],
1444 							link->dpcd_caps.branch_dev_name[5]);
1445 	} else {
1446 		core_link_write_dpcd(link, DP_SOURCE_OUI,
1447 				link->dc->vendor_signature.data.raw,
1448 				sizeof(link->dc->vendor_signature.data.raw));
1449 	}
1450 }
1451 
dpcd_write_cable_id_to_dprx(struct dc_link * link)1452 void dpcd_write_cable_id_to_dprx(struct dc_link *link)
1453 {
1454 	if (!link->dpcd_caps.channel_coding_cap.bits.DP_128b_132b_SUPPORTED ||
1455 			link->dpcd_caps.cable_id.raw == 0 ||
1456 			link->dprx_states.cable_id_written)
1457 		return;
1458 
1459 	core_link_write_dpcd(link, DP_CABLE_ATTRIBUTES_UPDATED_BY_DPTX,
1460 			&link->dpcd_caps.cable_id.raw,
1461 			sizeof(link->dpcd_caps.cable_id.raw));
1462 
1463 	link->dprx_states.cable_id_written = 1;
1464 }
1465 
get_usbc_cable_id(struct dc_link * link,union dp_cable_id * cable_id)1466 static bool get_usbc_cable_id(struct dc_link *link, union dp_cable_id *cable_id)
1467 {
1468 	union dmub_rb_cmd cmd;
1469 
1470 	if (!link->ctx->dmub_srv ||
1471 			link->ep_type != DISPLAY_ENDPOINT_PHY ||
1472 			link->link_enc->features.flags.bits.DP_IS_USB_C == 0)
1473 		return false;
1474 
1475 	memset(&cmd, 0, sizeof(cmd));
1476 	cmd.cable_id.header.type = DMUB_CMD_GET_USBC_CABLE_ID;
1477 	cmd.cable_id.header.payload_bytes = sizeof(cmd.cable_id.data);
1478 	cmd.cable_id.data.input.phy_inst = resource_transmitter_to_phy_idx(
1479 			link->dc, link->link_enc->transmitter);
1480 	if (dc_wake_and_execute_dmub_cmd(link->dc->ctx, &cmd, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY) &&
1481 			cmd.cable_id.header.ret_status == 1) {
1482 		cable_id->raw = cmd.cable_id.data.output_raw;
1483 		DC_LOG_DC("usbc_cable_id = %d.\n", cable_id->raw);
1484 	}
1485 	return cmd.cable_id.header.ret_status == 1;
1486 }
1487 
retrieve_cable_id(struct dc_link * link)1488 static void retrieve_cable_id(struct dc_link *link)
1489 {
1490 	union dp_cable_id usbc_cable_id = {0};
1491 
1492 	link->dpcd_caps.cable_id.raw = 0;
1493 	core_link_read_dpcd(link, DP_CABLE_ATTRIBUTES_UPDATED_BY_DPRX,
1494 			&link->dpcd_caps.cable_id.raw, sizeof(uint8_t));
1495 
1496 	if (get_usbc_cable_id(link, &usbc_cable_id))
1497 		link->dpcd_caps.cable_id = intersect_cable_id(
1498 				&link->dpcd_caps.cable_id, &usbc_cable_id);
1499 }
1500 
read_is_mst_supported(struct dc_link * link)1501 bool read_is_mst_supported(struct dc_link *link)
1502 {
1503 	bool mst          = false;
1504 	enum dc_status st = DC_OK;
1505 	union dpcd_rev rev;
1506 	union mstm_cap cap;
1507 
1508 	if (link->preferred_training_settings.mst_enable &&
1509 		*link->preferred_training_settings.mst_enable == false) {
1510 		return false;
1511 	}
1512 
1513 	rev.raw  = 0;
1514 	cap.raw  = 0;
1515 
1516 	st = core_link_read_dpcd(link, DP_DPCD_REV, &rev.raw,
1517 			sizeof(rev));
1518 
1519 	if (st == DC_OK && rev.raw >= DPCD_REV_12) {
1520 
1521 		st = core_link_read_dpcd(link, DP_MSTM_CAP,
1522 				&cap.raw, sizeof(cap));
1523 		if (st == DC_OK && cap.bits.MST_CAP == 1)
1524 			mst = true;
1525 	}
1526 	return mst;
1527 
1528 }
1529 
1530 /* Read additional sink caps defined in source specific DPCD area
1531  * This function currently only reads from SinkCapability address (DP_SOURCE_SINK_CAP)
1532  * TODO: Add FS caps and read from DP_SOURCE_SINK_FS_CAP as well
1533  */
dpcd_read_sink_ext_caps(struct dc_link * link)1534 static bool dpcd_read_sink_ext_caps(struct dc_link *link)
1535 {
1536 	uint8_t dpcd_data = 0;
1537 	uint8_t edp_general_cap2 = 0;
1538 
1539 	if (!link)
1540 		return false;
1541 
1542 	if (core_link_read_dpcd(link, DP_SOURCE_SINK_CAP, &dpcd_data, 1) != DC_OK)
1543 		return false;
1544 
1545 	link->dpcd_sink_ext_caps.raw = dpcd_data;
1546 
1547 	if (core_link_read_dpcd(link, DP_EDP_GENERAL_CAP_2, &edp_general_cap2, 1) != DC_OK)
1548 		return false;
1549 
1550 	link->dpcd_caps.panel_luminance_control = (edp_general_cap2 & DP_EDP_PANEL_LUMINANCE_CONTROL_CAPABLE) != 0;
1551 
1552 	return true;
1553 }
1554 
dp_retrieve_lttpr_cap(struct dc_link * link)1555 enum dc_status dp_retrieve_lttpr_cap(struct dc_link *link)
1556 {
1557 	uint8_t lttpr_dpcd_data[10] = {0};
1558 	enum dc_status status;
1559 	bool is_lttpr_present;
1560 	uint32_t lttpr_count;
1561 	uint32_t closest_lttpr_offset;
1562 
1563 	/* Logic to determine LTTPR support*/
1564 	bool vbios_lttpr_interop = link->dc->caps.vbios_lttpr_aware;
1565 
1566 	if (!vbios_lttpr_interop || !link->dc->caps.extended_aux_timeout_support)
1567 		return DC_NOT_SUPPORTED;
1568 
1569 	/* By reading LTTPR capability, RX assumes that we will enable
1570 	 * LTTPR extended aux timeout if LTTPR is present.
1571 	 */
1572 	status = core_link_read_dpcd(
1573 			link,
1574 			DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
1575 			lttpr_dpcd_data,
1576 			sizeof(lttpr_dpcd_data));
1577 
1578 	link->dpcd_caps.lttpr_caps.revision.raw =
1579 			lttpr_dpcd_data[DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV -
1580 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1581 
1582 	link->dpcd_caps.lttpr_caps.max_link_rate =
1583 			lttpr_dpcd_data[DP_MAX_LINK_RATE_PHY_REPEATER -
1584 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1585 
1586 	link->dpcd_caps.lttpr_caps.phy_repeater_cnt =
1587 			lttpr_dpcd_data[DP_PHY_REPEATER_CNT -
1588 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1589 
1590 	link->dpcd_caps.lttpr_caps.max_lane_count =
1591 			lttpr_dpcd_data[DP_MAX_LANE_COUNT_PHY_REPEATER -
1592 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1593 
1594 	link->dpcd_caps.lttpr_caps.mode =
1595 			lttpr_dpcd_data[DP_PHY_REPEATER_MODE -
1596 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1597 
1598 	link->dpcd_caps.lttpr_caps.max_ext_timeout =
1599 			lttpr_dpcd_data[DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT -
1600 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1601 	link->dpcd_caps.lttpr_caps.main_link_channel_coding.raw =
1602 			lttpr_dpcd_data[DP_MAIN_LINK_CHANNEL_CODING_PHY_REPEATER -
1603 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1604 
1605 	link->dpcd_caps.lttpr_caps.supported_128b_132b_rates.raw =
1606 			lttpr_dpcd_data[DP_PHY_REPEATER_128B132B_RATES -
1607 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1608 
1609 	link->dpcd_caps.lttpr_caps.alpm.raw =
1610 			lttpr_dpcd_data[DP_LTTPR_ALPM_CAPABILITIES -
1611 							DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
1612 
1613 	lttpr_count = dp_parse_lttpr_repeater_count(link->dpcd_caps.lttpr_caps.phy_repeater_cnt);
1614 
1615 	/* If this chip cap is set, at least one retimer must exist in the chain
1616 	 * Override count to 1 if we receive a known bad count (0 or an invalid value) */
1617 	if (((link->chip_caps & AMD_EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK) == AMD_EXT_DISPLAY_PATH_CAPS__DP_FIXED_VS_EN) &&
1618 			lttpr_count == 0) {
1619 		/* If you see this message consistently, either the host platform has FIXED_VS flag
1620 		 * incorrectly configured or the sink device is returning an invalid count.
1621 		 */
1622 		DC_LOG_ERROR("lttpr_caps phy_repeater_cnt is 0x%x, forcing it to 0x80.",
1623 			     link->dpcd_caps.lttpr_caps.phy_repeater_cnt);
1624 		link->dpcd_caps.lttpr_caps.phy_repeater_cnt = 0x80;
1625 		lttpr_count = 1;
1626 		DC_LOG_DC("lttpr_caps forced phy_repeater_cnt = %d\n", link->dpcd_caps.lttpr_caps.phy_repeater_cnt);
1627 	}
1628 
1629 	is_lttpr_present = dp_is_lttpr_present(link);
1630 
1631 	DC_LOG_DC("is_lttpr_present = %d\n", is_lttpr_present);
1632 
1633 	if (is_lttpr_present) {
1634 		CONN_DATA_DETECT(link, lttpr_dpcd_data, sizeof(lttpr_dpcd_data), "LTTPR Caps: ");
1635 
1636 		// Identify closest LTTPR to determine if workarounds required for known embedded LTTPR
1637 		closest_lttpr_offset = dp_get_closest_lttpr_offset(lttpr_count);
1638 
1639 		core_link_read_dpcd(link, (DP_LTTPR_IEEE_OUI + closest_lttpr_offset),
1640 				link->dpcd_caps.lttpr_caps.lttpr_ieee_oui, sizeof(link->dpcd_caps.lttpr_caps.lttpr_ieee_oui));
1641 		core_link_read_dpcd(link, (DP_LTTPR_DEVICE_ID + closest_lttpr_offset),
1642 				link->dpcd_caps.lttpr_caps.lttpr_device_id, sizeof(link->dpcd_caps.lttpr_caps.lttpr_device_id));
1643 
1644 		if (lttpr_count > 1) {
1645 			CONN_DATA_DETECT(link, link->dpcd_caps.lttpr_caps.lttpr_ieee_oui, sizeof(link->dpcd_caps.lttpr_caps.lttpr_ieee_oui),
1646 					"Closest LTTPR To Host's IEEE OUI: ");
1647 			CONN_DATA_DETECT(link, link->dpcd_caps.lttpr_caps.lttpr_device_id, sizeof(link->dpcd_caps.lttpr_caps.lttpr_device_id),
1648 					"Closest LTTPR To Host's LTTPR Device ID: ");
1649 		} else {
1650 			CONN_DATA_DETECT(link, link->dpcd_caps.lttpr_caps.lttpr_ieee_oui, sizeof(link->dpcd_caps.lttpr_caps.lttpr_ieee_oui),
1651 					"LTTPR IEEE OUI: ");
1652 			CONN_DATA_DETECT(link, link->dpcd_caps.lttpr_caps.lttpr_device_id, sizeof(link->dpcd_caps.lttpr_caps.lttpr_device_id),
1653 					"LTTPR Device ID: ");
1654 		}
1655 	}
1656 
1657 	return status;
1658 }
1659 
retrieve_link_cap(struct dc_link * link)1660 static bool retrieve_link_cap(struct dc_link *link)
1661 {
1662 	/* DP_ADAPTER_CAP - DP_DPCD_REV + 1 == 16 and also DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT + 1 == 16,
1663 	 * which means size 16 will be good for both of those DPCD register block reads
1664 	 */
1665 	uint8_t dpcd_data[16];
1666 	/*Only need to read 1 byte starting from DP_DPRX_FEATURE_ENUMERATION_LIST.
1667 	 */
1668 	uint8_t dpcd_dprx_data = '\0';
1669 
1670 	struct dp_device_vendor_id sink_id;
1671 	union down_stream_port_count down_strm_port_count;
1672 	union edp_configuration_cap edp_config_cap;
1673 	union dp_downstream_port_present ds_port = { 0 };
1674 	enum dc_status status = DC_ERROR_UNEXPECTED;
1675 	uint32_t read_dpcd_retry_cnt = 3;
1676 	int i;
1677 	struct dp_sink_hw_fw_revision dp_hw_fw_revision;
1678 	const uint32_t post_oui_delay = 30; // 30ms
1679 	bool is_fec_supported = false;
1680 	bool is_dsc_basic_supported = false;
1681 	bool is_dsc_passthrough_supported = false;
1682 
1683 	memset(dpcd_data, '\0', sizeof(dpcd_data));
1684 	memset(&down_strm_port_count,
1685 		'\0', sizeof(union down_stream_port_count));
1686 	memset(&edp_config_cap, '\0',
1687 		sizeof(union edp_configuration_cap));
1688 
1689 	/* if extended timeout is supported in hardware,
1690 	 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer
1691 	 * CTS 4.2.1.1 regression introduced by CTS specs requirement update.
1692 	 */
1693 	try_to_configure_aux_timeout(link->ddc,
1694 			LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD);
1695 
1696 	status = dp_retrieve_lttpr_cap(link);
1697 
1698 	if (status != DC_OK) {
1699 		status = wake_up_aux_channel(link);
1700 		if (status == DC_OK)
1701 			dp_retrieve_lttpr_cap(link);
1702 		else
1703 			return false;
1704 	}
1705 
1706 	if (dp_is_lttpr_present(link)) {
1707 		configure_lttpr_mode_transparent(link);
1708 
1709 		// Echo TOTAL_LTTPR_CNT back downstream
1710 		core_link_write_dpcd(
1711 				link,
1712 				DP_TOTAL_LTTPR_CNT,
1713 				&link->dpcd_caps.lttpr_caps.phy_repeater_cnt,
1714 				sizeof(link->dpcd_caps.lttpr_caps.phy_repeater_cnt));
1715 	}
1716 
1717 	dpcd_set_source_specific_data(link);
1718 	/* Sink may need to configure internals based on vendor, so allow some
1719 	 * time before proceeding with possibly vendor specific transactions
1720 	 */
1721 	msleep(post_oui_delay);
1722 
1723 	for (i = 0; i < read_dpcd_retry_cnt; i++) {
1724 		status = core_link_read_dpcd(
1725 				link,
1726 				DP_DPCD_REV,
1727 				dpcd_data,
1728 				sizeof(dpcd_data));
1729 		if (status == DC_OK)
1730 			break;
1731 	}
1732 
1733 
1734 	if (status != DC_OK) {
1735 		dm_error("%s: Read receiver caps dpcd data failed.\n", __func__);
1736 		return false;
1737 	}
1738 
1739 	if (!dp_is_lttpr_present(link))
1740 		try_to_configure_aux_timeout(link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD);
1741 
1742 
1743 	{
1744 		union training_aux_rd_interval aux_rd_interval;
1745 
1746 		aux_rd_interval.raw =
1747 			dpcd_data[DP_TRAINING_AUX_RD_INTERVAL];
1748 
1749 		link->dpcd_caps.ext_receiver_cap_field_present =
1750 				aux_rd_interval.bits.EXT_RECEIVER_CAP_FIELD_PRESENT == 1;
1751 
1752 		if (aux_rd_interval.bits.EXT_RECEIVER_CAP_FIELD_PRESENT == 1) {
1753 			uint8_t ext_cap_data[16];
1754 
1755 			memset(ext_cap_data, '\0', sizeof(ext_cap_data));
1756 			for (i = 0; i < read_dpcd_retry_cnt; i++) {
1757 				status = core_link_read_dpcd(
1758 				link,
1759 				DP_DP13_DPCD_REV,
1760 				ext_cap_data,
1761 				sizeof(ext_cap_data));
1762 				if (status == DC_OK) {
1763 					memcpy(dpcd_data, ext_cap_data, sizeof(dpcd_data));
1764 					break;
1765 				}
1766 			}
1767 			if (status != DC_OK)
1768 				dm_error("%s: Read extend caps data failed, use cap from dpcd 0.\n", __func__);
1769 		}
1770 	}
1771 
1772 	link->dpcd_caps.dpcd_rev.raw =
1773 			dpcd_data[DP_DPCD_REV - DP_DPCD_REV];
1774 
1775 	if (link->dpcd_caps.ext_receiver_cap_field_present) {
1776 		for (i = 0; i < read_dpcd_retry_cnt; i++) {
1777 			status = core_link_read_dpcd(
1778 					link,
1779 					DP_DPRX_FEATURE_ENUMERATION_LIST,
1780 					&dpcd_dprx_data,
1781 					sizeof(dpcd_dprx_data));
1782 			if (status == DC_OK)
1783 				break;
1784 		}
1785 
1786 		link->dpcd_caps.dprx_feature.raw = dpcd_dprx_data;
1787 
1788 		if (status != DC_OK)
1789 			dm_error("%s: Read DPRX feature list failed.\n", __func__);
1790 
1791 		/* AdaptiveSyncCapability  */
1792 		dpcd_dprx_data = 0;
1793 		for (i = 0; i < read_dpcd_retry_cnt; i++) {
1794 			status = core_link_read_dpcd(
1795 					link, DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1,
1796 					&dpcd_dprx_data, sizeof(dpcd_dprx_data));
1797 			if (status == DC_OK)
1798 				break;
1799 		}
1800 
1801 		link->dpcd_caps.adaptive_sync_caps.dp_adap_sync_caps.raw = dpcd_dprx_data;
1802 
1803 		if (status != DC_OK)
1804 			dm_error("%s: Read DPRX feature list_1 failed. Addr:%#x\n",
1805 					__func__, DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1);
1806 	}
1807 	else {
1808 		link->dpcd_caps.dprx_feature.raw = 0;
1809 	}
1810 
1811 	/* Error condition checking...
1812 	 * It is impossible for Sink to report Max Lane Count = 0.
1813 	 * It is possible for Sink to report Max Link Rate = 0, if it is
1814 	 * an eDP device that is reporting specialized link rates in the
1815 	 * SUPPORTED_LINK_RATE table.
1816 	 */
1817 	if (dpcd_data[DP_MAX_LANE_COUNT - DP_DPCD_REV] == 0)
1818 		return false;
1819 
1820 	ds_port.byte = dpcd_data[DP_DOWNSTREAMPORT_PRESENT -
1821 				 DP_DPCD_REV];
1822 
1823 	read_dp_device_vendor_id(link);
1824 
1825 	/* TODO - decouple raw mst capability from policy decision */
1826 	link->dpcd_caps.is_mst_capable = read_is_mst_supported(link);
1827 	DC_LOG_DC("%s: MST_Support: %s\n", __func__, str_yes_no(link->dpcd_caps.is_mst_capable));
1828 
1829 	get_active_converter_info(ds_port.byte, link);
1830 
1831 	dp_wa_power_up_0010FA(link, dpcd_data, sizeof(dpcd_data));
1832 
1833 	down_strm_port_count.raw = dpcd_data[DP_DOWN_STREAM_PORT_COUNT -
1834 				 DP_DPCD_REV];
1835 
1836 	link->dpcd_caps.allow_invalid_MSA_timing_param =
1837 		down_strm_port_count.bits.IGNORE_MSA_TIMING_PARAM;
1838 
1839 	link->dpcd_caps.max_ln_count.raw = dpcd_data[
1840 		DP_MAX_LANE_COUNT - DP_DPCD_REV];
1841 
1842 	link->dpcd_caps.max_down_spread.raw = dpcd_data[
1843 		DP_MAX_DOWNSPREAD - DP_DPCD_REV];
1844 
1845 	link->reported_link_cap.lane_count =
1846 		link->dpcd_caps.max_ln_count.bits.MAX_LANE_COUNT;
1847 	link->reported_link_cap.link_rate = get_link_rate_from_max_link_bw(
1848 			dpcd_data[DP_MAX_LINK_RATE - DP_DPCD_REV]);
1849 	link->reported_link_cap.link_spread =
1850 		link->dpcd_caps.max_down_spread.bits.MAX_DOWN_SPREAD ?
1851 		LINK_SPREAD_05_DOWNSPREAD_30KHZ : LINK_SPREAD_DISABLED;
1852 
1853 	edp_config_cap.raw = dpcd_data[
1854 		DP_EDP_CONFIGURATION_CAP - DP_DPCD_REV];
1855 	link->dpcd_caps.panel_mode_edp =
1856 		edp_config_cap.bits.ALT_SCRAMBLER_RESET;
1857 	link->dpcd_caps.dpcd_display_control_capable =
1858 		edp_config_cap.bits.DPCD_DISPLAY_CONTROL_CAPABLE;
1859 	link->dpcd_caps.channel_coding_cap.raw =
1860 			dpcd_data[DP_MAIN_LINK_CHANNEL_CODING - DP_DPCD_REV];
1861 	link->test_pattern_enabled = false;
1862 	link->compliance_test_state.raw = 0;
1863 
1864 	link->dpcd_caps.receive_port0_cap.raw[0] =
1865 			dpcd_data[DP_RECEIVE_PORT_0_CAP_0 - DP_DPCD_REV];
1866 	link->dpcd_caps.receive_port0_cap.raw[1] =
1867 			dpcd_data[DP_RECEIVE_PORT_0_BUFFER_SIZE - DP_DPCD_REV];
1868 
1869 	/* read sink count */
1870 	core_link_read_dpcd(link,
1871 			DP_SINK_COUNT,
1872 			&link->dpcd_caps.sink_count.raw,
1873 			sizeof(link->dpcd_caps.sink_count.raw));
1874 
1875 	/* read sink ieee oui */
1876 	core_link_read_dpcd(link,
1877 			DP_SINK_OUI,
1878 			(uint8_t *)(&sink_id),
1879 			sizeof(sink_id));
1880 
1881 	link->dpcd_caps.sink_dev_id =
1882 			(sink_id.ieee_oui[0] << 16) +
1883 			(sink_id.ieee_oui[1] << 8) +
1884 			(sink_id.ieee_oui[2]);
1885 
1886 	memmove(
1887 		link->dpcd_caps.sink_dev_id_str,
1888 		sink_id.ieee_device_id,
1889 		sizeof(sink_id.ieee_device_id));
1890 
1891 	core_link_read_dpcd(
1892 		link,
1893 		DP_SINK_HW_REVISION_START,
1894 		(uint8_t *)&dp_hw_fw_revision,
1895 		sizeof(dp_hw_fw_revision));
1896 
1897 	link->dpcd_caps.sink_hw_revision =
1898 		dp_hw_fw_revision.ieee_hw_rev;
1899 
1900 	memmove(
1901 		link->dpcd_caps.sink_fw_revision,
1902 		dp_hw_fw_revision.ieee_fw_rev,
1903 		sizeof(dp_hw_fw_revision.ieee_fw_rev));
1904 
1905 	/* Quirk for Retina panels: wrong DP_MAX_LINK_RATE */
1906 	{
1907 		uint8_t str_mbp_2018[] = { 101, 68, 21, 103, 98, 97 };
1908 		uint8_t fwrev_mbp_2018[] = { 7, 4 };
1909 		uint8_t fwrev_mbp_2018_vega[] = { 8, 4 };
1910 
1911 		/* We also check for the firmware revision as 16,1 models have an
1912 		 * identical device id and are incorrectly quirked otherwise.
1913 		 */
1914 		if ((link->dpcd_caps.sink_dev_id == 0x0010fa) &&
1915 		    !memcmp(link->dpcd_caps.sink_dev_id_str, str_mbp_2018,
1916 			     sizeof(str_mbp_2018)) &&
1917 		    (!memcmp(link->dpcd_caps.sink_fw_revision, fwrev_mbp_2018,
1918 			     sizeof(fwrev_mbp_2018)) ||
1919 		    !memcmp(link->dpcd_caps.sink_fw_revision, fwrev_mbp_2018_vega,
1920 			     sizeof(fwrev_mbp_2018_vega)))) {
1921 			link->reported_link_cap.link_rate = LINK_RATE_RBR2;
1922 		}
1923 	}
1924 
1925 	memset(&link->dpcd_caps.dsc_caps, '\0',
1926 			sizeof(link->dpcd_caps.dsc_caps));
1927 	memset(&link->dpcd_caps.fec_cap, '\0', sizeof(link->dpcd_caps.fec_cap));
1928 	/* Read DSC and FEC sink capabilities if DP revision is 1.4 and up */
1929 	if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_14) {
1930 		status = core_link_read_dpcd(
1931 				link,
1932 				DP_FEC_CAPABILITY,
1933 				&link->dpcd_caps.fec_cap.raw,
1934 				sizeof(link->dpcd_caps.fec_cap.raw));
1935 		if (status != DC_OK)
1936 			DC_LOG_ERROR("%s:%d: core_link_read_dpcd (DP_FEC_CAPABILITY) failed\n", __func__, __LINE__);
1937 
1938 		status = core_link_read_dpcd(
1939 				link,
1940 				DP_DSC_SUPPORT,
1941 				link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
1942 				sizeof(link->dpcd_caps.dsc_caps.dsc_basic_caps.raw));
1943 		if (status == DC_OK) {
1944 			is_fec_supported = link->dpcd_caps.fec_cap.bits.FEC_CAPABLE;
1945 			is_dsc_basic_supported = link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_SUPPORT;
1946 			is_dsc_passthrough_supported = link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_PASSTHROUGH_SUPPORT;
1947 			DC_LOG_DC("%s: FEC_Sink_Support: %s\n", __func__,
1948 				  str_yes_no(is_fec_supported));
1949 			DC_LOG_DC("%s: DSC_Basic_Sink_Support: %s\n", __func__,
1950 				  str_yes_no(is_dsc_basic_supported));
1951 			DC_LOG_DC("%s: DSC_Passthrough_Sink_Support: %s\n", __func__,
1952 				  str_yes_no(is_dsc_passthrough_supported));
1953 		}
1954 		if (link->dpcd_caps.dongle_type != DISPLAY_DONGLE_NONE) {
1955 			status = core_link_read_dpcd(
1956 					link,
1957 					DP_DSC_BRANCH_OVERALL_THROUGHPUT_0,
1958 					link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
1959 					sizeof(link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw));
1960 			if (status != DC_OK)
1961 				DC_LOG_ERROR("%s:%d: core_link_read_dpcd (DP_DSC_BRANCH_OVERALL_THROUGHPUT_0) failed\n", __func__, __LINE__);
1962 
1963 			DC_LOG_DSC("DSC branch decoder capability is read at link %d", link->link_index);
1964 			DC_LOG_DSC("\tBRANCH_OVERALL_THROUGHPUT_0 = 0x%02x",
1965 					link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.fields.BRANCH_OVERALL_THROUGHPUT_0);
1966 			DC_LOG_DSC("\tBRANCH_OVERALL_THROUGHPUT_1 = 0x%02x",
1967 					link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.fields.BRANCH_OVERALL_THROUGHPUT_1);
1968 			DC_LOG_DSC("\tBRANCH_MAX_LINE_WIDTH 0x%02x",
1969 					link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.fields.BRANCH_MAX_LINE_WIDTH);
1970 		}
1971 
1972 		/* Apply work around to disable FEC and DSC for USB4 tunneling in TBT3 compatibility mode
1973 		 * only if required.
1974 		 */
1975 		if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA &&
1976 				link->dc->debug.dpia_debug.bits.enable_force_tbt3_work_around &&
1977 				link->dpcd_caps.is_branch_dev &&
1978 				link->dpcd_caps.branch_dev_id == DP_BRANCH_DEVICE_ID_90CC24 &&
1979 				link->dpcd_caps.branch_hw_revision == DP_BRANCH_HW_REV_10 &&
1980 				(link->dpcd_caps.fec_cap.bits.FEC_CAPABLE ||
1981 				link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_SUPPORT)) {
1982 			/* A TBT3 device is expected to report no support for FEC or DSC to a USB4 DPIA.
1983 			 * Clear FEC and DSC capabilities as a work around if that is not the case.
1984 			 */
1985 			link->wa_flags.dpia_forced_tbt3_mode = true;
1986 			memset(&link->dpcd_caps.dsc_caps, '\0', sizeof(link->dpcd_caps.dsc_caps));
1987 			memset(&link->dpcd_caps.fec_cap, '\0', sizeof(link->dpcd_caps.fec_cap));
1988 			DC_LOG_DSC("Clear DSC SUPPORT for USB4 link(%d) in TBT3 compatibility mode", link->link_index);
1989 		} else
1990 			link->wa_flags.dpia_forced_tbt3_mode = false;
1991 	}
1992 
1993 	if (!dpcd_read_sink_ext_caps(link))
1994 		link->dpcd_sink_ext_caps.raw = 0;
1995 
1996 	if (link->dpcd_caps.channel_coding_cap.bits.DP_128b_132b_SUPPORTED) {
1997 		DC_LOG_DP2("128b/132b encoding is supported at link %d", link->link_index);
1998 
1999 		/* Read 128b/132b suppoerted link rates */
2000 		core_link_read_dpcd(link,
2001 				DP_128B132B_SUPPORTED_LINK_RATES,
2002 				&link->dpcd_caps.dp_128b_132b_supported_link_rates.raw,
2003 				sizeof(link->dpcd_caps.dp_128b_132b_supported_link_rates.raw));
2004 		if (link->dpcd_caps.dp_128b_132b_supported_link_rates.bits.UHBR20)
2005 			link->reported_link_cap.link_rate = LINK_RATE_UHBR20;
2006 		else if (link->dpcd_caps.dp_128b_132b_supported_link_rates.bits.UHBR13_5)
2007 			link->reported_link_cap.link_rate = LINK_RATE_UHBR13_5;
2008 		else if (link->dpcd_caps.dp_128b_132b_supported_link_rates.bits.UHBR10)
2009 			link->reported_link_cap.link_rate = LINK_RATE_UHBR10;
2010 		else
2011 			dm_error("%s: Invalid RX 128b_132b_supported_link_rates\n", __func__);
2012 		DC_LOG_DP2("128b/132b supported link rates is read at link %d", link->link_index);
2013 		DC_LOG_DP2("\tmax 128b/132b link rate support is %d.%d GHz",
2014 				link->reported_link_cap.link_rate / 100,
2015 				link->reported_link_cap.link_rate % 100);
2016 
2017 		core_link_read_dpcd(link,
2018 				DP_SINK_VIDEO_FALLBACK_FORMATS,
2019 				&link->dpcd_caps.fallback_formats.raw,
2020 				sizeof(link->dpcd_caps.fallback_formats.raw));
2021 		DC_LOG_DP2("sink video fallback format is read at link %d", link->link_index);
2022 		if (link->dpcd_caps.fallback_formats.bits.dp_1920x1080_60Hz_24bpp_support)
2023 			DC_LOG_DP2("\t1920x1080@60Hz 24bpp fallback format supported");
2024 		if (link->dpcd_caps.fallback_formats.bits.dp_1280x720_60Hz_24bpp_support)
2025 			DC_LOG_DP2("\t1280x720@60Hz 24bpp fallback format supported");
2026 		if (link->dpcd_caps.fallback_formats.bits.dp_1024x768_60Hz_24bpp_support)
2027 			DC_LOG_DP2("\t1024x768@60Hz 24bpp fallback format supported");
2028 		if (link->dpcd_caps.fallback_formats.raw == 0) {
2029 			DC_LOG_DP2("\tno supported fallback formats, assume 1920x1080@60Hz 24bpp is supported");
2030 			link->dpcd_caps.fallback_formats.bits.dp_1920x1080_60Hz_24bpp_support = 1;
2031 		}
2032 
2033 		core_link_read_dpcd(link,
2034 				DP_FEC_CAPABILITY_1,
2035 				&link->dpcd_caps.fec_cap1.raw,
2036 				sizeof(link->dpcd_caps.fec_cap1.raw));
2037 		DC_LOG_DP2("FEC CAPABILITY 1 is read at link %d", link->link_index);
2038 		if (link->dpcd_caps.fec_cap1.bits.AGGREGATED_ERROR_COUNTERS_CAPABLE)
2039 			DC_LOG_DP2("\tFEC aggregated error counters are supported");
2040 	}
2041 
2042 	core_link_read_dpcd(link,
2043 			DPCD_MAX_UNCOMPRESSED_PIXEL_RATE_CAP,
2044 			link->dpcd_caps.max_uncompressed_pixel_rate_cap.raw,
2045 			sizeof(link->dpcd_caps.max_uncompressed_pixel_rate_cap.raw));
2046 
2047 	/* Read DP tunneling information. */
2048 	status = dpcd_get_tunneling_device_data(link);
2049 	if (status != DC_OK)
2050 		DC_LOG_DP2("%s: Read DP tunneling device data failed.\n", __func__);
2051 
2052 	retrieve_cable_id(link);
2053 	dpcd_write_cable_id_to_dprx(link);
2054 
2055 	/* Connectivity log: detection */
2056 	CONN_DATA_DETECT(link, dpcd_data, sizeof(dpcd_data), "Rx Caps: ");
2057 
2058 	return true;
2059 }
2060 
detect_dp_sink_caps(struct dc_link * link)2061 bool detect_dp_sink_caps(struct dc_link *link)
2062 {
2063 	return retrieve_link_cap(link);
2064 }
2065 
detect_edp_sink_caps(struct dc_link * link)2066 void detect_edp_sink_caps(struct dc_link *link)
2067 {
2068 	uint8_t supported_link_rates[16];
2069 	uint32_t entry;
2070 	uint32_t link_rate_in_khz;
2071 	enum dc_link_rate link_rate = LINK_RATE_UNKNOWN;
2072 	uint8_t backlight_adj_cap = 0;
2073 	uint8_t general_edp_cap = 0;
2074 
2075 	retrieve_link_cap(link);
2076 	link->dpcd_caps.edp_supported_link_rates_count = 0;
2077 	memset(supported_link_rates, 0, sizeof(supported_link_rates));
2078 
2079 	/*
2080 	 * edp_supported_link_rates_count is only valid for eDP v1.4 or higher.
2081 	 * Per VESA eDP spec, "The DPCD revision for eDP v1.4 is 13h"
2082 	 */
2083 	if (link->dpcd_caps.dpcd_rev.raw >= DPCD_REV_13) {
2084 		// Read DPCD 00010h - 0001Fh 16 bytes at one shot
2085 		core_link_read_dpcd(link, DP_SUPPORTED_LINK_RATES,
2086 							supported_link_rates, sizeof(supported_link_rates));
2087 
2088 		for (entry = 0; entry < 16; entry += 2) {
2089 			// DPCD register reports per-lane link rate = 16-bit link rate capability
2090 			// value X 200 kHz. Need multiplier to find link rate in kHz.
2091 			link_rate_in_khz = (supported_link_rates[entry+1] * 0x100 +
2092 										supported_link_rates[entry]) * 200;
2093 
2094 			DC_LOG_DC("%s: eDP v1.4 supported sink rates: [%d] %d kHz\n", __func__,
2095 				  entry / 2, link_rate_in_khz);
2096 
2097 			if (link_rate_in_khz != 0) {
2098 				link_rate = linkRateInKHzToLinkRateMultiplier(link_rate_in_khz);
2099 				link->dpcd_caps.edp_supported_link_rates[link->dpcd_caps.edp_supported_link_rates_count] = link_rate;
2100 				link->dpcd_caps.edp_supported_link_rates_count++;
2101 			}
2102 		}
2103 	}
2104 
2105 	core_link_read_dpcd(link, DP_EDP_BACKLIGHT_ADJUSTMENT_CAP,
2106 						&backlight_adj_cap, sizeof(backlight_adj_cap));
2107 
2108 	link->dpcd_caps.dynamic_backlight_capable_edp =
2109 				(backlight_adj_cap & DP_EDP_DYNAMIC_BACKLIGHT_CAP) ? true:false;
2110 
2111 	core_link_read_dpcd(link, DP_EDP_GENERAL_CAP_1,
2112 						&general_edp_cap, sizeof(general_edp_cap));
2113 
2114 	link->dpcd_caps.set_power_state_capable_edp =
2115 				(general_edp_cap & DP_EDP_SET_POWER_CAP) ? true:false;
2116 
2117 	set_default_brightness_aux(link);
2118 
2119 	core_link_read_dpcd(link, DP_EDP_DPCD_REV,
2120 		&link->dpcd_caps.edp_rev,
2121 		sizeof(link->dpcd_caps.edp_rev));
2122 	/*
2123 	 * PSR is only valid for eDP v1.3 or higher.
2124 	 */
2125 	if (link->dpcd_caps.edp_rev >= DP_EDP_13) {
2126 		core_link_read_dpcd(link, DP_PSR_SUPPORT,
2127 			&link->dpcd_caps.psr_info.psr_version,
2128 			sizeof(link->dpcd_caps.psr_info.psr_version));
2129 		if (link->dpcd_caps.sink_dev_id == DP_BRANCH_DEVICE_ID_001CF8)
2130 			core_link_read_dpcd(link, DP_FORCE_PSRSU_CAPABILITY,
2131 						&link->dpcd_caps.psr_info.force_psrsu_cap,
2132 						sizeof(link->dpcd_caps.psr_info.force_psrsu_cap));
2133 		core_link_read_dpcd(link, DP_PSR_CAPS,
2134 			&link->dpcd_caps.psr_info.psr_dpcd_caps.raw,
2135 			sizeof(link->dpcd_caps.psr_info.psr_dpcd_caps.raw));
2136 		if (link->dpcd_caps.psr_info.psr_dpcd_caps.bits.Y_COORDINATE_REQUIRED) {
2137 			core_link_read_dpcd(link, DP_PSR2_SU_Y_GRANULARITY,
2138 				&link->dpcd_caps.psr_info.psr2_su_y_granularity_cap,
2139 				sizeof(link->dpcd_caps.psr_info.psr2_su_y_granularity_cap));
2140 		}
2141 	}
2142 
2143 	/*
2144 	 * ALPM is only valid for eDP v1.4 or higher.
2145 	 */
2146 	if (link->dpcd_caps.dpcd_rev.raw >= DP_EDP_14)
2147 		core_link_read_dpcd(link, DP_RECEIVER_ALPM_CAP,
2148 			&link->dpcd_caps.alpm_caps.raw,
2149 			sizeof(link->dpcd_caps.alpm_caps.raw));
2150 
2151 	/*
2152 	 * Read REPLAY info
2153 	 */
2154 	core_link_read_dpcd(link, DP_SINK_PR_PIXEL_DEVIATION_PER_LINE,
2155 			&link->dpcd_caps.pr_info.pixel_deviation_per_line,
2156 			sizeof(link->dpcd_caps.pr_info.pixel_deviation_per_line));
2157 	core_link_read_dpcd(link, DP_SINK_PR_MAX_NUMBER_OF_DEVIATION_LINE,
2158 			&link->dpcd_caps.pr_info.max_deviation_line,
2159 			sizeof(link->dpcd_caps.pr_info.max_deviation_line));
2160 
2161 	/*
2162 	 * OLED Emission Rate info
2163 	 */
2164 	if (link->dpcd_sink_ext_caps.bits.emission_output)
2165 		core_link_read_dpcd(link, DP_SINK_EMISSION_RATE,
2166 				(uint8_t *)&link->dpcd_caps.edp_oled_emission_rate,
2167 				sizeof(link->dpcd_caps.edp_oled_emission_rate));
2168 
2169 	/*
2170 	 * Read Multi-SST (Single Stream Transport) capability
2171 	 * for eDP version 1.4 or higher.
2172 	 */
2173 	if (link->dpcd_caps.dpcd_rev.raw >= DP_EDP_14)
2174 		core_link_read_dpcd(
2175 			link,
2176 			DP_EDP_MSO_LINK_CAPABILITIES,
2177 			(uint8_t *)&link->dpcd_caps.mso_cap_sst_links_supported,
2178 			sizeof(link->dpcd_caps.mso_cap_sst_links_supported));
2179 }
2180 
dp_get_max_link_enc_cap(const struct dc_link * link,struct dc_link_settings * max_link_enc_cap)2181 bool dp_get_max_link_enc_cap(const struct dc_link *link, struct dc_link_settings *max_link_enc_cap)
2182 {
2183 	struct resource_context *res_ctx = &link->dc->current_state->res_ctx;
2184 	struct resource_pool *res_pool = link->dc->res_pool;
2185 	struct link_encoder *link_enc = get_temp_dio_link_enc(res_ctx, res_pool, link);
2186 
2187 	if (!max_link_enc_cap) {
2188 		DC_LOG_ERROR("%s: Could not return max link encoder caps", __func__);
2189 		return false;
2190 	}
2191 
2192 	if (!link->dc->config.unify_link_enc_assignment)
2193 		link_enc = link_enc_cfg_get_link_enc(link);
2194 	ASSERT(link_enc);
2195 
2196 	if (link_enc && link_enc->funcs->get_max_link_cap) {
2197 		link_enc->funcs->get_max_link_cap(link_enc, max_link_enc_cap);
2198 		return true;
2199 	}
2200 
2201 	DC_LOG_ERROR("%s: Max link encoder caps unknown", __func__);
2202 	max_link_enc_cap->lane_count = 1;
2203 	max_link_enc_cap->link_rate = 6;
2204 	return false;
2205 }
2206 
dp_get_verified_link_cap(const struct dc_link * link)2207 const struct dc_link_settings *dp_get_verified_link_cap(
2208 		const struct dc_link *link)
2209 {
2210 	if (link->preferred_link_setting.lane_count != LANE_COUNT_UNKNOWN &&
2211 			link->preferred_link_setting.link_rate != LINK_RATE_UNKNOWN)
2212 		return &link->preferred_link_setting;
2213 	return &link->verified_link_cap;
2214 }
2215 
dp_get_max_link_cap(struct dc_link * link)2216 struct dc_link_settings dp_get_max_link_cap(struct dc_link *link)
2217 {
2218 	struct dc_link_settings max_link_cap = {0};
2219 	enum dc_link_rate lttpr_max_link_rate;
2220 	enum dc_link_rate cable_max_link_rate;
2221 	struct resource_context *res_ctx = &link->dc->current_state->res_ctx;
2222 	struct resource_pool *res_pool = link->dc->res_pool;
2223 	struct link_encoder *link_enc = get_temp_dio_link_enc(res_ctx, res_pool, link);
2224 	bool is_uhbr13_5_supported = true;
2225 
2226 	if (!link->dc->config.unify_link_enc_assignment)
2227 		link_enc = link_enc_cfg_get_link_enc(link);
2228 	ASSERT(link_enc);
2229 
2230 	/* get max link encoder capability */
2231 	if (link_enc)
2232 		link_enc->funcs->get_max_link_cap(link_enc, &max_link_cap);
2233 
2234 	/* Lower link settings based on sink's link cap */
2235 	if (link->reported_link_cap.lane_count < max_link_cap.lane_count)
2236 		max_link_cap.lane_count =
2237 				link->reported_link_cap.lane_count;
2238 	if (link->reported_link_cap.link_rate < max_link_cap.link_rate)
2239 		max_link_cap.link_rate =
2240 				link->reported_link_cap.link_rate;
2241 	if (link->reported_link_cap.link_spread <
2242 			max_link_cap.link_spread)
2243 		max_link_cap.link_spread =
2244 				link->reported_link_cap.link_spread;
2245 
2246 	if (!link->dpcd_caps.dp_128b_132b_supported_link_rates.bits.UHBR13_5)
2247 		is_uhbr13_5_supported = false;
2248 
2249 	/* Lower link settings based on cable attributes
2250 	 * Cable ID is a DP2 feature to identify max certified link rate that
2251 	 * a cable can carry. The cable identification method requires both
2252 	 * cable and display hardware support. Since the specs comes late, it is
2253 	 * anticipated that the first round of DP2 cables and displays may not
2254 	 * be fully compatible to reliably return cable ID data. Therefore the
2255 	 * decision of our cable id policy is that if the cable can return non
2256 	 * zero cable id data, we will take cable's link rate capability into
2257 	 * account. However if we get zero data, the cable link rate capability
2258 	 * is considered inconclusive. In this case, we will not take cable's
2259 	 * capability into account to avoid of over limiting hardware capability
2260 	 * from users. The max overall link rate capability is still determined
2261 	 * after actual dp pre-training. Cable id is considered as an auxiliary
2262 	 * method of determining max link bandwidth capability.
2263 	 */
2264 	cable_max_link_rate = get_cable_max_link_rate(link);
2265 
2266 	if (!link->dc->debug.ignore_cable_id &&
2267 			cable_max_link_rate != LINK_RATE_UNKNOWN) {
2268 		if (cable_max_link_rate < max_link_cap.link_rate)
2269 			max_link_cap.link_rate = cable_max_link_rate;
2270 
2271 		if (!link->dpcd_caps.cable_id.bits.UHBR13_5_CAPABILITY &&
2272 				link->dpcd_caps.cable_id.bits.CABLE_TYPE >= 2)
2273 			is_uhbr13_5_supported = false;
2274 	}
2275 
2276 	/* account for lttpr repeaters cap
2277 	 * notes: repeaters do not snoop in the DPRX Capabilities addresses (3.6.3).
2278 	 */
2279 	if (dp_is_lttpr_present(link)) {
2280 
2281 		/* Some LTTPR devices do not report valid DPCD revisions, if so, do not take it's link cap into consideration. */
2282 		if (link->dpcd_caps.lttpr_caps.revision.raw >= DPCD_REV_14) {
2283 			if (link->dpcd_caps.lttpr_caps.max_lane_count < max_link_cap.lane_count)
2284 				max_link_cap.lane_count = link->dpcd_caps.lttpr_caps.max_lane_count;
2285 			lttpr_max_link_rate = get_lttpr_max_link_rate(link);
2286 
2287 			if (lttpr_max_link_rate < max_link_cap.link_rate)
2288 				max_link_cap.link_rate = lttpr_max_link_rate;
2289 
2290 			if (!link->dpcd_caps.lttpr_caps.supported_128b_132b_rates.bits.UHBR13_5)
2291 				is_uhbr13_5_supported = false;
2292 		}
2293 
2294 		DC_LOG_HW_LINK_TRAINING("%s\n Training with LTTPR,  max_lane count %d max_link rate %d \n",
2295 						__func__,
2296 						max_link_cap.lane_count,
2297 						max_link_cap.link_rate);
2298 	}
2299 
2300 	if (max_link_cap.link_rate == LINK_RATE_UHBR13_5 &&
2301 			!is_uhbr13_5_supported)
2302 		max_link_cap.link_rate = LINK_RATE_UHBR10;
2303 
2304 	if (link_dp_get_encoding_format(&max_link_cap) == DP_128b_132b_ENCODING &&
2305 			link->dc->debug.disable_uhbr)
2306 		max_link_cap.link_rate = LINK_RATE_HIGH3;
2307 
2308 	return max_link_cap;
2309 }
2310 
dp_verify_link_cap(struct dc_link * link,struct dc_link_settings * known_limit_link_setting,int * fail_count)2311 static bool dp_verify_link_cap(
2312 	struct dc_link *link,
2313 	struct dc_link_settings *known_limit_link_setting,
2314 	int *fail_count)
2315 {
2316 	struct dc_link_settings cur_link_settings = {0};
2317 	struct dc_link_settings max_link_settings = *known_limit_link_setting;
2318 	bool success = false;
2319 	bool skip_video_pattern;
2320 	enum clock_source_id dp_cs_id = get_clock_source_id(link);
2321 	enum link_training_result status = LINK_TRAINING_SUCCESS;
2322 	union hpd_irq_data irq_data;
2323 	struct link_resource link_res;
2324 
2325 	memset(&irq_data, 0, sizeof(irq_data));
2326 	cur_link_settings = max_link_settings;
2327 
2328 	/* Grant extended timeout request */
2329 	if (dp_is_lttpr_present(link) && link->dpcd_caps.lttpr_caps.max_ext_timeout > 0) {
2330 		uint8_t grant = link->dpcd_caps.lttpr_caps.max_ext_timeout & 0x80;
2331 
2332 		core_link_write_dpcd(link, DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT, &grant, sizeof(grant));
2333 	}
2334 
2335 	do {
2336 		if (!get_temp_dp_link_res(link, &link_res, &cur_link_settings))
2337 			continue;
2338 
2339 		skip_video_pattern = cur_link_settings.link_rate != LINK_RATE_LOW;
2340 		dp_enable_link_phy(
2341 				link,
2342 				&link_res,
2343 				link->connector_signal,
2344 				dp_cs_id,
2345 				&cur_link_settings);
2346 
2347 		status = dp_perform_link_training(
2348 				link,
2349 				&link_res,
2350 				&cur_link_settings,
2351 				skip_video_pattern);
2352 
2353 		if (status == LINK_TRAINING_SUCCESS) {
2354 			success = true;
2355 			fsleep(1000);
2356 			if (dp_read_hpd_rx_irq_data(link, &irq_data) == DC_OK &&
2357 					dp_parse_link_loss_status(
2358 							link,
2359 							&irq_data))
2360 				(*fail_count)++;
2361 		} else if (status == LINK_TRAINING_LINK_LOSS) {
2362 			success = true;
2363 			(*fail_count)++;
2364 		} else {
2365 			(*fail_count)++;
2366 		}
2367 		dp_trace_lt_total_count_increment(link, true);
2368 		dp_trace_lt_result_update(link, status, true);
2369 		dp_disable_link_phy(link, &link_res, link->connector_signal);
2370 	} while (!success && decide_fallback_link_setting(link,
2371 			&max_link_settings, &cur_link_settings, status));
2372 
2373 	link->verified_link_cap = success ?
2374 			cur_link_settings : fail_safe_link_settings;
2375 	return success;
2376 }
2377 
dp_verify_link_cap_with_retries(struct dc_link * link,struct dc_link_settings * known_limit_link_setting,int attempts)2378 bool dp_verify_link_cap_with_retries(
2379 	struct dc_link *link,
2380 	struct dc_link_settings *known_limit_link_setting,
2381 	int attempts)
2382 {
2383 	int i = 0;
2384 	bool success = false;
2385 	int fail_count = 0;
2386 	struct dc_link_settings last_verified_link_cap = fail_safe_link_settings;
2387 
2388 	dp_trace_detect_lt_init(link);
2389 
2390 	if (link->link_enc && link->link_enc->features.flags.bits.DP_IS_USB_C &&
2391 			link->dc->debug.usbc_combo_phy_reset_wa)
2392 		apply_usbc_combo_phy_reset_wa(link, known_limit_link_setting);
2393 
2394 	dp_trace_set_lt_start_timestamp(link, false);
2395 	for (i = 0; i < attempts; i++) {
2396 		enum dc_connection_type type = dc_connection_none;
2397 
2398 		memset(&link->verified_link_cap, 0,
2399 				sizeof(struct dc_link_settings));
2400 		if (link->link_enc && (!link_detect_connection_type(link, &type) || type == dc_connection_none)) {
2401 			link->verified_link_cap = fail_safe_link_settings;
2402 			break;
2403 		} else if (dp_verify_link_cap(link, known_limit_link_setting, &fail_count)) {
2404 			last_verified_link_cap = link->verified_link_cap;
2405 			if (fail_count == 0) {
2406 				success = true;
2407 				break;
2408 			}
2409 		} else {
2410 			link->verified_link_cap = last_verified_link_cap;
2411 		}
2412 
2413 		/* For Dp tunneling link, a pending HPD means that we have a race condition between processing
2414 		 * current link and processing the pending HPD. Since the training is failed, we should just brak
2415 		 * the loop so that we have chance to process the pending HPD.
2416 		 */
2417 		if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA && link->is_hpd_pending)
2418 			break;
2419 
2420 		fsleep(10 * 1000);
2421 	}
2422 
2423 	dp_trace_lt_fail_count_update(link, fail_count, true);
2424 	dp_trace_set_lt_end_timestamp(link, true);
2425 
2426 	return success;
2427 }
2428 
2429 /*
2430  * Check if there is a native DP or passive DP-HDMI dongle connected
2431  */
dp_is_sink_present(struct dc_link * link)2432 bool dp_is_sink_present(struct dc_link *link)
2433 {
2434 	enum gpio_result gpio_result;
2435 	uint32_t clock_pin = 0;
2436 	uint8_t retry = 0;
2437 	struct ddc *ddc;
2438 
2439 	enum connector_id connector_id =
2440 		dal_graphics_object_id_get_connector_id(link->link_id);
2441 
2442 	bool present =
2443 		((connector_id == CONNECTOR_ID_DISPLAY_PORT) ||
2444 		(connector_id == CONNECTOR_ID_EDP) ||
2445 		(connector_id == CONNECTOR_ID_USBC));
2446 
2447 	ddc = get_ddc_pin(link->ddc);
2448 
2449 	if (!ddc) {
2450 		BREAK_TO_DEBUGGER();
2451 		return present;
2452 	}
2453 
2454 	/* Open GPIO and set it to I2C mode */
2455 	/* Note: this GpioMode_Input will be converted
2456 	 * to GpioConfigType_I2cAuxDualMode in GPIO component,
2457 	 * which indicates we need additional delay
2458 	 */
2459 
2460 	if (dal_ddc_open(ddc, GPIO_MODE_INPUT,
2461 			 GPIO_DDC_CONFIG_TYPE_MODE_I2C) != GPIO_RESULT_OK) {
2462 		dal_ddc_close(ddc);
2463 
2464 		return present;
2465 	}
2466 
2467 	/*
2468 	 * Read GPIO: DP sink is present if both clock and data pins are zero
2469 	 *
2470 	 * [W/A] plug-unplug DP cable, sometimes customer board has
2471 	 * one short pulse on clk_pin(1V, < 1ms). DP will be config to HDMI/DVI
2472 	 * then monitor can't br light up. Add retry 3 times
2473 	 * But in real passive dongle, it need additional 3ms to detect
2474 	 */
2475 	do {
2476 		gpio_result = dal_gpio_get_value(ddc->pin_clock, &clock_pin);
2477 		ASSERT(gpio_result == GPIO_RESULT_OK);
2478 		if (clock_pin)
2479 			fsleep(1000);
2480 		else
2481 			break;
2482 	} while (retry++ < 3);
2483 
2484 	present = (gpio_result == GPIO_RESULT_OK) && !clock_pin;
2485 
2486 	dal_ddc_close(ddc);
2487 
2488 	return present;
2489 }
2490