xref: /linux/drivers/gpu/drm/amd/display/dc/link/link_detection.c (revision 44ccd294f3fa7b216a1399e9700464efedd342ff)
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 manages link detection states and receiver states by using various
28  * link protocols. It also provides helper functions to interpret certain
29  * capabilities or status based on the states it manages or retrieve them
30  * directly from connected receivers.
31  */
32 
33 #include "link_dpms.h"
34 #include "link_detection.h"
35 #include "link_hwss.h"
36 #include "protocols/link_edp_panel_control.h"
37 #include "protocols/link_ddc.h"
38 #include "protocols/link_hpd.h"
39 #include "protocols/link_dpcd.h"
40 #include "protocols/link_dp_capability.h"
41 #include "protocols/link_dp_dpia.h"
42 #include "protocols/link_dp_phy.h"
43 #include "protocols/link_dp_training.h"
44 #include "protocols/link_hdmi_frl.h"
45 #include "protocols/link_dp_dpia_bw.h"
46 #include "accessories/link_dp_trace.h"
47 
48 #include "link_enc_cfg.h"
49 #include "dm_helpers.h"
50 #include "clk_mgr.h"
51 
52  // Offset DPCD 050Eh == 0x5A
53 #define MST_HUB_ID_0x5A  0x5A
54 
55 #define DC_LOGGER \
56 	link->ctx->logger
57 #define DC_LOGGER_INIT(logger)
58 
59 #define LINK_INFO(...) \
60 	DC_LOG_HW_HOTPLUG(  \
61 		__VA_ARGS__)
62 /*
63  * Some receivers fail to train on first try and are good
64  * on subsequent tries. 2 retries should be plenty. If we
65  * don't have a successful training then we don't expect to
66  * ever get one.
67  */
68 #define LINK_TRAINING_MAX_VERIFY_RETRY 2
69 
70 static const u8 DP_SINK_BRANCH_DEV_NAME_7580[] = "7580\x80u";
71 
72 static const u8 dp_hdmi_dongle_signature_str[] = "DP-HDMI ADAPTOR";
73 
74 static enum ddc_transaction_type get_ddc_transaction_type(enum signal_type sink_signal)
75 {
76 	enum ddc_transaction_type transaction_type = DDC_TRANSACTION_TYPE_NONE;
77 
78 	switch (sink_signal) {
79 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
80 	case SIGNAL_TYPE_DVI_DUAL_LINK:
81 	case SIGNAL_TYPE_HDMI_TYPE_A:
82 	case SIGNAL_TYPE_HDMI_FRL:
83 	case SIGNAL_TYPE_LVDS:
84 	case SIGNAL_TYPE_RGB:
85 		transaction_type = DDC_TRANSACTION_TYPE_I2C;
86 		break;
87 
88 	case SIGNAL_TYPE_DISPLAY_PORT:
89 	case SIGNAL_TYPE_EDP:
90 		transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
91 		break;
92 
93 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
94 		/* MST does not use I2COverAux, but there is the
95 		 * SPECIAL use case for "immediate dwnstrm device
96 		 * access" (EPR#370830).
97 		 */
98 		transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
99 		break;
100 
101 	default:
102 		break;
103 	}
104 
105 	return transaction_type;
106 }
107 
108 static enum signal_type get_basic_signal_type(struct graphics_object_id encoder,
109 					      struct graphics_object_id downstream)
110 {
111 	if (downstream.type == OBJECT_TYPE_CONNECTOR) {
112 		switch (downstream.id) {
113 		case CONNECTOR_ID_SINGLE_LINK_DVII:
114 			switch (encoder.id) {
115 			case ENCODER_ID_INTERNAL_DAC1:
116 			case ENCODER_ID_INTERNAL_KLDSCP_DAC1:
117 			case ENCODER_ID_INTERNAL_DAC2:
118 			case ENCODER_ID_INTERNAL_KLDSCP_DAC2:
119 				return SIGNAL_TYPE_RGB;
120 			default:
121 				return SIGNAL_TYPE_DVI_SINGLE_LINK;
122 			}
123 		break;
124 		case CONNECTOR_ID_DUAL_LINK_DVII:
125 		{
126 			switch (encoder.id) {
127 			case ENCODER_ID_INTERNAL_DAC1:
128 			case ENCODER_ID_INTERNAL_KLDSCP_DAC1:
129 			case ENCODER_ID_INTERNAL_DAC2:
130 			case ENCODER_ID_INTERNAL_KLDSCP_DAC2:
131 				return SIGNAL_TYPE_RGB;
132 			default:
133 				return SIGNAL_TYPE_DVI_DUAL_LINK;
134 			}
135 		}
136 		break;
137 		case CONNECTOR_ID_SINGLE_LINK_DVID:
138 			return SIGNAL_TYPE_DVI_SINGLE_LINK;
139 		case CONNECTOR_ID_DUAL_LINK_DVID:
140 			return SIGNAL_TYPE_DVI_DUAL_LINK;
141 		case CONNECTOR_ID_VGA:
142 			return SIGNAL_TYPE_RGB;
143 		case CONNECTOR_ID_HDMI_TYPE_A:
144 			return SIGNAL_TYPE_HDMI_TYPE_A;
145 		case CONNECTOR_ID_LVDS:
146 			return SIGNAL_TYPE_LVDS;
147 		case CONNECTOR_ID_DISPLAY_PORT:
148 		case CONNECTOR_ID_USBC:
149 			return SIGNAL_TYPE_DISPLAY_PORT;
150 		case CONNECTOR_ID_EDP:
151 			return SIGNAL_TYPE_EDP;
152 		default:
153 			return SIGNAL_TYPE_NONE;
154 		}
155 	}
156 
157 	return SIGNAL_TYPE_NONE;
158 }
159 
160 /*
161  * @brief
162  * Detect output sink type
163  */
164 static enum signal_type link_detect_sink_signal_type(struct dc_link *link,
165 					 enum dc_detect_reason reason)
166 {
167 	enum signal_type result;
168 	struct audio_support *aud_support;
169 	struct graphics_object_id enc_id;
170 
171 	/* External DP bridges should use DP signal regardless of connector type. */
172 	if (link->ext_enc_id.id)
173 		return SIGNAL_TYPE_DISPLAY_PORT;
174 
175 	if (link->is_dig_mapping_flexible)
176 		enc_id = (struct graphics_object_id){.id = ENCODER_ID_UNKNOWN};
177 	else
178 		enc_id = link->link_enc->id;
179 	result = get_basic_signal_type(enc_id, link->link_id);
180 
181 	/* Use basic signal type for link without physical connector. */
182 	if (link->ep_type != DISPLAY_ENDPOINT_PHY)
183 		return result;
184 
185 	/*
186 	 * Internal digital encoder will detect only dongles
187 	 * that require digital signal
188 	 */
189 
190 	/*
191 	 * Detection mechanism is different
192 	 * for different native connectors.
193 	 * LVDS connector supports only LVDS signal;
194 	 * PCIE is a bus slot, the actual connector needs to be detected first;
195 	 * eDP connector supports only eDP signal;
196 	 * HDMI should check straps for audio
197 	 */
198 	switch (link->link_id.id) {
199 	case CONNECTOR_ID_HDMI_TYPE_A:
200 		/*
201 		 * check audio support:
202 		 * if native HDMI is not supported, switch to DVI
203 		 */
204 		aud_support = &link->dc->res_pool->audio_support;
205 
206 		if (!aud_support->hdmi_audio_native)
207 			result = SIGNAL_TYPE_DVI_SINGLE_LINK;
208 		break;
209 	case CONNECTOR_ID_DISPLAY_PORT:
210 	case CONNECTOR_ID_USBC:
211 		/*
212 		 * DP HPD short pulse. Passive DP dongle will not
213 		 * have short pulse
214 		 */
215 		if (reason != DETECT_REASON_HPDRX) {
216 			/*
217 			 * Check whether DP signal detected: if not -
218 			 * we assume signal is DVI; it could be corrected
219 			 * to HDMI after dongle detection
220 			 */
221 			if (!dm_helpers_is_dp_sink_present(link))
222 				result = SIGNAL_TYPE_DVI_SINGLE_LINK;
223 		}
224 		break;
225 	case CONNECTOR_ID_PCIE:
226 		/* ZAZTODO implement PCIE add-on card detection */
227 		break;
228 	default:
229 		break;
230 	}
231 
232 	return result;
233 }
234 
235 static enum signal_type decide_signal_from_strap_and_dongle_type(enum display_dongle_type dongle_type,
236 								 struct audio_support *audio_support)
237 {
238 	enum signal_type signal = SIGNAL_TYPE_NONE;
239 
240 	switch (dongle_type) {
241 	case DISPLAY_DONGLE_DP_HDMI_DONGLE:
242 		if (audio_support->hdmi_audio_on_dongle)
243 			signal = SIGNAL_TYPE_HDMI_TYPE_A;
244 		else
245 			signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
246 		break;
247 	case DISPLAY_DONGLE_DP_DVI_DONGLE:
248 		signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
249 		break;
250 	case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
251 		if (audio_support->hdmi_audio_native)
252 			signal =  SIGNAL_TYPE_HDMI_TYPE_A;
253 		else
254 			signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
255 		break;
256 	default:
257 		signal = SIGNAL_TYPE_NONE;
258 		break;
259 	}
260 
261 	return signal;
262 }
263 
264 static void read_scdc_caps(struct ddc_service *ddc_service,
265 		struct dc_sink *sink)
266 {
267 	uint8_t slave_address = HDMI_SCDC_ADDRESS;
268 	uint8_t offset = HDMI_SCDC_MANUFACTURER_OUI;
269 
270 	if (ddc_service->link->local_sink &&
271 		!ddc_service->link->local_sink->edid_caps.scdc_present)
272 		return;
273 
274 	link_query_ddc_data(ddc_service, slave_address, &offset,
275 			sizeof(offset), sink->scdc_caps.manufacturer_OUI.byte,
276 			sizeof(sink->scdc_caps.manufacturer_OUI.byte));
277 
278 	offset = HDMI_SCDC_DEVICE_ID;
279 
280 	link_query_ddc_data(ddc_service, slave_address, &offset,
281 			sizeof(offset), &(sink->scdc_caps.device_id.byte),
282 			sizeof(sink->scdc_caps.device_id.byte));
283 }
284 
285 static bool i2c_read(
286 	struct ddc_service *ddc,
287 	uint32_t address,
288 	uint8_t *buffer,
289 	uint32_t len)
290 {
291 	uint8_t offs_data = 0;
292 	struct i2c_payload payloads[2] = {
293 		{
294 		.write = true,
295 		.address = (uint8_t)address,
296 		.length = 1,
297 		.data = &offs_data },
298 		{
299 		.write = false,
300 		.address = (uint8_t)address,
301 		.length = len,
302 		.data = buffer } };
303 
304 	struct i2c_command command = {
305 		.payloads = payloads,
306 		.number_of_payloads = 2,
307 		.engine = DDC_I2C_COMMAND_ENGINE,
308 		.speed = ddc->ctx->dc->caps.i2c_speed_in_khz };
309 
310 	return dm_helpers_submit_i2c(
311 			ddc->ctx,
312 			ddc->link,
313 			&command);
314 }
315 
316 enum {
317 	DP_SINK_CAP_SIZE =
318 		DP_EDP_CONFIGURATION_CAP - DP_DPCD_REV + 1
319 };
320 
321 static void query_dp_dual_mode_adaptor(
322 	struct ddc_service *ddc,
323 	struct display_sink_capability *sink_cap)
324 {
325 	uint8_t i;
326 	bool is_valid_hdmi_signature;
327 	enum display_dongle_type *dongle = &sink_cap->dongle_type;
328 	uint8_t type2_dongle_buf[DP_ADAPTOR_TYPE2_SIZE];
329 	bool is_type2_dongle = false;
330 	int retry_count = 2;
331 	struct dp_hdmi_dongle_signature_data *dongle_signature;
332 	struct dc_link *link = ddc->link;
333 
334 	/* Assume we have no valid DP passive dongle connected */
335 	*dongle = DISPLAY_DONGLE_NONE;
336 	sink_cap->max_hdmi_pixel_clock = DP_ADAPTOR_DVI_MAX_TMDS_CLK;
337 
338 	/* Read DP-HDMI dongle I2c (no response interpreted as DP-DVI dongle)*/
339 	if (link->force_to_use_aux) {
340 
341 		if (!dm_helpers_submit_i2c_over_aux(
342 			ddc,
343 			DP_HDMI_DONGLE_ADDRESS,
344 			0,
345 			type2_dongle_buf,
346 			sizeof(type2_dongle_buf),
347 			true)) {
348 
349 			/* Use same approach as below with native i2c - HDMI dongles can sometimes fail here without retrying*/
350 			while (retry_count > 0) {
351 				if (dm_helpers_submit_i2c_over_aux(
352 					ddc,
353 					DP_HDMI_DONGLE_ADDRESS,
354 					0,
355 					type2_dongle_buf,
356 					sizeof(type2_dongle_buf),
357 					true))
358 					break;
359 				retry_count--;
360 			}
361 			/* In case I2C over Aux no response, it is interpreted as DISPLAY_DONGLE_NONE */
362 			if (retry_count == 0) {
363 				return;
364 			}
365 		}
366 	} else {
367 	if (!i2c_read(
368 		ddc,
369 		DP_HDMI_DONGLE_ADDRESS,
370 		type2_dongle_buf,
371 		sizeof(type2_dongle_buf))) {
372 		/* Passive HDMI dongles can sometimes fail here without retrying*/
373 		while (retry_count > 0) {
374 			if (i2c_read(ddc,
375 				DP_HDMI_DONGLE_ADDRESS,
376 				type2_dongle_buf,
377 				sizeof(type2_dongle_buf)))
378 				break;
379 			retry_count--;
380 		}
381 		if (retry_count == 0) {
382 			*dongle = DISPLAY_DONGLE_DP_DVI_DONGLE;
383 			sink_cap->max_hdmi_pixel_clock = DP_ADAPTOR_DVI_MAX_TMDS_CLK;
384 
385 			CONN_DATA_DETECT(link, type2_dongle_buf, sizeof(type2_dongle_buf),
386 					"DP-DVI passive dongle %dMhz: ",
387 					DP_ADAPTOR_DVI_MAX_TMDS_CLK / 1000);
388 			return;
389 		}
390 	}
391 	}
392 
393 	/* Check if Type 2 dongle.*/
394 	if (type2_dongle_buf[DP_ADAPTOR_TYPE2_REG_ID] == DP_ADAPTOR_TYPE2_ID)
395 		is_type2_dongle = true;
396 
397 	dongle_signature =
398 		(struct dp_hdmi_dongle_signature_data *)type2_dongle_buf;
399 
400 	is_valid_hdmi_signature = true;
401 
402 	/* Check EOT */
403 	if (dongle_signature->eot != DP_HDMI_DONGLE_SIGNATURE_EOT) {
404 		is_valid_hdmi_signature = false;
405 	}
406 
407 	/* Check signature */
408 	for (i = 0; i < sizeof(dongle_signature->id); ++i) {
409 		/* If its not the right signature,
410 		 * skip mismatch in subversion byte.*/
411 		if (dongle_signature->id[i] !=
412 			dp_hdmi_dongle_signature_str[i] && i != 3) {
413 
414 			if (is_type2_dongle) {
415 				is_valid_hdmi_signature = false;
416 				break;
417 			}
418 
419 		}
420 	}
421 	if (is_valid_hdmi_signature)
422 		sink_cap->max_hdmi_pixel_clock = DP_ADAPTOR_HDMI_SAFE_MAX_TMDS_CLK;
423 
424 	if (is_type2_dongle) {
425 		uint32_t max_tmds_clk =
426 			type2_dongle_buf[DP_ADAPTOR_TYPE2_REG_MAX_TMDS_CLK];
427 
428 		max_tmds_clk = max_tmds_clk * 2 + max_tmds_clk / 2;
429 
430 		if (0 == max_tmds_clk ||
431 				max_tmds_clk < DP_ADAPTOR_TYPE2_MIN_TMDS_CLK ||
432 				max_tmds_clk > DP_ADAPTOR_TYPE2_MAX_TMDS_CLK) {
433 			*dongle = DISPLAY_DONGLE_DP_DVI_DONGLE;
434 
435 			CONN_DATA_DETECT(ddc->link, type2_dongle_buf,
436 					sizeof(type2_dongle_buf),
437 					"DP-DVI passive dongle %dMhz: ",
438 					DP_ADAPTOR_DVI_MAX_TMDS_CLK / 1000);
439 		} else {
440 			if (is_valid_hdmi_signature == true) {
441 				*dongle = DISPLAY_DONGLE_DP_HDMI_DONGLE;
442 
443 				CONN_DATA_DETECT(ddc->link, type2_dongle_buf,
444 						sizeof(type2_dongle_buf),
445 						"Type 2 DP-HDMI passive dongle %dMhz: ",
446 						max_tmds_clk);
447 			} else {
448 				*dongle = DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE;
449 
450 				CONN_DATA_DETECT(ddc->link, type2_dongle_buf,
451 						sizeof(type2_dongle_buf),
452 						"Type 2 DP-HDMI passive dongle (no signature) %dMhz: ",
453 						max_tmds_clk);
454 
455 			}
456 
457 			/* Multiply by 1000 to convert to kHz. */
458 			sink_cap->max_hdmi_pixel_clock =
459 				max_tmds_clk * 1000;
460 		}
461 		sink_cap->is_dongle_type_one = false;
462 
463 	} else {
464 		if (is_valid_hdmi_signature == true) {
465 			*dongle = DISPLAY_DONGLE_DP_HDMI_DONGLE;
466 
467 			CONN_DATA_DETECT(ddc->link, type2_dongle_buf,
468 					sizeof(type2_dongle_buf),
469 					"Type 1 DP-HDMI passive dongle %dMhz: ",
470 					sink_cap->max_hdmi_pixel_clock / 1000);
471 		} else {
472 			*dongle = DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE;
473 
474 			CONN_DATA_DETECT(ddc->link, type2_dongle_buf,
475 					sizeof(type2_dongle_buf),
476 					"Type 1 DP-HDMI passive dongle (no signature) %dMhz: ",
477 					sink_cap->max_hdmi_pixel_clock / 1000);
478 		}
479 		sink_cap->is_dongle_type_one = true;
480 	}
481 
482 	return;
483 }
484 
485 static enum signal_type dp_passive_dongle_detection(struct ddc_service *ddc,
486 						    struct display_sink_capability *sink_cap,
487 						    struct audio_support *audio_support)
488 {
489 	query_dp_dual_mode_adaptor(ddc, sink_cap);
490 
491 	return decide_signal_from_strap_and_dongle_type(sink_cap->dongle_type,
492 							audio_support);
493 }
494 
495 static void link_disconnect_sink(struct dc_link *link)
496 {
497 	if (link->local_sink) {
498 		dc_sink_release(link->local_sink);
499 		link->local_sink = NULL;
500 	}
501 
502 	link->dpcd_sink_count = 0;
503 	//link->dpcd_caps.dpcd_rev.raw = 0;
504 }
505 
506 static void link_disconnect_remap(struct dc_sink *prev_sink, struct dc_link *link)
507 {
508 	dc_sink_release(link->local_sink);
509 	link->local_sink = prev_sink;
510 }
511 
512 static void query_hdcp_capability(enum signal_type signal, struct dc_link *link)
513 {
514 	struct hdcp_protection_message msg22;
515 	struct hdcp_protection_message msg14;
516 
517 	memset(&msg22, 0, sizeof(struct hdcp_protection_message));
518 	memset(&msg14, 0, sizeof(struct hdcp_protection_message));
519 	memset(link->hdcp_caps.rx_caps.raw, 0,
520 		sizeof(link->hdcp_caps.rx_caps.raw));
521 
522 	if ((link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
523 			link->ddc->transaction_type ==
524 			DDC_TRANSACTION_TYPE_I2C_OVER_AUX) ||
525 			link->connector_signal == SIGNAL_TYPE_EDP) {
526 		msg22.data = link->hdcp_caps.rx_caps.raw;
527 		msg22.length = sizeof(link->hdcp_caps.rx_caps.raw);
528 		msg22.msg_id = HDCP_MESSAGE_ID_RX_CAPS;
529 		if (link->force_to_use_aux && (signal == SIGNAL_TYPE_HDMI_TYPE_A)) {
530 			// case with passive dongle with i2c over aux
531 			msg22.data = &link->hdcp_caps.rx_caps.fields.version;
532 			msg22.length = sizeof(link->hdcp_caps.rx_caps.fields.version);
533 			msg22.msg_id = HDCP_MESSAGE_ID_HDCP2VERSION;
534 		}
535 	} else {
536 		msg22.data = &link->hdcp_caps.rx_caps.fields.version;
537 		msg22.length = sizeof(link->hdcp_caps.rx_caps.fields.version);
538 		msg22.msg_id = HDCP_MESSAGE_ID_HDCP2VERSION;
539 	}
540 	msg22.version = HDCP_VERSION_22;
541 	msg22.link = HDCP_LINK_PRIMARY;
542 	msg22.max_retries = 5;
543 	dc_process_hdcp_msg(signal, link, &msg22);
544 
545 	if (signal == SIGNAL_TYPE_DISPLAY_PORT || signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
546 		msg14.data = &link->hdcp_caps.bcaps.raw;
547 		msg14.length = sizeof(link->hdcp_caps.bcaps.raw);
548 		msg14.msg_id = HDCP_MESSAGE_ID_READ_BCAPS;
549 		msg14.version = HDCP_VERSION_14;
550 		msg14.link = HDCP_LINK_PRIMARY;
551 		msg14.max_retries = 5;
552 
553 		dc_process_hdcp_msg(signal, link, &msg14);
554 	}
555 
556 }
557 static void read_current_link_settings_on_detect(struct dc_link *link)
558 {
559 	union lane_count_set lane_count_set = {0};
560 	uint8_t link_bw_set = 0;
561 	uint8_t link_rate_set = 0;
562 	uint32_t read_dpcd_retry_cnt = 10;
563 	enum dc_status status = DC_ERROR_UNEXPECTED;
564 	unsigned int i;
565 	union max_down_spread max_down_spread = {0};
566 
567 	// Read DPCD 00101h to find out the number of lanes currently set
568 	for (i = 0; i < read_dpcd_retry_cnt; i++) {
569 		status = core_link_read_dpcd(link,
570 					     DP_LANE_COUNT_SET,
571 					     &lane_count_set.raw,
572 					     sizeof(lane_count_set));
573 		/* First DPCD read after VDD ON can fail if the particular board
574 		 * does not have HPD pin wired correctly. So if DPCD read fails,
575 		 * which it should never happen, retry a few times. Target worst
576 		 * case scenario of 80 ms.
577 		 */
578 		if (status == DC_OK) {
579 			link->cur_link_settings.lane_count =
580 					lane_count_set.bits.LANE_COUNT_SET;
581 			break;
582 		}
583 
584 		msleep(8);
585 	}
586 
587 	// Read DPCD 00100h to find if standard link rates are set
588 	core_link_read_dpcd(link, DP_LINK_BW_SET,
589 			    &link_bw_set, sizeof(link_bw_set));
590 
591 	if (link_bw_set == 0) {
592 		if (link->connector_signal == SIGNAL_TYPE_EDP) {
593 			/* If standard link rates are not being used,
594 			 * Read DPCD 00115h to find the edp link rate set used
595 			 */
596 			core_link_read_dpcd(link, DP_LINK_RATE_SET,
597 					    &link_rate_set, sizeof(link_rate_set));
598 
599 			// edp_supported_link_rates_count = 0 for DP
600 			if (link_rate_set < link->dpcd_caps.edp_supported_link_rates_count) {
601 				link->cur_link_settings.link_rate =
602 					link->dpcd_caps.edp_supported_link_rates[link_rate_set];
603 				link->cur_link_settings.link_rate_set = link_rate_set;
604 				link->cur_link_settings.use_link_rate_set = true;
605 			}
606 		} else {
607 			// Link Rate not found. Seamless boot may not work.
608 			ASSERT(false);
609 		}
610 	} else {
611 		link->cur_link_settings.link_rate = link_bw_set;
612 		link->cur_link_settings.use_link_rate_set = false;
613 	}
614 	// Read DPCD 00003h to find the max down spread.
615 	core_link_read_dpcd(link, DP_MAX_DOWNSPREAD,
616 			    &max_down_spread.raw, sizeof(max_down_spread));
617 	link->cur_link_settings.link_spread =
618 		max_down_spread.bits.MAX_DOWN_SPREAD ?
619 		LINK_SPREAD_05_DOWNSPREAD_30KHZ : LINK_SPREAD_DISABLED;
620 }
621 
622 static bool detect_dp(struct dc_link *link,
623 		      struct display_sink_capability *sink_caps,
624 		      enum dc_detect_reason reason)
625 {
626 	struct audio_support *audio_support = &link->dc->res_pool->audio_support;
627 
628 	sink_caps->signal = link_detect_sink_signal_type(link, reason);
629 	sink_caps->transaction_type =
630 		get_ddc_transaction_type(sink_caps->signal);
631 
632 	if (sink_caps->transaction_type == DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
633 		sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT;
634 		if (!detect_dp_sink_caps(link)) {
635 			if (link->force_to_use_aux) {
636 				sink_caps->signal = dp_passive_dongle_detection(link->ddc, sink_caps, audio_support);
637 				link->dpcd_caps.dongle_type = sink_caps->dongle_type;
638 				link->dpcd_caps.is_dongle_type_one = sink_caps->is_dongle_type_one;
639 				link->dpcd_caps.dpcd_rev.raw = 0;
640 				/* Type 1 dongles do not work with I2C over Aux and also some of
641 				 * Type 2 dongles do not support I2C over Aux well, so for these
642 				 * cases when native Aux transactons fails and I2C over Aux fails
643 				 * report that nothing is connected, as we can't tell is it bad
644 				 * DP sink or bad passive dongle.
645 				 */
646 				if (sink_caps->dongle_type == DISPLAY_DONGLE_NONE)
647 					return false;
648 				else
649 					return true;
650 			} else {
651 				return false;
652 			}
653 		}
654 
655 		if (is_dp_branch_device(link))
656 			/* DP SST branch */
657 			link->type = dc_connection_sst_branch;
658 	} else {
659 		if (link->dc->debug.disable_dp_plus_plus_wa &&
660 				link->link_enc->features.flags.bits.IS_UHBR20_CAPABLE)
661 			return false;
662 
663 		/* DP passive dongles */
664 		sink_caps->signal = dp_passive_dongle_detection(link->ddc,
665 								sink_caps,
666 								audio_support);
667 		link->dpcd_caps.dongle_type = sink_caps->dongle_type;
668 		link->dpcd_caps.is_dongle_type_one = sink_caps->is_dongle_type_one;
669 		link->dpcd_caps.dpcd_rev.raw = 0;
670 		link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.raw = 0;
671 	}
672 
673 	if (link->ext_enc_id.id) {
674 		/* Fix number of connected sinks reported by external DP bridge */
675 		link->dpcd_caps.sink_count.bits.SINK_COUNT = 1;
676 		/* NUTMEG requires that we use HBR, doesn't work with RBR. */
677 		if (link->dpcd_caps.branch_dev_id == DP_BRANCH_DEVICE_ID_00001A)
678 			link->wa_flags.dp_skip_rbr = true;
679 	}
680 
681 	return true;
682 }
683 
684 static bool is_same_edid(struct dc_edid *old_edid, struct dc_edid *new_edid)
685 {
686 	if (old_edid == NULL || new_edid == NULL)
687 		return false;
688 
689 	if (old_edid->length != new_edid->length)
690 		return false;
691 
692 	if (new_edid->length == 0)
693 		return false;
694 
695 	return (memcmp(old_edid->raw_edid,
696 		       new_edid->raw_edid, new_edid->length) == 0);
697 }
698 
699 static bool wait_for_entering_dp_alt_mode(struct dc_link *link)
700 {
701 
702 	/**
703 	 * something is terribly wrong if time out is > 200ms. (5Hz)
704 	 * 500 microseconds * 400 tries us 200 ms
705 	 **/
706 	unsigned int sleep_time_in_microseconds = 500;
707 	unsigned int tries_allowed = 400;
708 	bool is_in_alt_mode;
709 	unsigned long long enter_timestamp;
710 	unsigned long long finish_timestamp;
711 	unsigned long long time_taken_in_ns;
712 	unsigned int tries_taken;
713 
714 	/**
715 	 * this function will only exist if we are on dcn21 (is_in_alt_mode is a
716 	 *  function pointer, so checking to see if it is equal to 0 is the same
717 	 *  as checking to see if it is null
718 	 **/
719 	if (!link->link_enc->funcs->is_in_alt_mode)
720 		return true;
721 
722 	is_in_alt_mode = link->link_enc->funcs->is_in_alt_mode(link->link_enc);
723 	DC_LOG_DC("DP Alt mode state on HPD: %d  Link=%d\n", is_in_alt_mode, link->link_index);
724 
725 	if (is_in_alt_mode)
726 		return true;
727 
728 	enter_timestamp = dm_get_timestamp(link->ctx);
729 
730 	for (tries_taken = 0; tries_taken < tries_allowed; tries_taken++) {
731 		udelay(sleep_time_in_microseconds);
732 		/* ask the link if alt mode is enabled, if so return ok */
733 		if (link->link_enc->funcs->is_in_alt_mode(link->link_enc)) {
734 			finish_timestamp = dm_get_timestamp(link->ctx);
735 			time_taken_in_ns =
736 				dm_get_elapse_time_in_ns(link->ctx,
737 							 finish_timestamp,
738 							 enter_timestamp);
739 			DC_LOG_WARNING("Alt mode entered finished after %llu ms\n",
740 				       div_u64(time_taken_in_ns, 1000000));
741 			return true;
742 		}
743 	}
744 	finish_timestamp = dm_get_timestamp(link->ctx);
745 	time_taken_in_ns = dm_get_elapse_time_in_ns(link->ctx, finish_timestamp,
746 						    enter_timestamp);
747 	DC_LOG_WARNING("Alt mode has timed out after %llu ms\n",
748 			div_u64(time_taken_in_ns, 1000000));
749 	return false;
750 }
751 
752 static void apply_dpia_mst_dsc_always_on_wa(struct dc_link *link)
753 {
754 	/* Apply work around for tunneled MST on certain USB4 docks. Always use DSC if dock
755 	 * reports DSC support.
756 	 */
757 	if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA &&
758 			link->type == dc_connection_mst_branch &&
759 			link->dpcd_caps.branch_dev_id == DP_BRANCH_DEVICE_ID_90CC24 &&
760 			link->dpcd_caps.branch_hw_revision == DP_BRANCH_HW_REV_20 &&
761 			link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_SUPPORT &&
762 			!link->dc->debug.dpia_debug.bits.disable_mst_dsc_work_around)
763 		link->wa_flags.dpia_mst_dsc_always_on = true;
764 
765 	if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA &&
766 		link->type == dc_connection_mst_branch &&
767 		link->dpcd_caps.branch_dev_id == DP_BRANCH_DEVICE_ID_90CC24 &&
768 		link->dpcd_caps.branch_vendor_specific_data[2] == MST_HUB_ID_0x5A &&
769 		link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_SUPPORT &&
770 		!link->dc->debug.dpia_debug.bits.disable_mst_dsc_work_around) {
771 			link->wa_flags.dpia_mst_dsc_always_on = true;
772 	}
773 }
774 
775 static void revert_dpia_mst_dsc_always_on_wa(struct dc_link *link)
776 {
777 	/* Disable work around which keeps DSC on for tunneled MST on certain USB4 docks. */
778 	if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA)
779 		link->wa_flags.dpia_mst_dsc_always_on = false;
780 }
781 
782 static bool discover_dp_mst_topology(struct dc_link *link, enum dc_detect_reason reason)
783 {
784 	LINK_INFO("link=%d, mst branch is now Connected\n",
785 		  link->link_index);
786 
787 	link->type = dc_connection_mst_branch;
788 	apply_dpia_mst_dsc_always_on_wa(link);
789 
790 	dm_helpers_dp_update_branch_info(link->ctx, link);
791 	if (dm_helpers_dp_mst_start_top_mgr(link->ctx,
792 			link, (reason == DETECT_REASON_BOOT || reason == DETECT_REASON_RESUMEFROMS3S4))) {
793 		link_disconnect_sink(link);
794 	} else {
795 		link->type = dc_connection_sst_branch;
796 	}
797 
798 	return link->type == dc_connection_mst_branch;
799 }
800 
801 bool link_reset_cur_dp_mst_topology(struct dc_link *link)
802 {
803 	LINK_INFO("link=%d, mst branch is now Disconnected\n",
804 		  link->link_index);
805 
806 	revert_dpia_mst_dsc_always_on_wa(link);
807 	return dm_helpers_dp_mst_stop_top_mgr(link->ctx, link);
808 }
809 
810 static bool should_prepare_phy_clocks_for_link_verification(const struct dc *dc,
811 		enum dc_detect_reason reason)
812 {
813 	int i;
814 	bool can_apply_seamless_boot = false;
815 
816 	for (i = 0; i < dc->current_state->stream_count; i++) {
817 		if (dc->current_state->streams[i]->apply_seamless_boot_optimization) {
818 			can_apply_seamless_boot = true;
819 			break;
820 		}
821 	}
822 
823 	return !can_apply_seamless_boot && reason != DETECT_REASON_BOOT;
824 }
825 
826 static bool is_hdmi_frl_in_use(struct dc_link *link)
827 {
828 	int i;
829 	unsigned int hdmi_conn_count = 0;
830 	unsigned int hdmi_stream_count = 0;
831 	bool hdmi_frl_in_use = false;
832 	bool incoming_link_identical = false;
833 
834 	/*Enumerate HDMI connector from all present links */
835 	for (i = 0; i < link->dc->link_count; i++) {
836 		if (link->dc->links[i] != NULL &&
837 				dc_is_hdmi_signal(link->dc->links[i]->connector_signal))
838 			hdmi_conn_count++;
839 	}
840 	/* If less than 2 HDMI Connector, assume HPO is always available*/
841 	if (hdmi_conn_count < 2)
842 		return false;
843 
844 	/*Enumerate existing HDMI stream count*/
845 	for (i = 0; i < link->dc->current_state->stream_count; i++) {
846 		if (dc_is_hdmi_signal(link->dc->current_state->streams[i]->signal))
847 			hdmi_stream_count++;
848 		if (link == link->dc->current_state->streams[i]->link &&
849 				(dc_is_hdmi_frl_signal(link->dc->current_state->streams[i]->signal)))
850 			incoming_link_identical = true;
851 	}
852 
853 	if (hdmi_stream_count > 1 || (hdmi_stream_count == 1 && !incoming_link_identical)) {
854 		for (i = 0; i < link->dc->current_state->stream_count; i++) {
855 			if (dc_is_hdmi_frl_signal(
856 					link->dc->current_state->streams[i]->signal)) {
857 				hdmi_frl_in_use = true;
858 				break;
859 			}
860 		}
861 	}
862 
863 	/* Check if previous link already has been assigned with FRL*/
864 	if (!hdmi_frl_in_use && !incoming_link_identical) {
865 		for (i = 0; i < link->dc->link_count; i++) {
866 			if (link->dc->links[i] != NULL &&
867 					link->dc->links[i]->local_sink != NULL &&
868 					link != link->dc->links[i] &&
869 					dc_is_hdmi_frl_signal(
870 							link->dc->links[i]->local_sink->sink_signal)) {
871 				hdmi_frl_in_use = true;
872 				break;
873 			}
874 		}
875 	}
876 
877 	return hdmi_frl_in_use;
878 }
879 
880 static void prepare_phy_clocks_for_destructive_link_verification(const struct dc *dc)
881 {
882 	dc_z10_restore(dc);
883 	clk_mgr_exit_optimized_pwr_state(dc, dc->clk_mgr);
884 }
885 
886 static void restore_phy_clocks_for_destructive_link_verification(const struct dc *dc)
887 {
888 	clk_mgr_optimize_pwr_state(dc, dc->clk_mgr);
889 }
890 
891 static void verify_link_capability_destructive(struct dc_link *link,
892 		enum dc_detect_reason reason)
893 {
894 	bool should_prepare_phy_clocks =
895 			should_prepare_phy_clocks_for_link_verification(link->dc, reason);
896 
897 	if (should_prepare_phy_clocks)
898 		prepare_phy_clocks_for_destructive_link_verification(link->dc);
899 
900 	if (dc_is_dp_signal(link->local_sink->sink_signal)) {
901 		struct dc_link_settings known_limit_link_setting =
902 				dp_get_max_link_cap(link);
903 		link_set_all_streams_dpms_off_for_link(link);
904 		dp_verify_link_cap_with_retries(
905 				link, &known_limit_link_setting,
906 				LINK_TRAINING_MAX_VERIFY_RETRY);
907 	} else if (dc_is_hdmi_signal(link->local_sink->sink_signal)) {
908 		if (!is_hdmi_frl_in_use(link)) {
909 			link_set_all_streams_dpms_off_for_link(link);
910 			hdmi_frl_verify_link_cap(link, &link->frl_reported_link_cap);
911 			link->local_sink->sink_signal = (link->frl_verified_link_cap.frl_link_rate != HDMI_FRL_LINK_RATE_DISABLE)
912 												? SIGNAL_TYPE_HDMI_FRL : SIGNAL_TYPE_HDMI_TYPE_A;
913 		} else {
914 			link->local_sink->sink_signal = SIGNAL_TYPE_HDMI_TYPE_A;
915 			link->frl_verified_link_cap.frl_link_rate = HDMI_FRL_LINK_RATE_DISABLE;
916 		}
917 	} else {
918 		ASSERT(0);
919 	}
920 
921 	if (should_prepare_phy_clocks)
922 		restore_phy_clocks_for_destructive_link_verification(link->dc);
923 }
924 
925 static void verify_link_capability_non_destructive(struct dc_link *link)
926 {
927 	if (dc_is_dp_signal(link->local_sink->sink_signal)) {
928 		if (dc_is_embedded_signal(link->local_sink->sink_signal) ||
929 				link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA)
930 			/* TODO - should we check link encoder's max link caps here?
931 			 * How do we know which link encoder to check from?
932 			 */
933 			link->verified_link_cap = link->reported_link_cap;
934 		else
935 			link->verified_link_cap = dp_get_max_link_cap(link);
936 	} else if (dc_is_hdmi_signal(link->local_sink->sink_signal)) {
937 		link->frl_verified_link_cap = link->frl_reported_link_cap;
938 
939 		if (is_hdmi_frl_in_use(link)) {
940 			link->local_sink->sink_signal = SIGNAL_TYPE_HDMI_TYPE_A;
941 			link->frl_verified_link_cap.frl_link_rate = HDMI_FRL_LINK_RATE_DISABLE;
942 		}
943 	}
944 }
945 
946 static bool should_verify_link_capability_destructively(struct dc_link *link,
947 		enum dc_detect_reason reason)
948 {
949 	(void)reason;
950 	bool destrictive = false;
951 	struct dc_link_settings max_link_cap;
952 	bool is_link_enc_unavailable = false;
953 
954 	if (!link->dc->config.unify_link_enc_assignment)
955 		is_link_enc_unavailable = link->link_enc &&
956 			link->dc->res_pool->funcs->link_encs_assign &&
957 			!link_enc_cfg_is_link_enc_avail(
958 					link->ctx->dc,
959 					link->link_enc->preferred_engine,
960 					link);
961 
962 	if (dc_is_dp_signal(link->local_sink->sink_signal)) {
963 		max_link_cap = dp_get_max_link_cap(link);
964 		destrictive = true;
965 
966 		if (link->dc->debug.skip_detection_link_training ||
967 				dc_is_embedded_signal(link->local_sink->sink_signal) ||
968 				(link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA &&
969 				!link->dc->config.enable_dpia_pre_training)) {
970 			destrictive = false;
971 		} else if (link_dp_get_encoding_format(&max_link_cap) ==
972 				DP_8b_10b_ENCODING) {
973 			if (link->dpcd_caps.is_mst_capable ||
974 					is_link_enc_unavailable) {
975 				destrictive = false;
976 			}
977 		}
978 	} else if (dc_is_hdmi_signal(link->local_sink->sink_signal) && link->link_enc &&
979 				link->link_enc->features.flags.bits.IS_HDMI_FRL_CAPABLE &&
980 				link->local_sink->edid_caps.max_frl_rate != 0) {
981 		int i = 0;
982 		struct pipe_ctx *pipes =
983 				link->dc->current_state->res_ctx.pipe_ctx;
984 
985 		destrictive = true;
986 		if (is_hdmi_frl_in_use(link)) {
987 			destrictive = false;
988 		} else if (link->local_sink->edid_caps.panel_patch.skip_frl_pre_training) {
989 			for (i = 0; i < MAX_PIPES; i++) {
990 				if (pipes[i].stream != NULL &&
991 					pipes[i].stream->link == link) {
992 					/*If link is already active, skip PHY programming*/
993 					if (link->link_status.link_active) {
994 						destrictive = false;
995 					}
996 				}
997 			}
998 		} else if (link->dc->debug.skip_detection_link_training) {
999 			destrictive = false;
1000 		}
1001 	}
1002 
1003 	return destrictive;
1004 }
1005 
1006 static void verify_link_capability(struct dc_link *link,
1007 		enum dc_detect_reason reason)
1008 {
1009 	if (should_verify_link_capability_destructively(link, reason))
1010 		verify_link_capability_destructive(link, reason);
1011 	else
1012 		verify_link_capability_non_destructive(link);
1013 }
1014 
1015 /**
1016  * link_detect_evaluate_edid_header() - Evaluate if an EDID header is acceptable.
1017  *
1018  * Evaluates an 8-byte EDID header to check if it's good enough
1019  * for the purpose of determining whether a display is connected
1020  * without reading the full EDID.
1021  *
1022  * @edid_header: The first 8 bytes of the EDID read from DDC.
1023  *
1024  * Return: true if the header looks valid (>= 6 of 8 bytes match the
1025  *         expected 00/FF pattern), false otherwise.
1026  */
1027 static bool link_detect_evaluate_edid_header(uint8_t edid_header[8])
1028 {
1029 	int edid_header_score = 0;
1030 	int i;
1031 
1032 	for (i = 0; i < 8; ++i)
1033 		edid_header_score += edid_header[i] == ((i == 0 || i == 7) ? 0x00 : 0xff);
1034 
1035 	return edid_header_score >= 6;
1036 }
1037 
1038 /**
1039  * link_detect_ddc_probe() - Probe the DDC to see if a display is connected.
1040  *
1041  * Detect whether a display is connected to DDC without reading full EDID.
1042  * Reads only the EDID header (the first 8 bytes of EDID) from DDC and
1043  * evaluates whether that matches.
1044  *
1045  * @link: DC link whose DDC/I2C is probed for the EDID header.
1046  *
1047  * Return: true if the EDID header was read and passes validation,
1048  *         false otherwise.
1049  */
1050 static bool link_detect_ddc_probe(struct dc_link *link)
1051 {
1052 	enum signal_type signal = link_detect_sink_signal_type(link, DETECT_REASON_HPD);
1053 	enum ddc_transaction_type transaction_type = get_ddc_transaction_type(signal);
1054 	uint8_t edid_header[8] = {0};
1055 	uint8_t zero = 0;
1056 	bool ddc_probed;
1057 
1058 	if (!link->ddc)
1059 		return false;
1060 
1061 	if (link->dc->hwss.prepare_ddc)
1062 		link->dc->hwss.prepare_ddc(link);
1063 
1064 	set_ddc_transaction_type(link->ddc, transaction_type);
1065 
1066 	ddc_probed = link_query_ddc_data(link->ddc, 0x50, &zero, 1, edid_header, sizeof(edid_header));
1067 
1068 	if (!ddc_probed)
1069 		return false;
1070 
1071 	if (!link_detect_evaluate_edid_header(edid_header))
1072 		return false;
1073 
1074 	return true;
1075 }
1076 
1077 /**
1078  * link_detect_dac_load_detect() - Performs DAC load detection.
1079  *
1080  * Load detection can be used to detect the presence of an
1081  * analog display when we can't read DDC. This causes a visible
1082  * visual glitch so it should be used sparingly.
1083  *
1084  * @link: DC link to test using the DAC load-detect path.
1085  *
1086  * Return: true if the VBIOS load-detect call reports OK, false
1087  *         otherwise.
1088  */
1089 static bool link_detect_dac_load_detect(struct dc_link *link)
1090 {
1091 	if (!link->dc->hwss.dac_load_detect)
1092 		return false;
1093 
1094 	return link->dc->hwss.dac_load_detect(link);
1095 }
1096 
1097 /*
1098  * detect_link_and_local_sink() - Detect if a sink is attached to a given link
1099  *
1100  * link->local_sink is created or destroyed as needed.
1101  *
1102  * This does not create remote sinks.
1103  */
1104 static bool detect_link_and_local_sink(struct dc_link *link,
1105 				  enum dc_detect_reason reason)
1106 {
1107 	struct dc_sink_init_data sink_init_data = { 0 };
1108 	struct display_sink_capability sink_caps = { 0 };
1109 	uint32_t i;
1110 	bool converter_disable_audio = false;
1111 	struct audio_support *aud_support = &link->dc->res_pool->audio_support;
1112 	bool same_edid = false;
1113 	enum dc_edid_status edid_status;
1114 	struct dc_context *dc_ctx = link->ctx;
1115 	struct dc *dc = dc_ctx->dc;
1116 	struct dc_sink *sink = NULL;
1117 	struct dc_sink *prev_sink = NULL;
1118 	struct dpcd_caps prev_dpcd_caps;
1119 	enum dc_connection_type new_connection_type = dc_connection_none;
1120 	const uint32_t post_oui_delay = 30; // 30ms
1121 
1122 	if (dc_is_virtual_signal(link->connector_signal))
1123 		return false;
1124 
1125 	if (((link->connector_signal == SIGNAL_TYPE_LVDS ||
1126 		link->connector_signal == SIGNAL_TYPE_EDP) &&
1127 		(!link->dc->config.allow_edp_hotplug_detection)) &&
1128 		link->local_sink) {
1129 		// need to re-write OUI and brightness in resume case
1130 		if (link->connector_signal == SIGNAL_TYPE_EDP) {
1131 			if (!link->dc->config.edp_no_power_sequencing)
1132 				link->dc->hwss.edp_power_control(link, true);
1133 			link->dc->hwss.edp_wait_for_hpd_ready(link, true);
1134 
1135 			if (link->dpcd_sink_ext_caps.bits.oled == 1) {
1136 				dpcd_set_source_specific_data(link);
1137 				msleep(post_oui_delay);
1138 				set_default_brightness_aux(link);
1139 			}
1140 		}
1141 
1142 		return true;
1143 	}
1144 
1145 	if (!link_detect_connection_type(link, &new_connection_type)) {
1146 		BREAK_TO_DEBUGGER();
1147 		return false;
1148 	}
1149 
1150 	prev_sink = link->local_sink;
1151 	if (prev_sink) {
1152 		dc_sink_retain(prev_sink);
1153 		memcpy(&prev_dpcd_caps, &link->dpcd_caps, sizeof(struct dpcd_caps));
1154 	}
1155 
1156 	link_disconnect_sink(link);
1157 	if (new_connection_type != dc_connection_none) {
1158 		link->type = new_connection_type;
1159 		link->link_state_valid = false;
1160 
1161 		/* From Disconnected-to-Connected. */
1162 		switch (link->connector_signal) {
1163 		case SIGNAL_TYPE_HDMI_FRL:
1164 		case SIGNAL_TYPE_HDMI_TYPE_A: {
1165 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1166 			if (aud_support->hdmi_audio_native)
1167 				sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
1168 			else
1169 				sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1170 			break;
1171 		}
1172 
1173 		case SIGNAL_TYPE_DVI_SINGLE_LINK: {
1174 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1175 			sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1176 			break;
1177 		}
1178 
1179 		case SIGNAL_TYPE_DVI_DUAL_LINK: {
1180 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1181 			sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
1182 			break;
1183 		}
1184 
1185 		case SIGNAL_TYPE_RGB: {
1186 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1187 			sink_caps.signal = SIGNAL_TYPE_RGB;
1188 			break;
1189 		}
1190 
1191 		case SIGNAL_TYPE_LVDS: {
1192 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1193 			sink_caps.signal = SIGNAL_TYPE_LVDS;
1194 			break;
1195 		}
1196 
1197 		case SIGNAL_TYPE_EDP: {
1198 			detect_edp_sink_caps(link);
1199 			read_current_link_settings_on_detect(link);
1200 
1201 			/* Disable power sequence on MIPI panel + converter
1202 			 */
1203 			if (dc->config.enable_mipi_converter_optimization &&
1204 				dc_ctx->dce_version == DCN_VERSION_3_01 &&
1205 				link->dpcd_caps.sink_dev_id == DP_BRANCH_DEVICE_ID_0022B9 &&
1206 				memcmp(&link->dpcd_caps.branch_dev_name, DP_SINK_BRANCH_DEV_NAME_7580,
1207 					sizeof(link->dpcd_caps.branch_dev_name)) == 0) {
1208 				dc->config.edp_no_power_sequencing = true;
1209 
1210 				if (!link->dpcd_caps.set_power_state_capable_edp)
1211 					link->wa_flags.dp_keep_receiver_powered = true;
1212 			}
1213 
1214 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
1215 			sink_caps.signal = SIGNAL_TYPE_EDP;
1216 			break;
1217 		}
1218 
1219 		case SIGNAL_TYPE_DISPLAY_PORT: {
1220 
1221 			/* wa HPD high coming too early*/
1222 			if (link->ep_type == DISPLAY_ENDPOINT_PHY &&
1223 			    link->link_enc->features.flags.bits.DP_IS_USB_C == 1) {
1224 
1225 				/* if alt mode times out, return false */
1226 				if (!wait_for_entering_dp_alt_mode(link)) {
1227 					if (prev_sink)
1228 						dc_sink_release(prev_sink);
1229 					return false;
1230 				}
1231 			}
1232 
1233 			if (!detect_dp(link, &sink_caps, reason)) {
1234 
1235 				if (prev_sink)
1236 					dc_sink_release(prev_sink);
1237 				return false;
1238 			}
1239 
1240 			/* Active SST downstream branch device unplug*/
1241 			if (link->type == dc_connection_sst_branch &&
1242 			    link->dpcd_caps.sink_count.bits.SINK_COUNT == 0) {
1243 				if (prev_sink)
1244 					/* Downstream unplug */
1245 					dc_sink_release(prev_sink);
1246 				return true;
1247 			}
1248 
1249 			/* disable audio for non DP to HDMI active sst converter */
1250 			if (link->type == dc_connection_sst_branch &&
1251 					is_dp_active_dongle(link) &&
1252 					(link->dpcd_caps.dongle_type !=
1253 							DISPLAY_DONGLE_DP_HDMI_CONVERTER))
1254 				converter_disable_audio = true;
1255 
1256 			/* limited link rate to HBR3 for DPIA until we implement USB4 V2 */
1257 			if (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA &&
1258 					link->reported_link_cap.link_rate > LINK_RATE_HIGH3)
1259 				link->reported_link_cap.link_rate = LINK_RATE_HIGH3;
1260 
1261 			if (link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.bits.dp_tunneling
1262 					&& link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.bits.dpia_bw_alloc
1263 					&& link->dpcd_caps.usb4_dp_tun_info.driver_bw_cap.bits.driver_bw_alloc_support) {
1264 				if (link_dpia_enable_usb4_dp_bw_alloc_mode(link) == false)
1265 					link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.bits.dpia_bw_alloc = false;
1266 			}
1267 			break;
1268 		}
1269 
1270 		default:
1271 			DC_ERROR("Invalid connector type! signal:%d\n",
1272 				 link->connector_signal);
1273 			if (prev_sink)
1274 				dc_sink_release(prev_sink);
1275 			return false;
1276 		} /* switch() */
1277 
1278 		if (link->dpcd_caps.sink_count.bits.SINK_COUNT)
1279 			link->dpcd_sink_count =
1280 				link->dpcd_caps.sink_count.bits.SINK_COUNT;
1281 		else
1282 			link->dpcd_sink_count = 1;
1283 
1284 		set_ddc_transaction_type(link->ddc,
1285 						     sink_caps.transaction_type);
1286 
1287 		link->aux_mode =
1288 			link_is_in_aux_transaction_mode(link->ddc);
1289 
1290 		sink_init_data.link = link;
1291 		sink_init_data.sink_signal = sink_caps.signal;
1292 
1293 		sink = dc_sink_create(&sink_init_data);
1294 		if (!sink) {
1295 			DC_ERROR("Failed to create sink!\n");
1296 			if (prev_sink)
1297 				dc_sink_release(prev_sink);
1298 			return false;
1299 		}
1300 
1301 		sink->link->dongle_max_pix_clk = sink_caps.max_hdmi_pixel_clock;
1302 		sink->converter_disable_audio = converter_disable_audio;
1303 
1304 		/* dc_sink_create returns a new reference */
1305 		link->local_sink = sink;
1306 
1307 		edid_status = dm_helpers_read_local_edid(link->ctx,
1308 							 link, sink);
1309 
1310 		switch (edid_status) {
1311 		case EDID_BAD_CHECKSUM:
1312 			DC_LOG_ERROR("EDID checksum invalid.\n");
1313 			break;
1314 		case EDID_PARTIAL_VALID:
1315 			DC_LOG_ERROR("Partial EDID valid, abandon invalid blocks.\n");
1316 			break;
1317 		case EDID_NO_RESPONSE:
1318 			/* Analog connectors without EDID:
1319 			 * - old monitor that actually doesn't have EDID
1320 			 * - cheap DVI-A cable or adapter that doesn't connect DDC
1321 			 */
1322 			if (dc_connector_supports_analog(link->link_id.id)) {
1323 				/* If we didn't already detect a display using
1324 				 * DAC load detection, we know it isn't connected.
1325 				 */
1326 				if (link->type != dc_connection_analog_load) {
1327 					if (prev_sink)
1328 						dc_sink_release(prev_sink);
1329 					link_disconnect_sink(link);
1330 					return false;
1331 				}
1332 
1333 				LINK_INFO("link=%d, analog display detected without EDID\n",
1334 					   link->link_index);
1335 
1336 				link->type = dc_connection_analog_load;
1337 				sink->edid_caps.analog = true;
1338 				break;
1339 			}
1340 
1341 			DC_LOG_ERROR("No EDID read.\n");
1342 
1343 			/*
1344 			 * Abort detection for non-DP connectors if we have
1345 			 * no EDID
1346 			 *
1347 			 * DP needs to report as connected if HDP is high
1348 			 * even if we have no EDID in order to go to
1349 			 * fail-safe mode
1350 			 */
1351 			if (dc_is_hdmi_signal(link->connector_signal) ||
1352 			    dc_is_dvi_signal(link->connector_signal)) {
1353 				if (prev_sink)
1354 					dc_sink_release(prev_sink);
1355 
1356 				return false;
1357 			}
1358 
1359 			if (link->type == dc_connection_sst_branch &&
1360 					link->dpcd_caps.dongle_type ==
1361 						DISPLAY_DONGLE_DP_VGA_CONVERTER &&
1362 					reason == DETECT_REASON_HPDRX) {
1363 				/* Abort detection for DP-VGA adapters when EDID
1364 				 * can't be read and detection reason is VGA-side
1365 				 * hotplug
1366 				 */
1367 				if (prev_sink)
1368 					dc_sink_release(prev_sink);
1369 				link_disconnect_sink(link);
1370 
1371 				return true;
1372 			}
1373 
1374 			break;
1375 		default:
1376 			break;
1377 		}
1378 
1379 		// Check if edid is the same
1380 		if ((prev_sink) &&
1381 		    (edid_status == EDID_THE_SAME || edid_status == EDID_OK))
1382 			same_edid = is_same_edid(&prev_sink->dc_edid,
1383 						 &sink->dc_edid);
1384 
1385 		if (sink->edid_caps.panel_patch.skip_scdc_overwrite)
1386 			link->ctx->dc->debug.hdmi20_disable = true;
1387 
1388 		if (sink->edid_caps.panel_patch.remove_sink_ext_caps)
1389 			link->dpcd_sink_ext_caps.raw = 0;
1390 
1391 		if (dc_is_hdmi_signal(link->connector_signal))
1392 			read_scdc_caps(link->ddc, link->local_sink);
1393 
1394 		if (dc_is_hdmi_signal(link->connector_signal) && dc->debug.enable_hdmi_idcc) {
1395 			memset(&link->hdmi_cable_id, 0, sizeof(union hdmi_idcc_cable_id));
1396 			read_idcc_data(link->ddc, HDMI_IDCC_SCOPE_RW_CA,
1397 				link->hdmi_cable_id.raw, 0, 4);
1398 		}
1399 
1400 		if (sink->edid_caps.rr_capable && !dc->debug.skip_hdmi_rr_enable)
1401 			hdmi_frl_write_read_request_enable(link->ddc);
1402 
1403 		/* When FreeSync is toggled through OSD,
1404 		 * we see same EDID no matter what. Check MCCS caps
1405 		 * to see if we should update FreeSync caps now.
1406 		 */
1407 		dm_helpers_read_mccs_caps(
1408 				link->ctx,
1409 				link,
1410 				sink);
1411 
1412 		if (prev_sink != NULL) {
1413 			if (memcmp(&sink->mccs_caps, &prev_sink->mccs_caps, sizeof(struct mccs_caps)))
1414 				same_edid = false;
1415 		}
1416 
1417 		if (reason != DETECT_REASON_FALLBACK && dc_is_hdmi_signal(link->connector_signal) &&
1418 				link->link_enc->features.flags.bits.IS_HDMI_FRL_CAPABLE && sink->edid_caps.max_frl_rate != 0) {
1419 			hdmi_frl_retrieve_link_cap(link, link->local_sink);
1420 		}
1421 		if (reason == DETECT_REASON_FALLBACK && sink->sink_signal == SIGNAL_TYPE_HDMI_FRL)
1422 			same_edid = false;
1423 		if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
1424 		    sink_caps.transaction_type ==
1425 		    DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
1426 			/*
1427 			 * TODO debug why certain monitors don't like
1428 			 *  two link trainings
1429 			 */
1430 			query_hdcp_capability(sink->sink_signal, link);
1431 		} else {
1432 			// If edid is the same, then discard new sink and revert back to original sink
1433 			if (same_edid) {
1434 				link_disconnect_remap(prev_sink, link);
1435 				sink = prev_sink;
1436 				prev_sink = NULL;
1437 				if (reason == DETECT_REASON_FALLBACK && sink->sink_signal == SIGNAL_TYPE_HDMI_FRL)
1438 						sink->sink_signal = SIGNAL_TYPE_HDMI_TYPE_A;
1439 			}
1440 
1441 			if (!sink->edid_caps.analog)
1442 				query_hdcp_capability(sink->sink_signal, link);
1443 		}
1444 
1445 		/* DVI-I connector connected to analog display. */
1446 		if ((link->link_id.id == CONNECTOR_ID_DUAL_LINK_DVII ||
1447 		     link->link_id.id == CONNECTOR_ID_SINGLE_LINK_DVII) &&
1448 			sink->edid_caps.analog)
1449 			sink->sink_signal = SIGNAL_TYPE_RGB;
1450 
1451 		/* HDMI-DVI Dongle */
1452 		if (sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A &&
1453 		    !sink->edid_caps.edid_hdmi)
1454 			sink->sink_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1455 		else if (dc_is_dvi_signal(sink->sink_signal) &&
1456 			 dc_is_dvi_signal(link->connector_signal) &&
1457 			 aud_support->hdmi_audio_native &&
1458 			 sink->edid_caps.edid_hdmi)
1459 			sink->sink_signal = SIGNAL_TYPE_HDMI_TYPE_A;
1460 
1461 		if (link->local_sink && dc_is_dp_signal(sink_caps.signal))
1462 			dp_trace_init(link);
1463 
1464 		/* Connectivity log: detection */
1465 		for (i = 0; i < sink->dc_edid.length / DC_EDID_BLOCK_SIZE; i++) {
1466 			CONN_DATA_DETECT(link,
1467 					 &sink->dc_edid.raw_edid[i * DC_EDID_BLOCK_SIZE],
1468 					 DC_EDID_BLOCK_SIZE,
1469 					 "%s: [Block %d] ", sink->edid_caps.display_name, i);
1470 		}
1471 
1472 		DC_LOG_DETECTION_EDID_PARSER("%s: "
1473 			"manufacturer_id = %X, "
1474 			"product_id = %X, "
1475 			"serial_number = %X, "
1476 			"manufacture_week = %d, "
1477 			"manufacture_year = %d, "
1478 			"display_name = %s, "
1479 			"speaker_flag = %d, "
1480 			"audio_mode_count = %d\n",
1481 			__func__,
1482 			sink->edid_caps.manufacturer_id,
1483 			sink->edid_caps.product_id,
1484 			sink->edid_caps.serial_number,
1485 			sink->edid_caps.manufacture_week,
1486 			sink->edid_caps.manufacture_year,
1487 			sink->edid_caps.display_name,
1488 			sink->edid_caps.speaker_flags,
1489 			sink->edid_caps.audio_mode_count);
1490 
1491 		for (i = 0; i < sink->edid_caps.audio_mode_count; i++) {
1492 			DC_LOG_DETECTION_EDID_PARSER("%s: mode number = %d, "
1493 				"format_code = %d, "
1494 				"channel_count = %d, "
1495 				"sample_rate = %d, "
1496 				"sample_size = %d\n",
1497 				__func__,
1498 				i,
1499 				sink->edid_caps.audio_modes[i].format_code,
1500 				sink->edid_caps.audio_modes[i].channel_count,
1501 				sink->edid_caps.audio_modes[i].sample_rate,
1502 				sink->edid_caps.audio_modes[i].sample_size);
1503 		}
1504 
1505 		if (link->connector_signal == SIGNAL_TYPE_EDP) {
1506 			// Init dc_panel_config by HW config
1507 			if (dc_ctx->dc->res_pool->funcs->get_panel_config_defaults)
1508 				dc_ctx->dc->res_pool->funcs->get_panel_config_defaults(&link->panel_config);
1509 			// Pickup base DM settings
1510 			dm_helpers_init_panel_settings(dc_ctx, &link->panel_config, sink);
1511 			// Override dc_panel_config if system has specific settings
1512 			dm_helpers_override_panel_settings(dc_ctx, link);
1513 
1514 			//sink only can use supported link rate table, we are foreced to enable it
1515 			if (link->reported_link_cap.link_rate == LINK_RATE_UNKNOWN)
1516 				link->panel_config.ilr.optimize_edp_link_rate = true;
1517 			link->reported_link_cap.link_rate = get_max_edp_link_rate(link);
1518 		}
1519 
1520 	} else {
1521 		/* From Connected-to-Disconnected. */
1522 		link->type = dc_connection_none;
1523 		sink_caps.signal = SIGNAL_TYPE_NONE;
1524 		memset(&link->hdcp_caps, 0, sizeof(struct hdcp_caps));
1525 		/* When we unplug a passive DP-HDMI dongle connection, dongle_max_pix_clk
1526 		 *  is not cleared. If we emulate a DP signal on this connection, it thinks
1527 		 *  the dongle is still there and limits the number of modes we can emulate.
1528 		 *  Clear dongle_max_pix_clk on disconnect to fix this
1529 		 */
1530 		link->dongle_max_pix_clk = 0;
1531 
1532 		dc_link_clear_dprx_states(link);
1533 		dp_trace_reset(link);
1534 	}
1535 
1536 	LINK_INFO("link=%d, dc_sink_in=%p is now %s prev_sink=%p edid same=%d\n",
1537 		  link->link_index, sink,
1538 		  (sink_caps.signal ==
1539 		   SIGNAL_TYPE_NONE ? "Disconnected" : "Connected"),
1540 		  prev_sink, same_edid);
1541 
1542 	if (prev_sink)
1543 		dc_sink_release(prev_sink);
1544 
1545 	return true;
1546 }
1547 
1548 /**
1549  * link_detect_connection_type_analog() - Determines if an analog sink is connected.
1550  *
1551  * @link: DC link to evaluate (must support analog signalling).
1552  * @type: Updated with the detected connection type:
1553  *        dc_connection_single (analog via DDC),
1554  *        dc_connection_analog_load (via load-detect),
1555  *        or dc_connection_none.
1556  *
1557  * Return: true if detection completed.
1558  */
1559 static bool link_detect_connection_type_analog(struct dc_link *link, enum dc_connection_type *type)
1560 {
1561 	/* Don't care about connectors that don't support an analog signal. */
1562 	ASSERT(dc_connector_supports_analog(link->link_id.id));
1563 
1564 	if (link_detect_ddc_probe(link)) {
1565 		*type = dc_connection_single;
1566 		return true;
1567 	}
1568 
1569 	if (link_detect_dac_load_detect(link)) {
1570 		*type = dc_connection_analog_load;
1571 		return true;
1572 	}
1573 
1574 	*type = dc_connection_none;
1575 	return true;
1576 }
1577 
1578 /*
1579  * link_detect_connection_type() - Determine if there is a sink connected
1580  *
1581  * @type: Returned connection type
1582  * Does not detect downstream devices, such as MST sinks
1583  * or display connected through active dongles
1584  */
1585 bool link_detect_connection_type(struct dc_link *link, enum dc_connection_type *type)
1586 {
1587 	if (link->connector_signal == SIGNAL_TYPE_LVDS) {
1588 		*type = dc_connection_single;
1589 		return true;
1590 	}
1591 
1592 	/* Ignore the HPD pin (if any) for analog connectors.
1593 	 * Instead rely on DDC and DAC.
1594 	 *
1595 	 * - VGA connectors don't have any HPD at all.
1596 	 * - Some DVI-A cables don't connect the HPD pin.
1597 	 * - Some DVI-A cables pull up the HPD pin.
1598 	 *   (So it's high even when no display is connected.)
1599 	 */
1600 	if (dc_connector_supports_analog(link->link_id.id))
1601 		return link_detect_connection_type_analog(link, type);
1602 
1603 	if (link->connector_signal == SIGNAL_TYPE_EDP) {
1604 		/*in case it is not on*/
1605 		if (!link->dc->config.edp_no_power_sequencing)
1606 			link->dc->hwss.edp_power_control(link, true);
1607 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
1608 	}
1609 
1610 	/* Link may not have physical HPD pin. */
1611 	if (link->ep_type != DISPLAY_ENDPOINT_PHY) {
1612 		if (link->is_hpd_pending || !dpia_query_hpd_status(link))
1613 			*type = dc_connection_none;
1614 		else
1615 			*type = dc_connection_single;
1616 
1617 		return true;
1618 	}
1619 
1620 
1621 	if (link_get_hpd_state(link)) {
1622 		*type = dc_connection_single;
1623 		/* TODO: need to do the actual detection */
1624 	} else {
1625 		*type = dc_connection_none;
1626 		if (link->connector_signal == SIGNAL_TYPE_EDP) {
1627 			/* eDP is not connected, power down it */
1628 			if (!link->dc->config.edp_no_power_sequencing)
1629 				link->dc->hwss.edp_power_control(link, false);
1630 		}
1631 	}
1632 
1633 	return true;
1634 }
1635 
1636 bool link_detect(struct dc_link *link, enum dc_detect_reason reason)
1637 {
1638 	bool is_local_sink_detect_success;
1639 	bool is_delegated_to_mst_top_mgr = false;
1640 	enum dc_connection_type pre_link_type = link->type;
1641 
1642 	is_local_sink_detect_success = detect_link_and_local_sink(link, reason);
1643 
1644 	if (is_local_sink_detect_success && link->local_sink) {
1645 		verify_link_capability(link, reason);
1646 	}
1647 
1648 	DC_LOG_DC("%s: link_index=%d is_local_sink_detect_success=%d pre_link_type=%d link_type=%d\n", __func__,
1649 				link->link_index, is_local_sink_detect_success, pre_link_type, link->type);
1650 
1651 	if (is_local_sink_detect_success && link->local_sink &&
1652 			dc_is_dp_signal(link->local_sink->sink_signal) &&
1653 			link->dpcd_caps.is_mst_capable)
1654 		is_delegated_to_mst_top_mgr = discover_dp_mst_topology(link, reason);
1655 
1656 	if (pre_link_type == dc_connection_mst_branch &&
1657 			link->type != dc_connection_mst_branch)
1658 		is_delegated_to_mst_top_mgr = link_reset_cur_dp_mst_topology(link);
1659 
1660 	return is_local_sink_detect_success && !is_delegated_to_mst_top_mgr;
1661 }
1662 
1663 void link_clear_dprx_states(struct dc_link *link)
1664 {
1665 	memset(&link->dprx_states, 0, sizeof(link->dprx_states));
1666 }
1667 
1668 bool link_is_hdcp14(struct dc_link *link, enum signal_type signal)
1669 {
1670 	bool ret = false;
1671 
1672 	switch (signal)	{
1673 	case SIGNAL_TYPE_DISPLAY_PORT:
1674 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
1675 		ret = link->hdcp_caps.bcaps.bits.HDCP_CAPABLE;
1676 		break;
1677 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
1678 	case SIGNAL_TYPE_DVI_DUAL_LINK:
1679 	case SIGNAL_TYPE_HDMI_TYPE_A:
1680 	/* HDMI doesn't tell us its HDCP(1.4) capability, so assume to always be capable,
1681 	 * we can poll for bksv but some displays have an issue with this. Since its so rare
1682 	 * for a display to not be 1.4 capable, this assumtion is ok
1683 	 */
1684 		ret = true;
1685 		break;
1686 	default:
1687 		break;
1688 	}
1689 	return ret;
1690 }
1691 
1692 bool link_is_hdcp22(struct dc_link *link, enum signal_type signal)
1693 {
1694 	bool ret = false;
1695 
1696 	switch (signal)	{
1697 	case SIGNAL_TYPE_DISPLAY_PORT:
1698 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
1699 		ret = (link->hdcp_caps.bcaps.bits.HDCP_CAPABLE &&
1700 				link->hdcp_caps.rx_caps.fields.byte0.hdcp_capable &&
1701 				(link->hdcp_caps.rx_caps.fields.version == 0x2)) ? 1 : 0;
1702 		break;
1703 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
1704 	case SIGNAL_TYPE_DVI_DUAL_LINK:
1705 	case SIGNAL_TYPE_HDMI_TYPE_A:
1706 	case SIGNAL_TYPE_HDMI_FRL:
1707 		ret = (link->hdcp_caps.rx_caps.fields.version == 0x4) ? 1:0;
1708 		break;
1709 	default:
1710 		break;
1711 	}
1712 
1713 	return ret;
1714 }
1715 
1716 const struct dc_link_status *link_get_status(const struct dc_link *link)
1717 {
1718 	return &link->link_status;
1719 }
1720 
1721 
1722 static bool link_add_remote_sink_helper(struct dc_link *dc_link, struct dc_sink *sink)
1723 {
1724 	if (dc_link->sink_count >= MAX_SINKS_PER_LINK) {
1725 		BREAK_TO_DEBUGGER();
1726 		return false;
1727 	}
1728 
1729 	dc_sink_retain(sink);
1730 
1731 	dc_link->remote_sinks[dc_link->sink_count] = sink;
1732 	dc_link->sink_count++;
1733 
1734 	return true;
1735 }
1736 
1737 struct dc_sink *link_add_remote_sink(
1738 		struct dc_link *link,
1739 		const uint8_t *edid,
1740 		unsigned int len,
1741 		struct dc_sink_init_data *init_data)
1742 {
1743 	struct dc_sink *dc_sink;
1744 	enum dc_edid_status edid_status;
1745 
1746 	if (len > DC_MAX_EDID_BUFFER_SIZE) {
1747 		dm_error("Max EDID buffer size breached!\n");
1748 		return NULL;
1749 	}
1750 
1751 	if (!init_data) {
1752 		BREAK_TO_DEBUGGER();
1753 		return NULL;
1754 	}
1755 
1756 	if (!init_data->link) {
1757 		BREAK_TO_DEBUGGER();
1758 		return NULL;
1759 	}
1760 
1761 	dc_sink = dc_sink_create(init_data);
1762 
1763 	if (!dc_sink)
1764 		return NULL;
1765 
1766 	memmove(dc_sink->dc_edid.raw_edid, edid, len);
1767 	dc_sink->dc_edid.length = len;
1768 
1769 	if (!link_add_remote_sink_helper(
1770 			link,
1771 			dc_sink))
1772 		goto fail_add_sink;
1773 
1774 	edid_status = dm_helpers_parse_edid_caps(
1775 			link,
1776 			&dc_sink->dc_edid,
1777 			&dc_sink->edid_caps);
1778 
1779 	/*
1780 	 * Treat device as no EDID device if EDID
1781 	 * parsing fails
1782 	 */
1783 	if (edid_status != EDID_OK && edid_status != EDID_PARTIAL_VALID) {
1784 		dc_sink->dc_edid.length = 0;
1785 		dm_error("Bad EDID, status%d!\n", edid_status);
1786 	}
1787 
1788 	return dc_sink;
1789 
1790 fail_add_sink:
1791 	dc_sink_release(dc_sink);
1792 	return NULL;
1793 }
1794 
1795 void link_remove_remote_sink(struct dc_link *link, struct dc_sink *sink)
1796 {
1797 	unsigned int i;
1798 
1799 	if (!link->sink_count) {
1800 		BREAK_TO_DEBUGGER();
1801 		return;
1802 	}
1803 
1804 	for (i = 0; i < link->sink_count; i++) {
1805 		if (link->remote_sinks[i] == sink) {
1806 			dc_sink_release(sink);
1807 			link->remote_sinks[i] = NULL;
1808 
1809 			/* shrink array to remove empty place */
1810 			while (i < link->sink_count - 1) {
1811 				link->remote_sinks[i] = link->remote_sinks[i+1];
1812 				i++;
1813 			}
1814 			link->remote_sinks[i] = NULL;
1815 			link->sink_count--;
1816 			return;
1817 		}
1818 	}
1819 }
1820