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