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