• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012-15 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 #include <linux/slab.h>
27 
28 #include "dm_services.h"
29 #include "atomfirmware.h"
30 #include "dm_helpers.h"
31 #include "dc.h"
32 #include "grph_object_id.h"
33 #include "gpio_service_interface.h"
34 #include "core_status.h"
35 #include "dc_link_dp.h"
36 #include "dc_link_ddc.h"
37 #include "link_hwss.h"
38 #include "opp.h"
39 
40 #include "link_encoder.h"
41 #include "hw_sequencer.h"
42 #include "resource.h"
43 #include "abm.h"
44 #include "fixed31_32.h"
45 #include "dpcd_defs.h"
46 #include "dmcu.h"
47 #include "hw/clk_mgr.h"
48 #include "dce/dmub_psr.h"
49 #include "dmub/dmub_srv.h"
50 #include "inc/hw/panel_cntl.h"
51 
52 #define DC_LOGGER_INIT(logger)
53 
54 #define LINK_INFO(...) \
55 	DC_LOG_HW_HOTPLUG(  \
56 		__VA_ARGS__)
57 
58 #define RETIMER_REDRIVER_INFO(...) \
59 	DC_LOG_RETIMER_REDRIVER(  \
60 		__VA_ARGS__)
61 /*******************************************************************************
62  * Private structures
63  ******************************************************************************/
64 
65 enum {
66 	PEAK_FACTOR_X1000 = 1006,
67 	/*
68 	 * Some receivers fail to train on first try and are good
69 	 * on subsequent tries. 2 retries should be plenty. If we
70 	 * don't have a successful training then we don't expect to
71 	 * ever get one.
72 	 */
73 	LINK_TRAINING_MAX_VERIFY_RETRY = 2
74 };
75 
76 /*******************************************************************************
77  * Private functions
78  ******************************************************************************/
dc_link_destruct(struct dc_link * link)79 static void dc_link_destruct(struct dc_link *link)
80 {
81 	int i;
82 
83 	if (link->hpd_gpio) {
84 		dal_gpio_destroy_irq(&link->hpd_gpio);
85 		link->hpd_gpio = NULL;
86 	}
87 
88 	if (link->ddc)
89 		dal_ddc_service_destroy(&link->ddc);
90 
91 	if (link->panel_cntl)
92 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
93 
94 	if (link->link_enc)
95 		link->link_enc->funcs->destroy(&link->link_enc);
96 
97 	if (link->local_sink)
98 		dc_sink_release(link->local_sink);
99 
100 	for (i = 0; i < link->sink_count; ++i)
101 		dc_sink_release(link->remote_sinks[i]);
102 }
103 
get_hpd_gpio(struct dc_bios * dcb,struct graphics_object_id link_id,struct gpio_service * gpio_service)104 struct gpio *get_hpd_gpio(struct dc_bios *dcb,
105 			  struct graphics_object_id link_id,
106 			  struct gpio_service *gpio_service)
107 {
108 	enum bp_result bp_result;
109 	struct graphics_object_hpd_info hpd_info;
110 	struct gpio_pin_info pin_info;
111 
112 	if (dcb->funcs->get_hpd_info(dcb, link_id, &hpd_info) != BP_RESULT_OK)
113 		return NULL;
114 
115 	bp_result = dcb->funcs->get_gpio_pin_info(dcb,
116 		hpd_info.hpd_int_gpio_uid, &pin_info);
117 
118 	if (bp_result != BP_RESULT_OK) {
119 		ASSERT(bp_result == BP_RESULT_NORECORD);
120 		return NULL;
121 	}
122 
123 	return dal_gpio_service_create_irq(gpio_service,
124 					   pin_info.offset,
125 					   pin_info.mask);
126 }
127 
128 /*
129  *  Function: program_hpd_filter
130  *
131  *  @brief
132  *     Programs HPD filter on associated HPD line
133  *
134  *  @param [in] delay_on_connect_in_ms: Connect filter timeout
135  *  @param [in] delay_on_disconnect_in_ms: Disconnect filter timeout
136  *
137  *  @return
138  *     true on success, false otherwise
139  */
program_hpd_filter(const struct dc_link * link)140 static bool program_hpd_filter(const struct dc_link *link)
141 {
142 	bool result = false;
143 	struct gpio *hpd;
144 	int delay_on_connect_in_ms = 0;
145 	int delay_on_disconnect_in_ms = 0;
146 
147 	if (link->is_hpd_filter_disabled)
148 		return false;
149 	/* Verify feature is supported */
150 	switch (link->connector_signal) {
151 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
152 	case SIGNAL_TYPE_DVI_DUAL_LINK:
153 	case SIGNAL_TYPE_HDMI_TYPE_A:
154 		/* Program hpd filter */
155 		delay_on_connect_in_ms = 500;
156 		delay_on_disconnect_in_ms = 100;
157 		break;
158 	case SIGNAL_TYPE_DISPLAY_PORT:
159 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
160 		/* Program hpd filter to allow DP signal to settle */
161 		/* 500:	not able to detect MST <-> SST switch as HPD is low for
162 		 * only 100ms on DELL U2413
163 		 * 0: some passive dongle still show aux mode instead of i2c
164 		 * 20-50: not enough to hide bouncing HPD with passive dongle.
165 		 * also see intermittent i2c read issues.
166 		 */
167 		delay_on_connect_in_ms = 80;
168 		delay_on_disconnect_in_ms = 0;
169 		break;
170 	case SIGNAL_TYPE_LVDS:
171 	case SIGNAL_TYPE_EDP:
172 	default:
173 		/* Don't program hpd filter */
174 		return false;
175 	}
176 
177 	/* Obtain HPD handle */
178 	hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
179 			   link->ctx->gpio_service);
180 
181 	if (!hpd)
182 		return result;
183 
184 	/* Setup HPD filtering */
185 	if (dal_gpio_open(hpd, GPIO_MODE_INTERRUPT) == GPIO_RESULT_OK) {
186 		struct gpio_hpd_config config;
187 
188 		config.delay_on_connect = delay_on_connect_in_ms;
189 		config.delay_on_disconnect = delay_on_disconnect_in_ms;
190 
191 		dal_irq_setup_hpd_filter(hpd, &config);
192 
193 		dal_gpio_close(hpd);
194 
195 		result = true;
196 	} else {
197 		ASSERT_CRITICAL(false);
198 	}
199 
200 	/* Release HPD handle */
201 	dal_gpio_destroy_irq(&hpd);
202 
203 	return result;
204 }
205 
206 /**
207  * dc_link_detect_sink() - Determine if there is a sink connected
208  *
209  * @type: Returned connection type
210  * Does not detect downstream devices, such as MST sinks
211  * or display connected through active dongles
212  */
dc_link_detect_sink(struct dc_link * link,enum dc_connection_type * type)213 bool dc_link_detect_sink(struct dc_link *link, enum dc_connection_type *type)
214 {
215 	uint32_t is_hpd_high = 0;
216 	struct gpio *hpd_pin;
217 
218 	if (link->connector_signal == SIGNAL_TYPE_LVDS) {
219 		*type = dc_connection_single;
220 		return true;
221 	}
222 
223 	if (link->connector_signal == SIGNAL_TYPE_EDP) {
224 		/*in case it is not on*/
225 		link->dc->hwss.edp_power_control(link, true);
226 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
227 	}
228 
229 	/* todo: may need to lock gpio access */
230 	hpd_pin = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
231 			       link->ctx->gpio_service);
232 	if (!hpd_pin)
233 		goto hpd_gpio_failure;
234 
235 	dal_gpio_open(hpd_pin, GPIO_MODE_INTERRUPT);
236 	dal_gpio_get_value(hpd_pin, &is_hpd_high);
237 	dal_gpio_close(hpd_pin);
238 	dal_gpio_destroy_irq(&hpd_pin);
239 
240 	if (is_hpd_high) {
241 		*type = dc_connection_single;
242 		/* TODO: need to do the actual detection */
243 	} else {
244 		*type = dc_connection_none;
245 	}
246 
247 	return true;
248 
249 hpd_gpio_failure:
250 	return false;
251 }
252 
get_ddc_transaction_type(enum signal_type sink_signal)253 static enum ddc_transaction_type get_ddc_transaction_type(enum signal_type sink_signal)
254 {
255 	enum ddc_transaction_type transaction_type = DDC_TRANSACTION_TYPE_NONE;
256 
257 	switch (sink_signal) {
258 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
259 	case SIGNAL_TYPE_DVI_DUAL_LINK:
260 	case SIGNAL_TYPE_HDMI_TYPE_A:
261 	case SIGNAL_TYPE_LVDS:
262 	case SIGNAL_TYPE_RGB:
263 		transaction_type = DDC_TRANSACTION_TYPE_I2C;
264 		break;
265 
266 	case SIGNAL_TYPE_DISPLAY_PORT:
267 	case SIGNAL_TYPE_EDP:
268 		transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
269 		break;
270 
271 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
272 		/* MST does not use I2COverAux, but there is the
273 		 * SPECIAL use case for "immediate dwnstrm device
274 		 * access" (EPR#370830).
275 		 */
276 		transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
277 		break;
278 
279 	default:
280 		break;
281 	}
282 
283 	return transaction_type;
284 }
285 
get_basic_signal_type(struct graphics_object_id encoder,struct graphics_object_id downstream)286 static enum signal_type get_basic_signal_type(struct graphics_object_id encoder,
287 					      struct graphics_object_id downstream)
288 {
289 	if (downstream.type == OBJECT_TYPE_CONNECTOR) {
290 		switch (downstream.id) {
291 		case CONNECTOR_ID_SINGLE_LINK_DVII:
292 			switch (encoder.id) {
293 			case ENCODER_ID_INTERNAL_DAC1:
294 			case ENCODER_ID_INTERNAL_KLDSCP_DAC1:
295 			case ENCODER_ID_INTERNAL_DAC2:
296 			case ENCODER_ID_INTERNAL_KLDSCP_DAC2:
297 				return SIGNAL_TYPE_RGB;
298 			default:
299 				return SIGNAL_TYPE_DVI_SINGLE_LINK;
300 			}
301 		break;
302 		case CONNECTOR_ID_DUAL_LINK_DVII:
303 		{
304 			switch (encoder.id) {
305 			case ENCODER_ID_INTERNAL_DAC1:
306 			case ENCODER_ID_INTERNAL_KLDSCP_DAC1:
307 			case ENCODER_ID_INTERNAL_DAC2:
308 			case ENCODER_ID_INTERNAL_KLDSCP_DAC2:
309 				return SIGNAL_TYPE_RGB;
310 			default:
311 				return SIGNAL_TYPE_DVI_DUAL_LINK;
312 			}
313 		}
314 		break;
315 		case CONNECTOR_ID_SINGLE_LINK_DVID:
316 			return SIGNAL_TYPE_DVI_SINGLE_LINK;
317 		case CONNECTOR_ID_DUAL_LINK_DVID:
318 			return SIGNAL_TYPE_DVI_DUAL_LINK;
319 		case CONNECTOR_ID_VGA:
320 			return SIGNAL_TYPE_RGB;
321 		case CONNECTOR_ID_HDMI_TYPE_A:
322 			return SIGNAL_TYPE_HDMI_TYPE_A;
323 		case CONNECTOR_ID_LVDS:
324 			return SIGNAL_TYPE_LVDS;
325 		case CONNECTOR_ID_DISPLAY_PORT:
326 			return SIGNAL_TYPE_DISPLAY_PORT;
327 		case CONNECTOR_ID_EDP:
328 			return SIGNAL_TYPE_EDP;
329 		default:
330 			return SIGNAL_TYPE_NONE;
331 		}
332 	} else if (downstream.type == OBJECT_TYPE_ENCODER) {
333 		switch (downstream.id) {
334 		case ENCODER_ID_EXTERNAL_NUTMEG:
335 		case ENCODER_ID_EXTERNAL_TRAVIS:
336 			return SIGNAL_TYPE_DISPLAY_PORT;
337 		default:
338 			return SIGNAL_TYPE_NONE;
339 		}
340 	}
341 
342 	return SIGNAL_TYPE_NONE;
343 }
344 
345 /**
346  * dc_link_is_dp_sink_present() - Check if there is a native DP
347  * or passive DP-HDMI dongle connected
348  */
dc_link_is_dp_sink_present(struct dc_link * link)349 bool dc_link_is_dp_sink_present(struct dc_link *link)
350 {
351 	enum gpio_result gpio_result;
352 	uint32_t clock_pin = 0;
353 	uint8_t retry = 0;
354 	struct ddc *ddc;
355 
356 	enum connector_id connector_id =
357 		dal_graphics_object_id_get_connector_id(link->link_id);
358 
359 	bool present =
360 		((connector_id == CONNECTOR_ID_DISPLAY_PORT) ||
361 		(connector_id == CONNECTOR_ID_EDP));
362 
363 	ddc = dal_ddc_service_get_ddc_pin(link->ddc);
364 
365 	if (!ddc) {
366 		BREAK_TO_DEBUGGER();
367 		return present;
368 	}
369 
370 	/* Open GPIO and set it to I2C mode */
371 	/* Note: this GpioMode_Input will be converted
372 	 * to GpioConfigType_I2cAuxDualMode in GPIO component,
373 	 * which indicates we need additional delay
374 	 */
375 
376 	if (dal_ddc_open(ddc, GPIO_MODE_INPUT,
377 			 GPIO_DDC_CONFIG_TYPE_MODE_I2C) != GPIO_RESULT_OK) {
378 		dal_ddc_close(ddc);
379 
380 		return present;
381 	}
382 
383 	/*
384 	 * Read GPIO: DP sink is present if both clock and data pins are zero
385 	 *
386 	 * [W/A] plug-unplug DP cable, sometimes customer board has
387 	 * one short pulse on clk_pin(1V, < 1ms). DP will be config to HDMI/DVI
388 	 * then monitor can't br light up. Add retry 3 times
389 	 * But in real passive dongle, it need additional 3ms to detect
390 	 */
391 	do {
392 		gpio_result = dal_gpio_get_value(ddc->pin_clock, &clock_pin);
393 		ASSERT(gpio_result == GPIO_RESULT_OK);
394 		if (clock_pin)
395 			udelay(1000);
396 		else
397 			break;
398 	} while (retry++ < 3);
399 
400 	present = (gpio_result == GPIO_RESULT_OK) && !clock_pin;
401 
402 	dal_ddc_close(ddc);
403 
404 	return present;
405 }
406 
407 /*
408  * @brief
409  * Detect output sink type
410  */
link_detect_sink(struct dc_link * link,enum dc_detect_reason reason)411 static enum signal_type link_detect_sink(struct dc_link *link,
412 					 enum dc_detect_reason reason)
413 {
414 	enum signal_type result = get_basic_signal_type(link->link_enc->id,
415 							link->link_id);
416 
417 	/* Internal digital encoder will detect only dongles
418 	 * that require digital signal
419 	 */
420 
421 	/* Detection mechanism is different
422 	 * for different native connectors.
423 	 * LVDS connector supports only LVDS signal;
424 	 * PCIE is a bus slot, the actual connector needs to be detected first;
425 	 * eDP connector supports only eDP signal;
426 	 * HDMI should check straps for audio
427 	 */
428 
429 	/* PCIE detects the actual connector on add-on board */
430 	if (link->link_id.id == CONNECTOR_ID_PCIE) {
431 		/* ZAZTODO implement PCIE add-on card detection */
432 	}
433 
434 	switch (link->link_id.id) {
435 	case CONNECTOR_ID_HDMI_TYPE_A: {
436 		/* check audio support:
437 		 * if native HDMI is not supported, switch to DVI
438 		 */
439 		struct audio_support *aud_support =
440 					&link->dc->res_pool->audio_support;
441 
442 		if (!aud_support->hdmi_audio_native)
443 			if (link->link_id.id == CONNECTOR_ID_HDMI_TYPE_A)
444 				result = SIGNAL_TYPE_DVI_SINGLE_LINK;
445 	}
446 	break;
447 	case CONNECTOR_ID_DISPLAY_PORT: {
448 		/* DP HPD short pulse. Passive DP dongle will not
449 		 * have short pulse
450 		 */
451 		if (reason != DETECT_REASON_HPDRX) {
452 			/* Check whether DP signal detected: if not -
453 			 * we assume signal is DVI; it could be corrected
454 			 * to HDMI after dongle detection
455 			 */
456 			if (!dm_helpers_is_dp_sink_present(link))
457 				result = SIGNAL_TYPE_DVI_SINGLE_LINK;
458 		}
459 	}
460 	break;
461 	default:
462 	break;
463 	}
464 
465 	return result;
466 }
467 
decide_signal_from_strap_and_dongle_type(enum display_dongle_type dongle_type,struct audio_support * audio_support)468 static enum signal_type decide_signal_from_strap_and_dongle_type(enum display_dongle_type dongle_type,
469 								 struct audio_support *audio_support)
470 {
471 	enum signal_type signal = SIGNAL_TYPE_NONE;
472 
473 	switch (dongle_type) {
474 	case DISPLAY_DONGLE_DP_HDMI_DONGLE:
475 		if (audio_support->hdmi_audio_on_dongle)
476 			signal = SIGNAL_TYPE_HDMI_TYPE_A;
477 		else
478 			signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
479 		break;
480 	case DISPLAY_DONGLE_DP_DVI_DONGLE:
481 		signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
482 		break;
483 	case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
484 		if (audio_support->hdmi_audio_native)
485 			signal =  SIGNAL_TYPE_HDMI_TYPE_A;
486 		else
487 			signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
488 		break;
489 	default:
490 		signal = SIGNAL_TYPE_NONE;
491 		break;
492 	}
493 
494 	return signal;
495 }
496 
dp_passive_dongle_detection(struct ddc_service * ddc,struct display_sink_capability * sink_cap,struct audio_support * audio_support)497 static enum signal_type dp_passive_dongle_detection(struct ddc_service *ddc,
498 						    struct display_sink_capability *sink_cap,
499 						    struct audio_support *audio_support)
500 {
501 	dal_ddc_service_i2c_query_dp_dual_mode_adaptor(ddc, sink_cap);
502 
503 	return decide_signal_from_strap_and_dongle_type(sink_cap->dongle_type,
504 							audio_support);
505 }
506 
link_disconnect_sink(struct dc_link * link)507 static void link_disconnect_sink(struct dc_link *link)
508 {
509 	if (link->local_sink) {
510 		dc_sink_release(link->local_sink);
511 		link->local_sink = NULL;
512 	}
513 
514 	link->dpcd_sink_count = 0;
515 }
516 
link_disconnect_remap(struct dc_sink * prev_sink,struct dc_link * link)517 static void link_disconnect_remap(struct dc_sink *prev_sink, struct dc_link *link)
518 {
519 	dc_sink_release(link->local_sink);
520 	link->local_sink = prev_sink;
521 }
522 
523 #if defined(CONFIG_DRM_AMD_DC_HDCP)
dc_link_is_hdcp14(struct dc_link * link,enum signal_type signal)524 bool dc_link_is_hdcp14(struct dc_link *link, enum signal_type signal)
525 {
526 	bool ret = false;
527 
528 	switch (signal)	{
529 	case SIGNAL_TYPE_DISPLAY_PORT:
530 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
531 		ret = link->hdcp_caps.bcaps.bits.HDCP_CAPABLE;
532 		break;
533 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
534 	case SIGNAL_TYPE_DVI_DUAL_LINK:
535 	case SIGNAL_TYPE_HDMI_TYPE_A:
536 	/* HDMI doesn't tell us its HDCP(1.4) capability, so assume to always be capable,
537 	 * we can poll for bksv but some displays have an issue with this. Since its so rare
538 	 * for a display to not be 1.4 capable, this assumtion is ok
539 	 */
540 		ret = true;
541 		break;
542 	default:
543 		break;
544 	}
545 	return ret;
546 }
547 
dc_link_is_hdcp22(struct dc_link * link,enum signal_type signal)548 bool dc_link_is_hdcp22(struct dc_link *link, enum signal_type signal)
549 {
550 	bool ret = false;
551 
552 	switch (signal)	{
553 	case SIGNAL_TYPE_DISPLAY_PORT:
554 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
555 		ret = (link->hdcp_caps.bcaps.bits.HDCP_CAPABLE &&
556 				link->hdcp_caps.rx_caps.fields.byte0.hdcp_capable &&
557 				(link->hdcp_caps.rx_caps.fields.version == 0x2)) ? 1 : 0;
558 		break;
559 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
560 	case SIGNAL_TYPE_DVI_DUAL_LINK:
561 	case SIGNAL_TYPE_HDMI_TYPE_A:
562 		ret = (link->hdcp_caps.rx_caps.fields.version == 0x4) ? 1:0;
563 		break;
564 	default:
565 		break;
566 	}
567 
568 	return ret;
569 }
570 
query_hdcp_capability(enum signal_type signal,struct dc_link * link)571 static void query_hdcp_capability(enum signal_type signal, struct dc_link *link)
572 {
573 	struct hdcp_protection_message msg22;
574 	struct hdcp_protection_message msg14;
575 
576 	memset(&msg22, 0, sizeof(struct hdcp_protection_message));
577 	memset(&msg14, 0, sizeof(struct hdcp_protection_message));
578 	memset(link->hdcp_caps.rx_caps.raw, 0,
579 		sizeof(link->hdcp_caps.rx_caps.raw));
580 
581 	if ((link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
582 			link->ddc->transaction_type ==
583 			DDC_TRANSACTION_TYPE_I2C_OVER_AUX) ||
584 			link->connector_signal == SIGNAL_TYPE_EDP) {
585 		msg22.data = link->hdcp_caps.rx_caps.raw;
586 		msg22.length = sizeof(link->hdcp_caps.rx_caps.raw);
587 		msg22.msg_id = HDCP_MESSAGE_ID_RX_CAPS;
588 	} else {
589 		msg22.data = &link->hdcp_caps.rx_caps.fields.version;
590 		msg22.length = sizeof(link->hdcp_caps.rx_caps.fields.version);
591 		msg22.msg_id = HDCP_MESSAGE_ID_HDCP2VERSION;
592 	}
593 	msg22.version = HDCP_VERSION_22;
594 	msg22.link = HDCP_LINK_PRIMARY;
595 	msg22.max_retries = 5;
596 	dc_process_hdcp_msg(signal, link, &msg22);
597 
598 	if (signal == SIGNAL_TYPE_DISPLAY_PORT || signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
599 		enum hdcp_message_status status = HDCP_MESSAGE_UNSUPPORTED;
600 
601 		msg14.data = &link->hdcp_caps.bcaps.raw;
602 		msg14.length = sizeof(link->hdcp_caps.bcaps.raw);
603 		msg14.msg_id = HDCP_MESSAGE_ID_READ_BCAPS;
604 		msg14.version = HDCP_VERSION_14;
605 		msg14.link = HDCP_LINK_PRIMARY;
606 		msg14.max_retries = 5;
607 
608 		status = dc_process_hdcp_msg(signal, link, &msg14);
609 	}
610 
611 }
612 #endif
613 
read_current_link_settings_on_detect(struct dc_link * link)614 static void read_current_link_settings_on_detect(struct dc_link *link)
615 {
616 	union lane_count_set lane_count_set = { {0} };
617 	uint8_t link_bw_set;
618 	uint8_t link_rate_set;
619 	uint32_t read_dpcd_retry_cnt = 10;
620 	enum dc_status status = DC_ERROR_UNEXPECTED;
621 	int i;
622 	union max_down_spread max_down_spread = { {0} };
623 
624 	// Read DPCD 00101h to find out the number of lanes currently set
625 	for (i = 0; i < read_dpcd_retry_cnt; i++) {
626 		status = core_link_read_dpcd(link,
627 					     DP_LANE_COUNT_SET,
628 					     &lane_count_set.raw,
629 					     sizeof(lane_count_set));
630 		/* First DPCD read after VDD ON can fail if the particular board
631 		 * does not have HPD pin wired correctly. So if DPCD read fails,
632 		 * which it should never happen, retry a few times. Target worst
633 		 * case scenario of 80 ms.
634 		 */
635 		if (status == DC_OK) {
636 			link->cur_link_settings.lane_count =
637 					lane_count_set.bits.LANE_COUNT_SET;
638 			break;
639 		}
640 
641 		msleep(8);
642 	}
643 
644 	// Read DPCD 00100h to find if standard link rates are set
645 	core_link_read_dpcd(link, DP_LINK_BW_SET,
646 			    &link_bw_set, sizeof(link_bw_set));
647 
648 	if (link_bw_set == 0) {
649 		if (link->connector_signal == SIGNAL_TYPE_EDP) {
650 			/* If standard link rates are not being used,
651 			 * Read DPCD 00115h to find the edp link rate set used
652 			 */
653 			core_link_read_dpcd(link, DP_LINK_RATE_SET,
654 					    &link_rate_set, sizeof(link_rate_set));
655 
656 			// edp_supported_link_rates_count = 0 for DP
657 			if (link_rate_set < link->dpcd_caps.edp_supported_link_rates_count) {
658 				link->cur_link_settings.link_rate =
659 					link->dpcd_caps.edp_supported_link_rates[link_rate_set];
660 				link->cur_link_settings.link_rate_set = link_rate_set;
661 				link->cur_link_settings.use_link_rate_set = true;
662 			}
663 		} else {
664 			// Link Rate not found. Seamless boot may not work.
665 			ASSERT(false);
666 		}
667 	} else {
668 		link->cur_link_settings.link_rate = link_bw_set;
669 		link->cur_link_settings.use_link_rate_set = false;
670 	}
671 	// Read DPCD 00003h to find the max down spread.
672 	core_link_read_dpcd(link, DP_MAX_DOWNSPREAD,
673 			    &max_down_spread.raw, sizeof(max_down_spread));
674 	link->cur_link_settings.link_spread =
675 		max_down_spread.bits.MAX_DOWN_SPREAD ?
676 		LINK_SPREAD_05_DOWNSPREAD_30KHZ : LINK_SPREAD_DISABLED;
677 }
678 
detect_dp(struct dc_link * link,struct display_sink_capability * sink_caps,bool * converter_disable_audio,struct audio_support * audio_support,enum dc_detect_reason reason)679 static bool detect_dp(struct dc_link *link,
680 		      struct display_sink_capability *sink_caps,
681 		      bool *converter_disable_audio,
682 		      struct audio_support *audio_support,
683 		      enum dc_detect_reason reason)
684 {
685 	bool boot = false;
686 
687 	sink_caps->signal = link_detect_sink(link, reason);
688 	sink_caps->transaction_type =
689 		get_ddc_transaction_type(sink_caps->signal);
690 
691 	if (sink_caps->transaction_type == DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
692 		sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT;
693 		if (!detect_dp_sink_caps(link))
694 			return false;
695 		if (is_mst_supported(link)) {
696 			sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT_MST;
697 			link->type = dc_connection_mst_branch;
698 
699 			dal_ddc_service_set_transaction_type(link->ddc,
700 							     sink_caps->transaction_type);
701 
702 #if defined(CONFIG_DRM_AMD_DC_HDCP)
703 			/* In case of fallback to SST when topology discovery below fails
704 			 * HDCP caps will be querried again later by the upper layer (caller
705 			 * of this function). */
706 			query_hdcp_capability(SIGNAL_TYPE_DISPLAY_PORT_MST, link);
707 #endif
708 			/*
709 			 * This call will initiate MST topology discovery. Which
710 			 * will detect MST ports and add new DRM connector DRM
711 			 * framework. Then read EDID via remote i2c over aux. In
712 			 * the end, will notify DRM detect result and save EDID
713 			 * into DRM framework.
714 			 *
715 			 * .detect is called by .fill_modes.
716 			 * .fill_modes is called by user mode ioctl
717 			 * DRM_IOCTL_MODE_GETCONNECTOR.
718 			 *
719 			 * .get_modes is called by .fill_modes.
720 			 *
721 			 * call .get_modes, AMDGPU DM implementation will create
722 			 * new dc_sink and add to dc_link. For long HPD plug
723 			 * in/out, MST has its own handle.
724 			 *
725 			 * Therefore, just after dc_create, link->sink is not
726 			 * created for MST until user mode app calls
727 			 * DRM_IOCTL_MODE_GETCONNECTOR.
728 			 *
729 			 * Need check ->sink usages in case ->sink = NULL
730 			 * TODO: s3 resume check
731 			 */
732 			if (reason == DETECT_REASON_BOOT)
733 				boot = true;
734 
735 			dm_helpers_dp_update_branch_info(link->ctx, link);
736 
737 			if (!dm_helpers_dp_mst_start_top_mgr(link->ctx,
738 							     link, boot)) {
739 				/* MST not supported */
740 				link->type = dc_connection_single;
741 				sink_caps->signal = SIGNAL_TYPE_DISPLAY_PORT;
742 			}
743 		}
744 
745 		if (link->type != dc_connection_mst_branch &&
746 		    is_dp_active_dongle(link)) {
747 			/* DP active dongles */
748 			link->type = dc_connection_active_dongle;
749 			if (!link->dpcd_caps.sink_count.bits.SINK_COUNT) {
750 				/*
751 				 * active dongle unplug processing for short irq
752 				 */
753 				link_disconnect_sink(link);
754 				return true;
755 			}
756 
757 			if (link->dpcd_caps.dongle_type !=
758 			    DISPLAY_DONGLE_DP_HDMI_CONVERTER)
759 				*converter_disable_audio = true;
760 		}
761 	} else {
762 		/* DP passive dongles */
763 		sink_caps->signal = dp_passive_dongle_detection(link->ddc,
764 								sink_caps,
765 								audio_support);
766 		link->dpcd_caps.dongle_type = sink_caps->dongle_type;
767 	}
768 
769 	return true;
770 }
771 
is_same_edid(struct dc_edid * old_edid,struct dc_edid * new_edid)772 static bool is_same_edid(struct dc_edid *old_edid, struct dc_edid *new_edid)
773 {
774 	if (old_edid->length != new_edid->length)
775 		return false;
776 
777 	if (new_edid->length == 0)
778 		return false;
779 
780 	return (memcmp(old_edid->raw_edid,
781 		       new_edid->raw_edid, new_edid->length) == 0);
782 }
783 
wait_for_entering_dp_alt_mode(struct dc_link * link)784 static bool wait_for_entering_dp_alt_mode(struct dc_link *link)
785 {
786 	/**
787 	 * something is terribly wrong if time out is > 200ms. (5Hz)
788 	 * 500 microseconds * 400 tries us 200 ms
789 	 **/
790 	unsigned int sleep_time_in_microseconds = 500;
791 	unsigned int tries_allowed = 400;
792 	bool is_in_alt_mode;
793 	unsigned long long enter_timestamp;
794 	unsigned long long finish_timestamp;
795 	unsigned long long time_taken_in_ns;
796 	int tries_taken;
797 
798 	DC_LOGGER_INIT(link->ctx->logger);
799 
800 	if (!link->link_enc->funcs->is_in_alt_mode)
801 		return true;
802 
803 	is_in_alt_mode = link->link_enc->funcs->is_in_alt_mode(link->link_enc);
804 	DC_LOG_WARNING("DP Alt mode state on HPD: %d\n", is_in_alt_mode);
805 
806 	if (is_in_alt_mode)
807 		return true;
808 
809 	enter_timestamp = dm_get_timestamp(link->ctx);
810 
811 	for (tries_taken = 0; tries_taken < tries_allowed; tries_taken++) {
812 		udelay(sleep_time_in_microseconds);
813 		/* ask the link if alt mode is enabled, if so return ok */
814 		if (link->link_enc->funcs->is_in_alt_mode(link->link_enc)) {
815 			finish_timestamp = dm_get_timestamp(link->ctx);
816 			time_taken_in_ns =
817 				dm_get_elapse_time_in_ns(link->ctx,
818 							 finish_timestamp,
819 							 enter_timestamp);
820 			DC_LOG_WARNING("Alt mode entered finished after %llu ms\n",
821 				       div_u64(time_taken_in_ns, 1000000));
822 			return true;
823 		}
824 	}
825 	finish_timestamp = dm_get_timestamp(link->ctx);
826 	time_taken_in_ns = dm_get_elapse_time_in_ns(link->ctx, finish_timestamp,
827 						    enter_timestamp);
828 	DC_LOG_WARNING("Alt mode has timed out after %llu ms\n",
829 		       div_u64(time_taken_in_ns, 1000000));
830 	return false;
831 }
832 
833 /**
834  * dc_link_detect() - Detect if a sink is attached to a given link
835  *
836  * link->local_sink is created or destroyed as needed.
837  *
838  * This does not create remote sinks but will trigger DM
839  * to start MST detection if a branch is detected.
840  */
dc_link_detect_helper(struct dc_link * link,enum dc_detect_reason reason)841 static bool dc_link_detect_helper(struct dc_link *link,
842 				  enum dc_detect_reason reason)
843 {
844 	struct dc_sink_init_data sink_init_data = { 0 };
845 	struct display_sink_capability sink_caps = { 0 };
846 	uint8_t i;
847 	bool converter_disable_audio = false;
848 	struct audio_support *aud_support = &link->dc->res_pool->audio_support;
849 	bool same_edid = false;
850 	enum dc_edid_status edid_status;
851 	struct dc_context *dc_ctx = link->ctx;
852 	struct dc_sink *sink = NULL;
853 	struct dc_sink *prev_sink = NULL;
854 	struct dpcd_caps prev_dpcd_caps;
855 	bool same_dpcd = true;
856 	enum dc_connection_type new_connection_type = dc_connection_none;
857 	bool perform_dp_seamless_boot = false;
858 	const uint32_t post_oui_delay = 30; // 30ms
859 
860 	DC_LOGGER_INIT(link->ctx->logger);
861 
862 	if (dc_is_virtual_signal(link->connector_signal))
863 		return false;
864 
865 	if ((link->connector_signal == SIGNAL_TYPE_LVDS ||
866 	     link->connector_signal == SIGNAL_TYPE_EDP) &&
867 	    link->local_sink) {
868 		// need to re-write OUI and brightness in resume case
869 		if (link->connector_signal == SIGNAL_TYPE_EDP) {
870 			dpcd_set_source_specific_data(link);
871 			msleep(post_oui_delay);
872 			dc_link_set_default_brightness_aux(link);
873 			//TODO: use cached
874 		}
875 
876 		return true;
877 	}
878 
879 	if (!dc_link_detect_sink(link, &new_connection_type)) {
880 		BREAK_TO_DEBUGGER();
881 		return false;
882 	}
883 
884 	prev_sink = link->local_sink;
885 	if (prev_sink) {
886 		dc_sink_retain(prev_sink);
887 		memcpy(&prev_dpcd_caps, &link->dpcd_caps, sizeof(struct dpcd_caps));
888 	}
889 
890 	link_disconnect_sink(link);
891 	if (new_connection_type != dc_connection_none) {
892 		link->type = new_connection_type;
893 		link->link_state_valid = false;
894 
895 		/* From Disconnected-to-Connected. */
896 		switch (link->connector_signal) {
897 		case SIGNAL_TYPE_HDMI_TYPE_A: {
898 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
899 			if (aud_support->hdmi_audio_native)
900 				sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
901 			else
902 				sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
903 			break;
904 		}
905 
906 		case SIGNAL_TYPE_DVI_SINGLE_LINK: {
907 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
908 			sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
909 			break;
910 		}
911 
912 		case SIGNAL_TYPE_DVI_DUAL_LINK: {
913 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
914 			sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
915 			break;
916 		}
917 
918 		case SIGNAL_TYPE_LVDS: {
919 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
920 			sink_caps.signal = SIGNAL_TYPE_LVDS;
921 			break;
922 		}
923 
924 		case SIGNAL_TYPE_EDP: {
925 			read_current_link_settings_on_detect(link);
926 
927 			detect_edp_sink_caps(link);
928 			read_current_link_settings_on_detect(link);
929 			sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
930 			sink_caps.signal = SIGNAL_TYPE_EDP;
931 			break;
932 		}
933 
934 		case SIGNAL_TYPE_DISPLAY_PORT: {
935 			/* wa HPD high coming too early*/
936 			if (link->link_enc->features.flags.bits.DP_IS_USB_C == 1) {
937 				/* if alt mode times out, return false */
938 				if (!wait_for_entering_dp_alt_mode(link))
939 					return false;
940 			}
941 
942 			if (!detect_dp(link, &sink_caps,
943 				       &converter_disable_audio,
944 				       aud_support, reason)) {
945 				if (prev_sink)
946 					dc_sink_release(prev_sink);
947 				return false;
948 			}
949 
950 			// Check if dpcp block is the same
951 			if (prev_sink) {
952 				if (memcmp(&link->dpcd_caps, &prev_dpcd_caps,
953 					   sizeof(struct dpcd_caps)))
954 					same_dpcd = false;
955 			}
956 			/* Active dongle downstream unplug*/
957 			if (link->type == dc_connection_active_dongle &&
958 			    link->dpcd_caps.sink_count.bits.SINK_COUNT == 0) {
959 				if (prev_sink)
960 					/* Downstream unplug */
961 					dc_sink_release(prev_sink);
962 				return true;
963 			}
964 
965 			if (link->type == dc_connection_mst_branch) {
966 				LINK_INFO("link=%d, mst branch is now Connected\n",
967 					  link->link_index);
968 				/* Need to setup mst link_cap struct here
969 				 * otherwise dc_link_detect() will leave mst link_cap
970 				 * empty which leads to allocate_mst_payload() has "0"
971 				 * pbn_per_slot value leading to exception on dc_fixpt_div()
972 				 */
973 				dp_verify_mst_link_cap(link);
974 
975 				if (prev_sink)
976 					dc_sink_release(prev_sink);
977 				return false;
978 			}
979 
980 			// For seamless boot, to skip verify link cap, we read UEFI settings and set them as verified.
981 			if (reason == DETECT_REASON_BOOT &&
982 			    !dc_ctx->dc->config.power_down_display_on_boot &&
983 			    link->link_status.link_active)
984 				perform_dp_seamless_boot = true;
985 
986 			if (perform_dp_seamless_boot) {
987 				read_current_link_settings_on_detect(link);
988 				link->verified_link_cap = link->reported_link_cap;
989 			}
990 
991 			break;
992 		}
993 
994 		default:
995 			DC_ERROR("Invalid connector type! signal:%d\n",
996 				 link->connector_signal);
997 			if (prev_sink)
998 				dc_sink_release(prev_sink);
999 			return false;
1000 		} /* switch() */
1001 
1002 		if (link->dpcd_caps.sink_count.bits.SINK_COUNT)
1003 			link->dpcd_sink_count =
1004 				link->dpcd_caps.sink_count.bits.SINK_COUNT;
1005 		else
1006 			link->dpcd_sink_count = 1;
1007 
1008 		dal_ddc_service_set_transaction_type(link->ddc,
1009 						     sink_caps.transaction_type);
1010 
1011 		link->aux_mode =
1012 			dal_ddc_service_is_in_aux_transaction_mode(link->ddc);
1013 
1014 		sink_init_data.link = link;
1015 		sink_init_data.sink_signal = sink_caps.signal;
1016 
1017 		sink = dc_sink_create(&sink_init_data);
1018 		if (!sink) {
1019 			DC_ERROR("Failed to create sink!\n");
1020 			if (prev_sink)
1021 				dc_sink_release(prev_sink);
1022 			return false;
1023 		}
1024 
1025 		sink->link->dongle_max_pix_clk = sink_caps.max_hdmi_pixel_clock;
1026 		sink->converter_disable_audio = converter_disable_audio;
1027 
1028 		/* dc_sink_create returns a new reference */
1029 		link->local_sink = sink;
1030 
1031 		edid_status = dm_helpers_read_local_edid(link->ctx,
1032 							 link, sink);
1033 
1034 		switch (edid_status) {
1035 		case EDID_BAD_CHECKSUM:
1036 			DC_LOG_ERROR("EDID checksum invalid.\n");
1037 			break;
1038 		case EDID_NO_RESPONSE:
1039 			DC_LOG_ERROR("No EDID read.\n");
1040 			/*
1041 			 * Abort detection for non-DP connectors if we have
1042 			 * no EDID
1043 			 *
1044 			 * DP needs to report as connected if HDP is high
1045 			 * even if we have no EDID in order to go to
1046 			 * fail-safe mode
1047 			 */
1048 			if (dc_is_hdmi_signal(link->connector_signal) ||
1049 			    dc_is_dvi_signal(link->connector_signal)) {
1050 				if (prev_sink)
1051 					dc_sink_release(prev_sink);
1052 				link_disconnect_sink(link);
1053 
1054 				return false;
1055 			}
1056 			/*
1057 			 * Abort detection for DP connectors if we have
1058 			 * no EDID and connector is active converter
1059 			 * as there are no display downstream
1060 			 *
1061 			 */
1062 			if (dc_is_dp_sst_signal(link->connector_signal) &&
1063 				(link->dpcd_caps.dongle_type ==
1064 						DISPLAY_DONGLE_DP_VGA_CONVERTER ||
1065 				link->dpcd_caps.dongle_type ==
1066 						DISPLAY_DONGLE_DP_DVI_CONVERTER)) {
1067 				if (prev_sink)
1068 					dc_sink_release(prev_sink);
1069 				link_disconnect_sink(link);
1070 
1071 				return false;
1072 			}
1073 		default:
1074 			break;
1075 		}
1076 
1077 		if (link->local_sink->edid_caps.panel_patch.disable_fec)
1078 			link->ctx->dc->debug.disable_fec = true;
1079 
1080 		// Check if edid is the same
1081 		if ((prev_sink) &&
1082 		    (edid_status == EDID_THE_SAME || edid_status == EDID_OK))
1083 			same_edid = is_same_edid(&prev_sink->dc_edid,
1084 						 &sink->dc_edid);
1085 
1086 		if (sink->edid_caps.panel_patch.skip_scdc_overwrite)
1087 			link->ctx->dc->debug.hdmi20_disable = true;
1088 
1089 		if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
1090 		    sink_caps.transaction_type ==
1091 		    DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
1092 			/*
1093 			 * TODO debug why Dell 2413 doesn't like
1094 			 *  two link trainings
1095 			 */
1096 #if defined(CONFIG_DRM_AMD_DC_HDCP)
1097 			query_hdcp_capability(sink->sink_signal, link);
1098 #endif
1099 
1100 			// verify link cap for SST non-seamless boot
1101 			if (!perform_dp_seamless_boot)
1102 				dp_verify_link_cap_with_retries(link,
1103 								&link->reported_link_cap,
1104 								LINK_TRAINING_MAX_VERIFY_RETRY);
1105 		} else {
1106 			// If edid is the same, then discard new sink and revert back to original sink
1107 			if (same_edid) {
1108 				link_disconnect_remap(prev_sink, link);
1109 				sink = prev_sink;
1110 				prev_sink = NULL;
1111 			}
1112 #if defined(CONFIG_DRM_AMD_DC_HDCP)
1113 			query_hdcp_capability(sink->sink_signal, link);
1114 #endif
1115 		}
1116 
1117 		/* HDMI-DVI Dongle */
1118 		if (sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A &&
1119 		    !sink->edid_caps.edid_hdmi)
1120 			sink->sink_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1121 
1122 		/* Connectivity log: detection */
1123 		for (i = 0; i < sink->dc_edid.length / DC_EDID_BLOCK_SIZE; i++) {
1124 			CONN_DATA_DETECT(link,
1125 					 &sink->dc_edid.raw_edid[i * DC_EDID_BLOCK_SIZE],
1126 					 DC_EDID_BLOCK_SIZE,
1127 					 "%s: [Block %d] ", sink->edid_caps.display_name, i);
1128 		}
1129 
1130 		DC_LOG_DETECTION_EDID_PARSER("%s: "
1131 			"manufacturer_id = %X, "
1132 			"product_id = %X, "
1133 			"serial_number = %X, "
1134 			"manufacture_week = %d, "
1135 			"manufacture_year = %d, "
1136 			"display_name = %s, "
1137 			"speaker_flag = %d, "
1138 			"audio_mode_count = %d\n",
1139 			__func__,
1140 			sink->edid_caps.manufacturer_id,
1141 			sink->edid_caps.product_id,
1142 			sink->edid_caps.serial_number,
1143 			sink->edid_caps.manufacture_week,
1144 			sink->edid_caps.manufacture_year,
1145 			sink->edid_caps.display_name,
1146 			sink->edid_caps.speaker_flags,
1147 			sink->edid_caps.audio_mode_count);
1148 
1149 		for (i = 0; i < sink->edid_caps.audio_mode_count; i++) {
1150 			DC_LOG_DETECTION_EDID_PARSER("%s: mode number = %d, "
1151 				"format_code = %d, "
1152 				"channel_count = %d, "
1153 				"sample_rate = %d, "
1154 				"sample_size = %d\n",
1155 				__func__,
1156 				i,
1157 				sink->edid_caps.audio_modes[i].format_code,
1158 				sink->edid_caps.audio_modes[i].channel_count,
1159 				sink->edid_caps.audio_modes[i].sample_rate,
1160 				sink->edid_caps.audio_modes[i].sample_size);
1161 		}
1162 	} else {
1163 		/* From Connected-to-Disconnected. */
1164 		if (link->type == dc_connection_mst_branch) {
1165 			LINK_INFO("link=%d, mst branch is now Disconnected\n",
1166 				  link->link_index);
1167 
1168 			dm_helpers_dp_mst_stop_top_mgr(link->ctx, link);
1169 
1170 			link->mst_stream_alloc_table.stream_count = 0;
1171 			memset(link->mst_stream_alloc_table.stream_allocations,
1172 			       0,
1173 			       sizeof(link->mst_stream_alloc_table.stream_allocations));
1174 		}
1175 
1176 		link->type = dc_connection_none;
1177 		sink_caps.signal = SIGNAL_TYPE_NONE;
1178 		/* When we unplug a passive DP-HDMI dongle connection, dongle_max_pix_clk
1179 		 *  is not cleared. If we emulate a DP signal on this connection, it thinks
1180 		 *  the dongle is still there and limits the number of modes we can emulate.
1181 		 *  Clear dongle_max_pix_clk on disconnect to fix this
1182 		 */
1183 		link->dongle_max_pix_clk = 0;
1184 	}
1185 
1186 	LINK_INFO("link=%d, dc_sink_in=%p is now %s prev_sink=%p dpcd same=%d edid same=%d\n",
1187 		  link->link_index, sink,
1188 		  (sink_caps.signal ==
1189 		   SIGNAL_TYPE_NONE ? "Disconnected" : "Connected"),
1190 		  prev_sink, same_dpcd, same_edid);
1191 
1192 	if (prev_sink)
1193 		dc_sink_release(prev_sink);
1194 
1195 	return true;
1196 }
1197 
dc_link_detect(struct dc_link * link,enum dc_detect_reason reason)1198 bool dc_link_detect(struct dc_link *link, enum dc_detect_reason reason)
1199 {
1200 	const struct dc *dc = link->dc;
1201 	bool ret;
1202 
1203 	/* get out of low power state */
1204 	clk_mgr_exit_optimized_pwr_state(dc, dc->clk_mgr);
1205 
1206 	ret = dc_link_detect_helper(link, reason);
1207 
1208 	/* Go back to power optimized state */
1209 	clk_mgr_optimize_pwr_state(dc, dc->clk_mgr);
1210 
1211 	return ret;
1212 }
1213 
dc_link_get_hpd_state(struct dc_link * dc_link)1214 bool dc_link_get_hpd_state(struct dc_link *dc_link)
1215 {
1216 	uint32_t state;
1217 
1218 	dal_gpio_lock_pin(dc_link->hpd_gpio);
1219 	dal_gpio_get_value(dc_link->hpd_gpio, &state);
1220 	dal_gpio_unlock_pin(dc_link->hpd_gpio);
1221 
1222 	return state;
1223 }
1224 
get_hpd_line(struct dc_link * link)1225 static enum hpd_source_id get_hpd_line(struct dc_link *link)
1226 {
1227 	struct gpio *hpd;
1228 	enum hpd_source_id hpd_id = HPD_SOURCEID_UNKNOWN;
1229 
1230 	hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
1231 			   link->ctx->gpio_service);
1232 
1233 	if (hpd) {
1234 		switch (dal_irq_get_source(hpd)) {
1235 		case DC_IRQ_SOURCE_HPD1:
1236 			hpd_id = HPD_SOURCEID1;
1237 		break;
1238 		case DC_IRQ_SOURCE_HPD2:
1239 			hpd_id = HPD_SOURCEID2;
1240 		break;
1241 		case DC_IRQ_SOURCE_HPD3:
1242 			hpd_id = HPD_SOURCEID3;
1243 		break;
1244 		case DC_IRQ_SOURCE_HPD4:
1245 			hpd_id = HPD_SOURCEID4;
1246 		break;
1247 		case DC_IRQ_SOURCE_HPD5:
1248 			hpd_id = HPD_SOURCEID5;
1249 		break;
1250 		case DC_IRQ_SOURCE_HPD6:
1251 			hpd_id = HPD_SOURCEID6;
1252 		break;
1253 		default:
1254 			BREAK_TO_DEBUGGER();
1255 		break;
1256 		}
1257 
1258 		dal_gpio_destroy_irq(&hpd);
1259 	}
1260 
1261 	return hpd_id;
1262 }
1263 
get_ddc_line(struct dc_link * link)1264 static enum channel_id get_ddc_line(struct dc_link *link)
1265 {
1266 	struct ddc *ddc;
1267 	enum channel_id channel = CHANNEL_ID_UNKNOWN;
1268 
1269 	ddc = dal_ddc_service_get_ddc_pin(link->ddc);
1270 
1271 	if (ddc) {
1272 		switch (dal_ddc_get_line(ddc)) {
1273 		case GPIO_DDC_LINE_DDC1:
1274 			channel = CHANNEL_ID_DDC1;
1275 			break;
1276 		case GPIO_DDC_LINE_DDC2:
1277 			channel = CHANNEL_ID_DDC2;
1278 			break;
1279 		case GPIO_DDC_LINE_DDC3:
1280 			channel = CHANNEL_ID_DDC3;
1281 			break;
1282 		case GPIO_DDC_LINE_DDC4:
1283 			channel = CHANNEL_ID_DDC4;
1284 			break;
1285 		case GPIO_DDC_LINE_DDC5:
1286 			channel = CHANNEL_ID_DDC5;
1287 			break;
1288 		case GPIO_DDC_LINE_DDC6:
1289 			channel = CHANNEL_ID_DDC6;
1290 			break;
1291 		case GPIO_DDC_LINE_DDC_VGA:
1292 			channel = CHANNEL_ID_DDC_VGA;
1293 			break;
1294 		case GPIO_DDC_LINE_I2C_PAD:
1295 			channel = CHANNEL_ID_I2C_PAD;
1296 			break;
1297 		default:
1298 			BREAK_TO_DEBUGGER();
1299 			break;
1300 		}
1301 	}
1302 
1303 	return channel;
1304 }
1305 
translate_encoder_to_transmitter(struct graphics_object_id encoder)1306 static enum transmitter translate_encoder_to_transmitter(struct graphics_object_id encoder)
1307 {
1308 	switch (encoder.id) {
1309 	case ENCODER_ID_INTERNAL_UNIPHY:
1310 		switch (encoder.enum_id) {
1311 		case ENUM_ID_1:
1312 			return TRANSMITTER_UNIPHY_A;
1313 		case ENUM_ID_2:
1314 			return TRANSMITTER_UNIPHY_B;
1315 		default:
1316 			return TRANSMITTER_UNKNOWN;
1317 		}
1318 	break;
1319 	case ENCODER_ID_INTERNAL_UNIPHY1:
1320 		switch (encoder.enum_id) {
1321 		case ENUM_ID_1:
1322 			return TRANSMITTER_UNIPHY_C;
1323 		case ENUM_ID_2:
1324 			return TRANSMITTER_UNIPHY_D;
1325 		default:
1326 			return TRANSMITTER_UNKNOWN;
1327 		}
1328 	break;
1329 	case ENCODER_ID_INTERNAL_UNIPHY2:
1330 		switch (encoder.enum_id) {
1331 		case ENUM_ID_1:
1332 			return TRANSMITTER_UNIPHY_E;
1333 		case ENUM_ID_2:
1334 			return TRANSMITTER_UNIPHY_F;
1335 		default:
1336 			return TRANSMITTER_UNKNOWN;
1337 		}
1338 	break;
1339 	case ENCODER_ID_INTERNAL_UNIPHY3:
1340 		switch (encoder.enum_id) {
1341 		case ENUM_ID_1:
1342 			return TRANSMITTER_UNIPHY_G;
1343 		default:
1344 			return TRANSMITTER_UNKNOWN;
1345 		}
1346 	break;
1347 	case ENCODER_ID_EXTERNAL_NUTMEG:
1348 		switch (encoder.enum_id) {
1349 		case ENUM_ID_1:
1350 			return TRANSMITTER_NUTMEG_CRT;
1351 		default:
1352 			return TRANSMITTER_UNKNOWN;
1353 		}
1354 	break;
1355 	case ENCODER_ID_EXTERNAL_TRAVIS:
1356 		switch (encoder.enum_id) {
1357 		case ENUM_ID_1:
1358 			return TRANSMITTER_TRAVIS_CRT;
1359 		case ENUM_ID_2:
1360 			return TRANSMITTER_TRAVIS_LCD;
1361 		default:
1362 			return TRANSMITTER_UNKNOWN;
1363 		}
1364 	break;
1365 	default:
1366 		return TRANSMITTER_UNKNOWN;
1367 	}
1368 }
1369 
dc_link_construct(struct dc_link * link,const struct link_init_data * init_params)1370 static bool dc_link_construct(struct dc_link *link,
1371 			      const struct link_init_data *init_params)
1372 {
1373 	uint8_t i;
1374 	struct ddc_service_init_data ddc_service_init_data = { { 0 } };
1375 	struct dc_context *dc_ctx = init_params->ctx;
1376 	struct encoder_init_data enc_init_data = { 0 };
1377 	struct panel_cntl_init_data panel_cntl_init_data = { 0 };
1378 	struct integrated_info info = {{{ 0 }}};
1379 	struct dc_bios *bios = init_params->dc->ctx->dc_bios;
1380 	const struct dc_vbios_funcs *bp_funcs = bios->funcs;
1381 
1382 	DC_LOGGER_INIT(dc_ctx->logger);
1383 
1384 	link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
1385 	link->irq_source_hpd_rx = DC_IRQ_SOURCE_INVALID;
1386 
1387 	link->link_status.dpcd_caps = &link->dpcd_caps;
1388 
1389 	link->dc = init_params->dc;
1390 	link->ctx = dc_ctx;
1391 	link->link_index = init_params->link_index;
1392 
1393 	memset(&link->preferred_training_settings, 0,
1394 	       sizeof(struct dc_link_training_overrides));
1395 	memset(&link->preferred_link_setting, 0,
1396 	       sizeof(struct dc_link_settings));
1397 
1398 	link->link_id =
1399 		bios->funcs->get_connector_id(bios, init_params->connector_index);
1400 
1401 	if (link->link_id.type != OBJECT_TYPE_CONNECTOR) {
1402 		dm_output_to_console("%s: Invalid Connector ObjectID from Adapter Service for connector index:%d! type %d expected %d\n",
1403 				     __func__, init_params->connector_index,
1404 				     link->link_id.type, OBJECT_TYPE_CONNECTOR);
1405 		goto create_fail;
1406 	}
1407 
1408 	if (link->dc->res_pool->funcs->link_init)
1409 		link->dc->res_pool->funcs->link_init(link);
1410 
1411 	link->hpd_gpio = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
1412 				      link->ctx->gpio_service);
1413 	if (link->hpd_gpio) {
1414 		dal_gpio_open(link->hpd_gpio, GPIO_MODE_INTERRUPT);
1415 		dal_gpio_unlock_pin(link->hpd_gpio);
1416 		link->irq_source_hpd = dal_irq_get_source(link->hpd_gpio);
1417 	}
1418 
1419 	switch (link->link_id.id) {
1420 	case CONNECTOR_ID_HDMI_TYPE_A:
1421 		link->connector_signal = SIGNAL_TYPE_HDMI_TYPE_A;
1422 
1423 		break;
1424 	case CONNECTOR_ID_SINGLE_LINK_DVID:
1425 	case CONNECTOR_ID_SINGLE_LINK_DVII:
1426 		link->connector_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1427 		break;
1428 	case CONNECTOR_ID_DUAL_LINK_DVID:
1429 	case CONNECTOR_ID_DUAL_LINK_DVII:
1430 		link->connector_signal = SIGNAL_TYPE_DVI_DUAL_LINK;
1431 		break;
1432 	case CONNECTOR_ID_DISPLAY_PORT:
1433 		link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
1434 
1435 		if (link->hpd_gpio)
1436 			link->irq_source_hpd_rx =
1437 					dal_irq_get_rx_source(link->hpd_gpio);
1438 
1439 		break;
1440 	case CONNECTOR_ID_EDP:
1441 		link->connector_signal = SIGNAL_TYPE_EDP;
1442 
1443 		if (link->hpd_gpio) {
1444 			link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
1445 			link->irq_source_hpd_rx =
1446 					dal_irq_get_rx_source(link->hpd_gpio);
1447 		}
1448 
1449 		break;
1450 	case CONNECTOR_ID_LVDS:
1451 		link->connector_signal = SIGNAL_TYPE_LVDS;
1452 		break;
1453 	default:
1454 		DC_LOG_WARNING("Unsupported Connector type:%d!\n",
1455 			       link->link_id.id);
1456 		goto create_fail;
1457 	}
1458 
1459 	/* TODO: #DAL3 Implement id to str function.*/
1460 	LINK_INFO("Connector[%d] description:"
1461 		  "signal %d\n",
1462 		  init_params->connector_index,
1463 		  link->connector_signal);
1464 
1465 	ddc_service_init_data.ctx = link->ctx;
1466 	ddc_service_init_data.id = link->link_id;
1467 	ddc_service_init_data.link = link;
1468 	link->ddc = dal_ddc_service_create(&ddc_service_init_data);
1469 
1470 	if (!link->ddc) {
1471 		DC_ERROR("Failed to create ddc_service!\n");
1472 		goto ddc_create_fail;
1473 	}
1474 
1475 	if (!link->ddc->ddc_pin) {
1476 		DC_ERROR("Failed to get I2C info for connector!\n");
1477 		goto ddc_create_fail;
1478 	}
1479 
1480 	link->ddc_hw_inst =
1481 		dal_ddc_get_line(dal_ddc_service_get_ddc_pin(link->ddc));
1482 
1483 
1484 	if (link->dc->res_pool->funcs->panel_cntl_create &&
1485 		(link->link_id.id == CONNECTOR_ID_EDP ||
1486 			link->link_id.id == CONNECTOR_ID_LVDS)) {
1487 		panel_cntl_init_data.ctx = dc_ctx;
1488 		panel_cntl_init_data.inst = 0;
1489 		link->panel_cntl =
1490 			link->dc->res_pool->funcs->panel_cntl_create(
1491 								&panel_cntl_init_data);
1492 
1493 		if (link->panel_cntl == NULL) {
1494 			DC_ERROR("Failed to create link panel_cntl!\n");
1495 			goto panel_cntl_create_fail;
1496 		}
1497 	}
1498 
1499 	enc_init_data.ctx = dc_ctx;
1500 	bp_funcs->get_src_obj(dc_ctx->dc_bios, link->link_id, 0,
1501 			      &enc_init_data.encoder);
1502 	enc_init_data.connector = link->link_id;
1503 	enc_init_data.channel = get_ddc_line(link);
1504 	enc_init_data.hpd_source = get_hpd_line(link);
1505 
1506 	link->hpd_src = enc_init_data.hpd_source;
1507 
1508 	enc_init_data.transmitter =
1509 		translate_encoder_to_transmitter(enc_init_data.encoder);
1510 	link->link_enc =
1511 		link->dc->res_pool->funcs->link_enc_create(&enc_init_data);
1512 
1513 	if (!link->link_enc) {
1514 		DC_ERROR("Failed to create link encoder!\n");
1515 		goto link_enc_create_fail;
1516 	}
1517 
1518 	link->link_enc_hw_inst = link->link_enc->transmitter;
1519 
1520 	for (i = 0; i < 4; i++) {
1521 		if (bp_funcs->get_device_tag(dc_ctx->dc_bios,
1522 					     link->link_id, i,
1523 					     &link->device_tag) != BP_RESULT_OK) {
1524 			DC_ERROR("Failed to find device tag!\n");
1525 			goto device_tag_fail;
1526 		}
1527 
1528 		/* Look for device tag that matches connector signal,
1529 		 * CRT for rgb, LCD for other supported signal tyes
1530 		 */
1531 		if (!bp_funcs->is_device_id_supported(dc_ctx->dc_bios,
1532 						      link->device_tag.dev_id))
1533 			continue;
1534 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_CRT &&
1535 		    link->connector_signal != SIGNAL_TYPE_RGB)
1536 			continue;
1537 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_LCD &&
1538 		    link->connector_signal == SIGNAL_TYPE_RGB)
1539 			continue;
1540 		break;
1541 	}
1542 
1543 	if (bios->integrated_info)
1544 		info = *bios->integrated_info;
1545 
1546 	/* Look for channel mapping corresponding to connector and device tag */
1547 	for (i = 0; i < MAX_NUMBER_OF_EXT_DISPLAY_PATH; i++) {
1548 		struct external_display_path *path =
1549 			&info.ext_disp_conn_info.path[i];
1550 
1551 		if (path->device_connector_id.enum_id == link->link_id.enum_id &&
1552 		    path->device_connector_id.id == link->link_id.id &&
1553 		    path->device_connector_id.type == link->link_id.type) {
1554 			if (link->device_tag.acpi_device != 0 &&
1555 			    path->device_acpi_enum == link->device_tag.acpi_device) {
1556 				link->ddi_channel_mapping = path->channel_mapping;
1557 				link->chip_caps = path->caps;
1558 			} else if (path->device_tag ==
1559 				   link->device_tag.dev_id.raw_device_tag) {
1560 				link->ddi_channel_mapping = path->channel_mapping;
1561 				link->chip_caps = path->caps;
1562 			}
1563 			break;
1564 		}
1565 	}
1566 
1567 	if (bios->funcs->get_atom_dc_golden_table)
1568 		bios->funcs->get_atom_dc_golden_table(bios);
1569 
1570 	/*
1571 	 * TODO check if GPIO programmed correctly
1572 	 *
1573 	 * If GPIO isn't programmed correctly HPD might not rise or drain
1574 	 * fast enough, leading to bounces.
1575 	 */
1576 	program_hpd_filter(link);
1577 
1578 	link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
1579 
1580 	return true;
1581 device_tag_fail:
1582 	link->link_enc->funcs->destroy(&link->link_enc);
1583 link_enc_create_fail:
1584 	if (link->panel_cntl != NULL)
1585 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
1586 panel_cntl_create_fail:
1587 	dal_ddc_service_destroy(&link->ddc);
1588 ddc_create_fail:
1589 create_fail:
1590 
1591 	if (link->hpd_gpio) {
1592 		dal_gpio_destroy_irq(&link->hpd_gpio);
1593 		link->hpd_gpio = NULL;
1594 	}
1595 
1596 	return false;
1597 }
1598 
1599 /*******************************************************************************
1600  * Public functions
1601  ******************************************************************************/
link_create(const struct link_init_data * init_params)1602 struct dc_link *link_create(const struct link_init_data *init_params)
1603 {
1604 	struct dc_link *link =
1605 			kzalloc(sizeof(*link), GFP_KERNEL);
1606 
1607 	if (NULL == link)
1608 		goto alloc_fail;
1609 
1610 	if (false == dc_link_construct(link, init_params))
1611 		goto construct_fail;
1612 
1613 	return link;
1614 
1615 construct_fail:
1616 	kfree(link);
1617 
1618 alloc_fail:
1619 	return NULL;
1620 }
1621 
link_destroy(struct dc_link ** link)1622 void link_destroy(struct dc_link **link)
1623 {
1624 	dc_link_destruct(*link);
1625 	kfree(*link);
1626 	*link = NULL;
1627 }
1628 
enable_stream_features(struct pipe_ctx * pipe_ctx)1629 static void enable_stream_features(struct pipe_ctx *pipe_ctx)
1630 {
1631 	struct dc_stream_state *stream = pipe_ctx->stream;
1632 	struct dc_link *link = stream->link;
1633 	union down_spread_ctrl old_downspread;
1634 	union down_spread_ctrl new_downspread;
1635 
1636 	core_link_read_dpcd(link, DP_DOWNSPREAD_CTRL,
1637 			&old_downspread.raw, sizeof(old_downspread));
1638 
1639 	new_downspread.raw = old_downspread.raw;
1640 
1641 	new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
1642 			(stream->ignore_msa_timing_param) ? 1 : 0;
1643 
1644 	if (new_downspread.raw != old_downspread.raw) {
1645 		core_link_write_dpcd(link, DP_DOWNSPREAD_CTRL,
1646 			&new_downspread.raw, sizeof(new_downspread));
1647 	}
1648 }
1649 
enable_link_dp(struct dc_state * state,struct pipe_ctx * pipe_ctx)1650 static enum dc_status enable_link_dp(struct dc_state *state,
1651 				     struct pipe_ctx *pipe_ctx)
1652 {
1653 	struct dc_stream_state *stream = pipe_ctx->stream;
1654 	enum dc_status status;
1655 	bool skip_video_pattern;
1656 	struct dc_link *link = stream->link;
1657 	struct dc_link_settings link_settings = {0};
1658 	bool fec_enable;
1659 	int i;
1660 	bool apply_seamless_boot_optimization = false;
1661 	uint32_t bl_oled_enable_delay = 50; // in ms
1662 	const uint32_t post_oui_delay = 30; // 30ms
1663 
1664 	// check for seamless boot
1665 	for (i = 0; i < state->stream_count; i++) {
1666 		if (state->streams[i]->apply_seamless_boot_optimization) {
1667 			apply_seamless_boot_optimization = true;
1668 			break;
1669 		}
1670 	}
1671 
1672 	/* get link settings for video mode timing */
1673 	decide_link_settings(stream, &link_settings);
1674 
1675 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP) {
1676 		/*in case it is not on*/
1677 		link->dc->hwss.edp_power_control(link, true);
1678 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
1679 	}
1680 
1681 	pipe_ctx->stream_res.pix_clk_params.requested_sym_clk =
1682 			link_settings.link_rate * LINK_RATE_REF_FREQ_IN_KHZ;
1683 	if (state->clk_mgr && !apply_seamless_boot_optimization)
1684 		state->clk_mgr->funcs->update_clocks(state->clk_mgr,
1685 						     state, false);
1686 
1687 	// during mode switch we do DP_SET_POWER off then on, and OUI is lost
1688 	dpcd_set_source_specific_data(link);
1689 	if (link->dpcd_sink_ext_caps.raw != 0)
1690 		msleep(post_oui_delay);
1691 
1692 	skip_video_pattern = true;
1693 
1694 	if (link_settings.link_rate == LINK_RATE_LOW)
1695 		skip_video_pattern = false;
1696 
1697 	if (perform_link_training_with_retries(&link_settings,
1698 					       skip_video_pattern,
1699 					       LINK_TRAINING_ATTEMPTS,
1700 					       pipe_ctx,
1701 					       pipe_ctx->stream->signal)) {
1702 		link->cur_link_settings = link_settings;
1703 		status = DC_OK;
1704 	} else {
1705 		status = DC_FAIL_DP_LINK_TRAINING;
1706 	}
1707 
1708 	if (link->preferred_training_settings.fec_enable)
1709 		fec_enable = *link->preferred_training_settings.fec_enable;
1710 	else
1711 		fec_enable = true;
1712 
1713 	dp_set_fec_enable(link, fec_enable);
1714 
1715 	// during mode set we do DP_SET_POWER off then on, aux writes are lost
1716 	if (link->dpcd_sink_ext_caps.bits.oled == 1 ||
1717 		link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1 ||
1718 		link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1) {
1719 		dc_link_set_default_brightness_aux(link); // TODO: use cached if known
1720 		if (link->dpcd_sink_ext_caps.bits.oled == 1)
1721 			msleep(bl_oled_enable_delay);
1722 		dc_link_backlight_enable_aux(link, true);
1723 	}
1724 
1725 	return status;
1726 }
1727 
enable_link_edp(struct dc_state * state,struct pipe_ctx * pipe_ctx)1728 static enum dc_status enable_link_edp(
1729 		struct dc_state *state,
1730 		struct pipe_ctx *pipe_ctx)
1731 {
1732 	enum dc_status status;
1733 
1734 	status = enable_link_dp(state, pipe_ctx);
1735 
1736 	return status;
1737 }
1738 
enable_link_dp_mst(struct dc_state * state,struct pipe_ctx * pipe_ctx)1739 static enum dc_status enable_link_dp_mst(
1740 		struct dc_state *state,
1741 		struct pipe_ctx *pipe_ctx)
1742 {
1743 	struct dc_link *link = pipe_ctx->stream->link;
1744 
1745 	/* sink signal type after MST branch is MST. Multiple MST sinks
1746 	 * share one link. Link DP PHY is enable or training only once.
1747 	 */
1748 	if (link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN)
1749 		return DC_OK;
1750 
1751 	/* clear payload table */
1752 	dm_helpers_dp_mst_clear_payload_allocation_table(link->ctx, link);
1753 
1754 	/* to make sure the pending down rep can be processed
1755 	 * before enabling the link
1756 	 */
1757 	dm_helpers_dp_mst_poll_pending_down_reply(link->ctx, link);
1758 
1759 	/* set the sink to MST mode before enabling the link */
1760 	dp_enable_mst_on_sink(link, true);
1761 
1762 	return enable_link_dp(state, pipe_ctx);
1763 }
1764 
get_ext_hdmi_settings(struct pipe_ctx * pipe_ctx,enum engine_id eng_id,struct ext_hdmi_settings * settings)1765 static bool get_ext_hdmi_settings(struct pipe_ctx *pipe_ctx,
1766 		enum engine_id eng_id,
1767 		struct ext_hdmi_settings *settings)
1768 {
1769 	bool result = false;
1770 	int i = 0;
1771 	struct integrated_info *integrated_info =
1772 			pipe_ctx->stream->ctx->dc_bios->integrated_info;
1773 
1774 	if (integrated_info == NULL)
1775 		return false;
1776 
1777 	/*
1778 	 * Get retimer settings from sbios for passing SI eye test for DCE11
1779 	 * The setting values are varied based on board revision and port id
1780 	 * Therefore the setting values of each ports is passed by sbios.
1781 	 */
1782 
1783 	// Check if current bios contains ext Hdmi settings
1784 	if (integrated_info->gpu_cap_info & 0x20) {
1785 		switch (eng_id) {
1786 		case ENGINE_ID_DIGA:
1787 			settings->slv_addr = integrated_info->dp0_ext_hdmi_slv_addr;
1788 			settings->reg_num = integrated_info->dp0_ext_hdmi_6g_reg_num;
1789 			settings->reg_num_6g = integrated_info->dp0_ext_hdmi_6g_reg_num;
1790 			memmove(settings->reg_settings,
1791 					integrated_info->dp0_ext_hdmi_reg_settings,
1792 					sizeof(integrated_info->dp0_ext_hdmi_reg_settings));
1793 			memmove(settings->reg_settings_6g,
1794 					integrated_info->dp0_ext_hdmi_6g_reg_settings,
1795 					sizeof(integrated_info->dp0_ext_hdmi_6g_reg_settings));
1796 			result = true;
1797 			break;
1798 		case ENGINE_ID_DIGB:
1799 			settings->slv_addr = integrated_info->dp1_ext_hdmi_slv_addr;
1800 			settings->reg_num = integrated_info->dp1_ext_hdmi_6g_reg_num;
1801 			settings->reg_num_6g = integrated_info->dp1_ext_hdmi_6g_reg_num;
1802 			memmove(settings->reg_settings,
1803 					integrated_info->dp1_ext_hdmi_reg_settings,
1804 					sizeof(integrated_info->dp1_ext_hdmi_reg_settings));
1805 			memmove(settings->reg_settings_6g,
1806 					integrated_info->dp1_ext_hdmi_6g_reg_settings,
1807 					sizeof(integrated_info->dp1_ext_hdmi_6g_reg_settings));
1808 			result = true;
1809 			break;
1810 		case ENGINE_ID_DIGC:
1811 			settings->slv_addr = integrated_info->dp2_ext_hdmi_slv_addr;
1812 			settings->reg_num = integrated_info->dp2_ext_hdmi_6g_reg_num;
1813 			settings->reg_num_6g = integrated_info->dp2_ext_hdmi_6g_reg_num;
1814 			memmove(settings->reg_settings,
1815 					integrated_info->dp2_ext_hdmi_reg_settings,
1816 					sizeof(integrated_info->dp2_ext_hdmi_reg_settings));
1817 			memmove(settings->reg_settings_6g,
1818 					integrated_info->dp2_ext_hdmi_6g_reg_settings,
1819 					sizeof(integrated_info->dp2_ext_hdmi_6g_reg_settings));
1820 			result = true;
1821 			break;
1822 		case ENGINE_ID_DIGD:
1823 			settings->slv_addr = integrated_info->dp3_ext_hdmi_slv_addr;
1824 			settings->reg_num = integrated_info->dp3_ext_hdmi_6g_reg_num;
1825 			settings->reg_num_6g = integrated_info->dp3_ext_hdmi_6g_reg_num;
1826 			memmove(settings->reg_settings,
1827 					integrated_info->dp3_ext_hdmi_reg_settings,
1828 					sizeof(integrated_info->dp3_ext_hdmi_reg_settings));
1829 			memmove(settings->reg_settings_6g,
1830 					integrated_info->dp3_ext_hdmi_6g_reg_settings,
1831 					sizeof(integrated_info->dp3_ext_hdmi_6g_reg_settings));
1832 			result = true;
1833 			break;
1834 		default:
1835 			break;
1836 		}
1837 
1838 		if (result == true) {
1839 			// Validate settings from bios integrated info table
1840 			if (settings->slv_addr == 0)
1841 				return false;
1842 			if (settings->reg_num > 9)
1843 				return false;
1844 			if (settings->reg_num_6g > 3)
1845 				return false;
1846 
1847 			for (i = 0; i < settings->reg_num; i++) {
1848 				if (settings->reg_settings[i].i2c_reg_index > 0x20)
1849 					return false;
1850 			}
1851 
1852 			for (i = 0; i < settings->reg_num_6g; i++) {
1853 				if (settings->reg_settings_6g[i].i2c_reg_index > 0x20)
1854 					return false;
1855 			}
1856 		}
1857 	}
1858 
1859 	return result;
1860 }
1861 
i2c_write(struct pipe_ctx * pipe_ctx,uint8_t address,uint8_t * buffer,uint32_t length)1862 static bool i2c_write(struct pipe_ctx *pipe_ctx,
1863 		uint8_t address, uint8_t *buffer, uint32_t length)
1864 {
1865 	struct i2c_command cmd = {0};
1866 	struct i2c_payload payload = {0};
1867 
1868 	memset(&payload, 0, sizeof(payload));
1869 	memset(&cmd, 0, sizeof(cmd));
1870 
1871 	cmd.number_of_payloads = 1;
1872 	cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
1873 	cmd.speed = pipe_ctx->stream->ctx->dc->caps.i2c_speed_in_khz;
1874 
1875 	payload.address = address;
1876 	payload.data = buffer;
1877 	payload.length = length;
1878 	payload.write = true;
1879 	cmd.payloads = &payload;
1880 
1881 	if (dm_helpers_submit_i2c(pipe_ctx->stream->ctx,
1882 			pipe_ctx->stream->link, &cmd))
1883 		return true;
1884 
1885 	return false;
1886 }
1887 
write_i2c_retimer_setting(struct pipe_ctx * pipe_ctx,bool is_vga_mode,bool is_over_340mhz,struct ext_hdmi_settings * settings)1888 static void write_i2c_retimer_setting(
1889 		struct pipe_ctx *pipe_ctx,
1890 		bool is_vga_mode,
1891 		bool is_over_340mhz,
1892 		struct ext_hdmi_settings *settings)
1893 {
1894 	uint8_t slave_address = (settings->slv_addr >> 1);
1895 	uint8_t buffer[2];
1896 	const uint8_t apply_rx_tx_change = 0x4;
1897 	uint8_t offset = 0xA;
1898 	uint8_t value = 0;
1899 	int i = 0;
1900 	bool i2c_success = false;
1901 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1902 
1903 	memset(&buffer, 0, sizeof(buffer));
1904 
1905 	/* Start Ext-Hdmi programming*/
1906 
1907 	for (i = 0; i < settings->reg_num; i++) {
1908 		/* Apply 3G settings */
1909 		if (settings->reg_settings[i].i2c_reg_index <= 0x20) {
1910 
1911 			buffer[0] = settings->reg_settings[i].i2c_reg_index;
1912 			buffer[1] = settings->reg_settings[i].i2c_reg_val;
1913 			i2c_success = i2c_write(pipe_ctx, slave_address,
1914 						buffer, sizeof(buffer));
1915 			RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1916 				offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
1917 				slave_address, buffer[0], buffer[1], i2c_success?1:0);
1918 
1919 			if (!i2c_success)
1920 				goto i2c_write_fail;
1921 
1922 			/* Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
1923 			 * needs to be set to 1 on every 0xA-0xC write.
1924 			 */
1925 			if (settings->reg_settings[i].i2c_reg_index == 0xA ||
1926 				settings->reg_settings[i].i2c_reg_index == 0xB ||
1927 				settings->reg_settings[i].i2c_reg_index == 0xC) {
1928 
1929 				/* Query current value from offset 0xA */
1930 				if (settings->reg_settings[i].i2c_reg_index == 0xA)
1931 					value = settings->reg_settings[i].i2c_reg_val;
1932 				else {
1933 					i2c_success =
1934 						dal_ddc_service_query_ddc_data(
1935 						pipe_ctx->stream->link->ddc,
1936 						slave_address, &offset, 1, &value, 1);
1937 					if (!i2c_success)
1938 						goto i2c_write_fail;
1939 				}
1940 
1941 				buffer[0] = offset;
1942 				/* Set APPLY_RX_TX_CHANGE bit to 1 */
1943 				buffer[1] = value | apply_rx_tx_change;
1944 				i2c_success = i2c_write(pipe_ctx, slave_address,
1945 						buffer, sizeof(buffer));
1946 				RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1947 					offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1948 					slave_address, buffer[0], buffer[1], i2c_success?1:0);
1949 				if (!i2c_success)
1950 					goto i2c_write_fail;
1951 			}
1952 		}
1953 	}
1954 
1955 	/* Apply 3G settings */
1956 	if (is_over_340mhz) {
1957 		for (i = 0; i < settings->reg_num_6g; i++) {
1958 			/* Apply 3G settings */
1959 			if (settings->reg_settings[i].i2c_reg_index <= 0x20) {
1960 
1961 				buffer[0] = settings->reg_settings_6g[i].i2c_reg_index;
1962 				buffer[1] = settings->reg_settings_6g[i].i2c_reg_val;
1963 				i2c_success = i2c_write(pipe_ctx, slave_address,
1964 							buffer, sizeof(buffer));
1965 				RETIMER_REDRIVER_INFO("above 340Mhz: retimer write to slave_address = 0x%x,\
1966 					offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1967 					slave_address, buffer[0], buffer[1], i2c_success?1:0);
1968 
1969 				if (!i2c_success)
1970 					goto i2c_write_fail;
1971 
1972 				/* Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
1973 				 * needs to be set to 1 on every 0xA-0xC write.
1974 				 */
1975 				if (settings->reg_settings_6g[i].i2c_reg_index == 0xA ||
1976 					settings->reg_settings_6g[i].i2c_reg_index == 0xB ||
1977 					settings->reg_settings_6g[i].i2c_reg_index == 0xC) {
1978 
1979 					/* Query current value from offset 0xA */
1980 					if (settings->reg_settings_6g[i].i2c_reg_index == 0xA)
1981 						value = settings->reg_settings_6g[i].i2c_reg_val;
1982 					else {
1983 						i2c_success =
1984 								dal_ddc_service_query_ddc_data(
1985 								pipe_ctx->stream->link->ddc,
1986 								slave_address, &offset, 1, &value, 1);
1987 						if (!i2c_success)
1988 							goto i2c_write_fail;
1989 					}
1990 
1991 					buffer[0] = offset;
1992 					/* Set APPLY_RX_TX_CHANGE bit to 1 */
1993 					buffer[1] = value | apply_rx_tx_change;
1994 					i2c_success = i2c_write(pipe_ctx, slave_address,
1995 							buffer, sizeof(buffer));
1996 					RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1997 						offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1998 						slave_address, buffer[0], buffer[1], i2c_success?1:0);
1999 					if (!i2c_success)
2000 						goto i2c_write_fail;
2001 				}
2002 			}
2003 		}
2004 	}
2005 
2006 	if (is_vga_mode) {
2007 		/* Program additional settings if using 640x480 resolution */
2008 
2009 		/* Write offset 0xFF to 0x01 */
2010 		buffer[0] = 0xff;
2011 		buffer[1] = 0x01;
2012 		i2c_success = i2c_write(pipe_ctx, slave_address,
2013 				buffer, sizeof(buffer));
2014 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2015 				offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2016 				slave_address, buffer[0], buffer[1], i2c_success?1:0);
2017 		if (!i2c_success)
2018 			goto i2c_write_fail;
2019 
2020 		/* Write offset 0x00 to 0x23 */
2021 		buffer[0] = 0x00;
2022 		buffer[1] = 0x23;
2023 		i2c_success = i2c_write(pipe_ctx, slave_address,
2024 				buffer, sizeof(buffer));
2025 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2026 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2027 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2028 		if (!i2c_success)
2029 			goto i2c_write_fail;
2030 
2031 		/* Write offset 0xff to 0x00 */
2032 		buffer[0] = 0xff;
2033 		buffer[1] = 0x00;
2034 		i2c_success = i2c_write(pipe_ctx, slave_address,
2035 				buffer, sizeof(buffer));
2036 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2037 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2038 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2039 		if (!i2c_success)
2040 			goto i2c_write_fail;
2041 
2042 	}
2043 
2044 	return;
2045 
2046 i2c_write_fail:
2047 	DC_LOG_DEBUG("Set retimer failed");
2048 }
2049 
write_i2c_default_retimer_setting(struct pipe_ctx * pipe_ctx,bool is_vga_mode,bool is_over_340mhz)2050 static void write_i2c_default_retimer_setting(
2051 		struct pipe_ctx *pipe_ctx,
2052 		bool is_vga_mode,
2053 		bool is_over_340mhz)
2054 {
2055 	uint8_t slave_address = (0xBA >> 1);
2056 	uint8_t buffer[2];
2057 	bool i2c_success = false;
2058 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2059 
2060 	memset(&buffer, 0, sizeof(buffer));
2061 
2062 	/* Program Slave Address for tuning single integrity */
2063 	/* Write offset 0x0A to 0x13 */
2064 	buffer[0] = 0x0A;
2065 	buffer[1] = 0x13;
2066 	i2c_success = i2c_write(pipe_ctx, slave_address,
2067 			buffer, sizeof(buffer));
2068 	RETIMER_REDRIVER_INFO("retimer writes default setting to slave_address = 0x%x,\
2069 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2070 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2071 	if (!i2c_success)
2072 		goto i2c_write_fail;
2073 
2074 	/* Write offset 0x0A to 0x17 */
2075 	buffer[0] = 0x0A;
2076 	buffer[1] = 0x17;
2077 	i2c_success = i2c_write(pipe_ctx, slave_address,
2078 			buffer, sizeof(buffer));
2079 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2080 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2081 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2082 	if (!i2c_success)
2083 		goto i2c_write_fail;
2084 
2085 	/* Write offset 0x0B to 0xDA or 0xD8 */
2086 	buffer[0] = 0x0B;
2087 	buffer[1] = is_over_340mhz ? 0xDA : 0xD8;
2088 	i2c_success = i2c_write(pipe_ctx, slave_address,
2089 			buffer, sizeof(buffer));
2090 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2091 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2092 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2093 	if (!i2c_success)
2094 		goto i2c_write_fail;
2095 
2096 	/* Write offset 0x0A to 0x17 */
2097 	buffer[0] = 0x0A;
2098 	buffer[1] = 0x17;
2099 	i2c_success = i2c_write(pipe_ctx, slave_address,
2100 			buffer, sizeof(buffer));
2101 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2102 		offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
2103 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2104 	if (!i2c_success)
2105 		goto i2c_write_fail;
2106 
2107 	/* Write offset 0x0C to 0x1D or 0x91 */
2108 	buffer[0] = 0x0C;
2109 	buffer[1] = is_over_340mhz ? 0x1D : 0x91;
2110 	i2c_success = i2c_write(pipe_ctx, slave_address,
2111 			buffer, sizeof(buffer));
2112 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2113 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2114 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2115 	if (!i2c_success)
2116 		goto i2c_write_fail;
2117 
2118 	/* Write offset 0x0A to 0x17 */
2119 	buffer[0] = 0x0A;
2120 	buffer[1] = 0x17;
2121 	i2c_success = i2c_write(pipe_ctx, slave_address,
2122 			buffer, sizeof(buffer));
2123 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2124 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2125 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2126 	if (!i2c_success)
2127 		goto i2c_write_fail;
2128 
2129 
2130 	if (is_vga_mode) {
2131 		/* Program additional settings if using 640x480 resolution */
2132 
2133 		/* Write offset 0xFF to 0x01 */
2134 		buffer[0] = 0xff;
2135 		buffer[1] = 0x01;
2136 		i2c_success = i2c_write(pipe_ctx, slave_address,
2137 				buffer, sizeof(buffer));
2138 		RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2139 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2140 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2141 		if (!i2c_success)
2142 			goto i2c_write_fail;
2143 
2144 		/* Write offset 0x00 to 0x23 */
2145 		buffer[0] = 0x00;
2146 		buffer[1] = 0x23;
2147 		i2c_success = i2c_write(pipe_ctx, slave_address,
2148 				buffer, sizeof(buffer));
2149 		RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2150 			offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
2151 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2152 		if (!i2c_success)
2153 			goto i2c_write_fail;
2154 
2155 		/* Write offset 0xff to 0x00 */
2156 		buffer[0] = 0xff;
2157 		buffer[1] = 0x00;
2158 		i2c_success = i2c_write(pipe_ctx, slave_address,
2159 				buffer, sizeof(buffer));
2160 		RETIMER_REDRIVER_INFO("retimer write default setting to slave_addr = 0x%x,\
2161 			offset = 0x%x, reg_val= 0x%x, i2c_success = %d end here\n",
2162 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2163 		if (!i2c_success)
2164 			goto i2c_write_fail;
2165 	}
2166 
2167 	return;
2168 
2169 i2c_write_fail:
2170 	DC_LOG_DEBUG("Set default retimer failed");
2171 }
2172 
write_i2c_redriver_setting(struct pipe_ctx * pipe_ctx,bool is_over_340mhz)2173 static void write_i2c_redriver_setting(
2174 		struct pipe_ctx *pipe_ctx,
2175 		bool is_over_340mhz)
2176 {
2177 	uint8_t slave_address = (0xF0 >> 1);
2178 	uint8_t buffer[16];
2179 	bool i2c_success = false;
2180 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2181 
2182 	memset(&buffer, 0, sizeof(buffer));
2183 
2184 	// Program Slave Address for tuning single integrity
2185 	buffer[3] = 0x4E;
2186 	buffer[4] = 0x4E;
2187 	buffer[5] = 0x4E;
2188 	buffer[6] = is_over_340mhz ? 0x4E : 0x4A;
2189 
2190 	i2c_success = i2c_write(pipe_ctx, slave_address,
2191 					buffer, sizeof(buffer));
2192 	RETIMER_REDRIVER_INFO("redriver write 0 to all 16 reg offset expect following:\n\
2193 		\t slave_addr = 0x%x, offset[3] = 0x%x, offset[4] = 0x%x,\
2194 		offset[5] = 0x%x,offset[6] is_over_340mhz = 0x%x,\
2195 		i2c_success = %d\n",
2196 		slave_address, buffer[3], buffer[4], buffer[5], buffer[6], i2c_success?1:0);
2197 
2198 	if (!i2c_success)
2199 		DC_LOG_DEBUG("Set redriver failed");
2200 }
2201 
disable_link(struct dc_link * link,enum signal_type signal)2202 static void disable_link(struct dc_link *link, enum signal_type signal)
2203 {
2204 	/*
2205 	 * TODO: implement call for dp_set_hw_test_pattern
2206 	 * it is needed for compliance testing
2207 	 */
2208 
2209 	/* Here we need to specify that encoder output settings
2210 	 * need to be calculated as for the set mode,
2211 	 * it will lead to querying dynamic link capabilities
2212 	 * which should be done before enable output
2213 	 */
2214 
2215 	if (dc_is_dp_signal(signal)) {
2216 		/* SST DP, eDP */
2217 		if (dc_is_dp_sst_signal(signal))
2218 			dp_disable_link_phy(link, signal);
2219 		else
2220 			dp_disable_link_phy_mst(link, signal);
2221 
2222 		if (dc_is_dp_sst_signal(signal) ||
2223 				link->mst_stream_alloc_table.stream_count == 0) {
2224 			dp_set_fec_enable(link, false);
2225 			dp_set_fec_ready(link, false);
2226 		}
2227 	} else {
2228 		if (signal != SIGNAL_TYPE_VIRTUAL)
2229 			link->link_enc->funcs->disable_output(link->link_enc, signal);
2230 	}
2231 
2232 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2233 		/* MST disable link only when no stream use the link */
2234 		if (link->mst_stream_alloc_table.stream_count <= 0)
2235 			link->link_status.link_active = false;
2236 	} else {
2237 		link->link_status.link_active = false;
2238 	}
2239 }
2240 
enable_link_hdmi(struct pipe_ctx * pipe_ctx)2241 static void enable_link_hdmi(struct pipe_ctx *pipe_ctx)
2242 {
2243 	struct dc_stream_state *stream = pipe_ctx->stream;
2244 	struct dc_link *link = stream->link;
2245 	enum dc_color_depth display_color_depth;
2246 	enum engine_id eng_id;
2247 	struct ext_hdmi_settings settings = {0};
2248 	bool is_over_340mhz = false;
2249 	bool is_vga_mode = (stream->timing.h_addressable == 640)
2250 			&& (stream->timing.v_addressable == 480);
2251 
2252 	if (stream->phy_pix_clk == 0)
2253 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2254 	if (stream->phy_pix_clk > 340000)
2255 		is_over_340mhz = true;
2256 
2257 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
2258 		unsigned short masked_chip_caps = pipe_ctx->stream->link->chip_caps &
2259 				EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
2260 		if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
2261 			/* DP159, Retimer settings */
2262 			eng_id = pipe_ctx->stream_res.stream_enc->id;
2263 
2264 			if (get_ext_hdmi_settings(pipe_ctx, eng_id, &settings)) {
2265 				write_i2c_retimer_setting(pipe_ctx,
2266 						is_vga_mode, is_over_340mhz, &settings);
2267 			} else {
2268 				write_i2c_default_retimer_setting(pipe_ctx,
2269 						is_vga_mode, is_over_340mhz);
2270 			}
2271 		} else if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
2272 			/* PI3EQX1204, Redriver settings */
2273 			write_i2c_redriver_setting(pipe_ctx, is_over_340mhz);
2274 		}
2275 	}
2276 
2277 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
2278 		dal_ddc_service_write_scdc_data(
2279 			stream->link->ddc,
2280 			stream->phy_pix_clk,
2281 			stream->timing.flags.LTE_340MCSC_SCRAMBLE);
2282 
2283 	memset(&stream->link->cur_link_settings, 0,
2284 			sizeof(struct dc_link_settings));
2285 
2286 	display_color_depth = stream->timing.display_color_depth;
2287 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
2288 		display_color_depth = COLOR_DEPTH_888;
2289 
2290 	link->link_enc->funcs->enable_tmds_output(
2291 			link->link_enc,
2292 			pipe_ctx->clock_source->id,
2293 			display_color_depth,
2294 			pipe_ctx->stream->signal,
2295 			stream->phy_pix_clk);
2296 
2297 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
2298 		dal_ddc_service_read_scdc_data(link->ddc);
2299 }
2300 
enable_link_lvds(struct pipe_ctx * pipe_ctx)2301 static void enable_link_lvds(struct pipe_ctx *pipe_ctx)
2302 {
2303 	struct dc_stream_state *stream = pipe_ctx->stream;
2304 	struct dc_link *link = stream->link;
2305 
2306 	if (stream->phy_pix_clk == 0)
2307 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2308 
2309 	memset(&stream->link->cur_link_settings, 0,
2310 			sizeof(struct dc_link_settings));
2311 
2312 	link->link_enc->funcs->enable_lvds_output(
2313 			link->link_enc,
2314 			pipe_ctx->clock_source->id,
2315 			stream->phy_pix_clk);
2316 
2317 }
2318 
2319 /****************************enable_link***********************************/
enable_link(struct dc_state * state,struct pipe_ctx * pipe_ctx)2320 static enum dc_status enable_link(
2321 		struct dc_state *state,
2322 		struct pipe_ctx *pipe_ctx)
2323 {
2324 	enum dc_status status = DC_ERROR_UNEXPECTED;
2325 	struct dc_stream_state *stream = pipe_ctx->stream;
2326 	struct dc_link *link = stream->link;
2327 
2328 	/* There's some scenarios where driver is unloaded with display
2329 	 * still enabled. When driver is reloaded, it may cause a display
2330 	 * to not light up if there is a mismatch between old and new
2331 	 * link settings. Need to call disable first before enabling at
2332 	 * new link settings.
2333 	 */
2334 	if (link->link_status.link_active) {
2335 		disable_link(link, pipe_ctx->stream->signal);
2336 	}
2337 
2338 	switch (pipe_ctx->stream->signal) {
2339 	case SIGNAL_TYPE_DISPLAY_PORT:
2340 		status = enable_link_dp(state, pipe_ctx);
2341 		break;
2342 	case SIGNAL_TYPE_EDP:
2343 		status = enable_link_edp(state, pipe_ctx);
2344 		break;
2345 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
2346 		status = enable_link_dp_mst(state, pipe_ctx);
2347 		msleep(200);
2348 		break;
2349 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
2350 	case SIGNAL_TYPE_DVI_DUAL_LINK:
2351 	case SIGNAL_TYPE_HDMI_TYPE_A:
2352 		enable_link_hdmi(pipe_ctx);
2353 		status = DC_OK;
2354 		break;
2355 	case SIGNAL_TYPE_LVDS:
2356 		enable_link_lvds(pipe_ctx);
2357 		status = DC_OK;
2358 		break;
2359 	case SIGNAL_TYPE_VIRTUAL:
2360 		status = DC_OK;
2361 		break;
2362 	default:
2363 		break;
2364 	}
2365 
2366 	if (status == DC_OK)
2367 		pipe_ctx->stream->link->link_status.link_active = true;
2368 
2369 	return status;
2370 }
2371 
get_timing_pixel_clock_100hz(const struct dc_crtc_timing * timing)2372 static uint32_t get_timing_pixel_clock_100hz(const struct dc_crtc_timing *timing)
2373 {
2374 
2375 	uint32_t pxl_clk = timing->pix_clk_100hz;
2376 
2377 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2378 		pxl_clk /= 2;
2379 	else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
2380 		pxl_clk = pxl_clk * 2 / 3;
2381 
2382 	if (timing->display_color_depth == COLOR_DEPTH_101010)
2383 		pxl_clk = pxl_clk * 10 / 8;
2384 	else if (timing->display_color_depth == COLOR_DEPTH_121212)
2385 		pxl_clk = pxl_clk * 12 / 8;
2386 
2387 	return pxl_clk;
2388 }
2389 
dp_active_dongle_validate_timing(const struct dc_crtc_timing * timing,const struct dpcd_caps * dpcd_caps)2390 static bool dp_active_dongle_validate_timing(
2391 		const struct dc_crtc_timing *timing,
2392 		const struct dpcd_caps *dpcd_caps)
2393 {
2394 	const struct dc_dongle_caps *dongle_caps = &dpcd_caps->dongle_caps;
2395 
2396 	switch (dpcd_caps->dongle_type) {
2397 	case DISPLAY_DONGLE_DP_VGA_CONVERTER:
2398 	case DISPLAY_DONGLE_DP_DVI_CONVERTER:
2399 	case DISPLAY_DONGLE_DP_DVI_DONGLE:
2400 		if (timing->pixel_encoding == PIXEL_ENCODING_RGB)
2401 			return true;
2402 		else
2403 			return false;
2404 	default:
2405 		break;
2406 	}
2407 
2408 	if (dpcd_caps->dongle_type != DISPLAY_DONGLE_DP_HDMI_CONVERTER ||
2409 		dongle_caps->extendedCapValid == false)
2410 		return true;
2411 
2412 	/* Check Pixel Encoding */
2413 	switch (timing->pixel_encoding) {
2414 	case PIXEL_ENCODING_RGB:
2415 	case PIXEL_ENCODING_YCBCR444:
2416 		break;
2417 	case PIXEL_ENCODING_YCBCR422:
2418 		if (!dongle_caps->is_dp_hdmi_ycbcr422_pass_through)
2419 			return false;
2420 		break;
2421 	case PIXEL_ENCODING_YCBCR420:
2422 		if (!dongle_caps->is_dp_hdmi_ycbcr420_pass_through)
2423 			return false;
2424 		break;
2425 	default:
2426 		/* Invalid Pixel Encoding*/
2427 		return false;
2428 	}
2429 
2430 	switch (timing->display_color_depth) {
2431 	case COLOR_DEPTH_666:
2432 	case COLOR_DEPTH_888:
2433 		/*888 and 666 should always be supported*/
2434 		break;
2435 	case COLOR_DEPTH_101010:
2436 		if (dongle_caps->dp_hdmi_max_bpc < 10)
2437 			return false;
2438 		break;
2439 	case COLOR_DEPTH_121212:
2440 		if (dongle_caps->dp_hdmi_max_bpc < 12)
2441 			return false;
2442 		break;
2443 	case COLOR_DEPTH_141414:
2444 	case COLOR_DEPTH_161616:
2445 	default:
2446 		/* These color depths are currently not supported */
2447 		return false;
2448 	}
2449 
2450 	if (get_timing_pixel_clock_100hz(timing) > (dongle_caps->dp_hdmi_max_pixel_clk_in_khz * 10))
2451 		return false;
2452 
2453 	return true;
2454 }
2455 
dc_link_validate_mode_timing(const struct dc_stream_state * stream,struct dc_link * link,const struct dc_crtc_timing * timing)2456 enum dc_status dc_link_validate_mode_timing(
2457 		const struct dc_stream_state *stream,
2458 		struct dc_link *link,
2459 		const struct dc_crtc_timing *timing)
2460 {
2461 	uint32_t max_pix_clk = stream->link->dongle_max_pix_clk * 10;
2462 	struct dpcd_caps *dpcd_caps = &link->dpcd_caps;
2463 
2464 	/* A hack to avoid failing any modes for EDID override feature on
2465 	 * topology change such as lower quality cable for DP or different dongle
2466 	 */
2467 	if (link->remote_sinks[0] && link->remote_sinks[0]->sink_signal == SIGNAL_TYPE_VIRTUAL)
2468 		return DC_OK;
2469 
2470 	/* Passive Dongle */
2471 	if (max_pix_clk != 0 && get_timing_pixel_clock_100hz(timing) > max_pix_clk)
2472 		return DC_EXCEED_DONGLE_CAP;
2473 
2474 	/* Active Dongle*/
2475 	if (!dp_active_dongle_validate_timing(timing, dpcd_caps))
2476 		return DC_EXCEED_DONGLE_CAP;
2477 
2478 	switch (stream->signal) {
2479 	case SIGNAL_TYPE_EDP:
2480 	case SIGNAL_TYPE_DISPLAY_PORT:
2481 		if (!dp_validate_mode_timing(
2482 				link,
2483 				timing))
2484 			return DC_NO_DP_LINK_BANDWIDTH;
2485 		break;
2486 
2487 	default:
2488 		break;
2489 	}
2490 
2491 	return DC_OK;
2492 }
2493 
get_abm_from_stream_res(const struct dc_link * link)2494 static struct abm *get_abm_from_stream_res(const struct dc_link *link)
2495 {
2496 	int i;
2497 	struct dc *dc = NULL;
2498 	struct abm *abm = NULL;
2499 
2500 	if (!link || !link->ctx)
2501 		return NULL;
2502 
2503 	dc = link->ctx->dc;
2504 
2505 	for (i = 0; i < MAX_PIPES; i++) {
2506 		struct pipe_ctx pipe_ctx = dc->current_state->res_ctx.pipe_ctx[i];
2507 		struct dc_stream_state *stream = pipe_ctx.stream;
2508 
2509 		if (stream && stream->link == link) {
2510 			abm = pipe_ctx.stream_res.abm;
2511 			break;
2512 		}
2513 	}
2514 	return abm;
2515 }
2516 
dc_link_get_backlight_level(const struct dc_link * link)2517 int dc_link_get_backlight_level(const struct dc_link *link)
2518 {
2519 
2520 	struct abm *abm = get_abm_from_stream_res(link);
2521 
2522 	if (abm == NULL || abm->funcs->get_current_backlight == NULL)
2523 		return DC_ERROR_UNEXPECTED;
2524 
2525 	return (int) abm->funcs->get_current_backlight(abm);
2526 }
2527 
dc_link_get_target_backlight_pwm(const struct dc_link * link)2528 int dc_link_get_target_backlight_pwm(const struct dc_link *link)
2529 {
2530 	struct abm *abm = get_abm_from_stream_res(link);
2531 
2532 	if (abm == NULL || abm->funcs->get_target_backlight == NULL)
2533 		return DC_ERROR_UNEXPECTED;
2534 
2535 	return (int) abm->funcs->get_target_backlight(abm);
2536 }
2537 
get_pipe_from_link(const struct dc_link * link)2538 static struct pipe_ctx *get_pipe_from_link(const struct dc_link *link)
2539 {
2540 	int i;
2541 	struct dc *dc = link->ctx->dc;
2542 	struct pipe_ctx *pipe_ctx = NULL;
2543 
2544 	for (i = 0; i < MAX_PIPES; i++) {
2545 		if (dc->current_state->res_ctx.pipe_ctx[i].stream) {
2546 			if (dc->current_state->res_ctx.pipe_ctx[i].stream->link == link) {
2547 				pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
2548 				break;
2549 			}
2550 		}
2551 	}
2552 
2553 	return pipe_ctx;
2554 }
2555 
dc_link_set_backlight_level(const struct dc_link * link,uint32_t backlight_pwm_u16_16,uint32_t frame_ramp)2556 bool dc_link_set_backlight_level(const struct dc_link *link,
2557 		uint32_t backlight_pwm_u16_16,
2558 		uint32_t frame_ramp)
2559 {
2560 	struct dc  *dc = link->ctx->dc;
2561 
2562 	DC_LOGGER_INIT(link->ctx->logger);
2563 	DC_LOG_BACKLIGHT("New Backlight level: %d (0x%X)\n",
2564 			backlight_pwm_u16_16, backlight_pwm_u16_16);
2565 
2566 	if (dc_is_embedded_signal(link->connector_signal)) {
2567 		struct pipe_ctx *pipe_ctx = get_pipe_from_link(link);
2568 
2569 		if (pipe_ctx) {
2570 			/* Disable brightness ramping when the display is blanked
2571 			 * as it can hang the DMCU
2572 			 */
2573 			if (pipe_ctx->plane_state == NULL)
2574 				frame_ramp = 0;
2575 		} else {
2576 			return false;
2577 		}
2578 
2579 		dc->hwss.set_backlight_level(
2580 				pipe_ctx,
2581 				backlight_pwm_u16_16,
2582 				frame_ramp);
2583 	}
2584 	return true;
2585 }
2586 
dc_link_set_psr_allow_active(struct dc_link * link,bool allow_active,bool wait)2587 bool dc_link_set_psr_allow_active(struct dc_link *link, bool allow_active, bool wait)
2588 {
2589 	struct dc  *dc = link->ctx->dc;
2590 	struct dmcu *dmcu = dc->res_pool->dmcu;
2591 	struct dmub_psr *psr = dc->res_pool->psr;
2592 
2593 	link->psr_settings.psr_allow_active = allow_active;
2594 
2595 	if (psr != NULL && link->psr_settings.psr_feature_enabled)
2596 		psr->funcs->psr_enable(psr, allow_active, wait);
2597 	else if ((dmcu != NULL && dmcu->funcs->is_dmcu_initialized(dmcu)) && link->psr_settings.psr_feature_enabled)
2598 		dmcu->funcs->set_psr_enable(dmcu, allow_active, wait);
2599 	else
2600 		return false;
2601 
2602 	return true;
2603 }
2604 
dc_link_get_psr_state(const struct dc_link * link,uint32_t * psr_state)2605 bool dc_link_get_psr_state(const struct dc_link *link, uint32_t *psr_state)
2606 {
2607 	struct dc  *dc = link->ctx->dc;
2608 	struct dmcu *dmcu = dc->res_pool->dmcu;
2609 	struct dmub_psr *psr = dc->res_pool->psr;
2610 
2611 	if (psr != NULL && link->psr_settings.psr_feature_enabled)
2612 		psr->funcs->psr_get_state(psr, psr_state);
2613 	else if (dmcu != NULL && link->psr_settings.psr_feature_enabled)
2614 		dmcu->funcs->get_psr_state(dmcu, psr_state);
2615 
2616 	return true;
2617 }
2618 
2619 static inline enum physical_phy_id
transmitter_to_phy_id(enum transmitter transmitter_value)2620 transmitter_to_phy_id(enum transmitter transmitter_value)
2621 {
2622 	switch (transmitter_value) {
2623 	case TRANSMITTER_UNIPHY_A:
2624 		return PHYLD_0;
2625 	case TRANSMITTER_UNIPHY_B:
2626 		return PHYLD_1;
2627 	case TRANSMITTER_UNIPHY_C:
2628 		return PHYLD_2;
2629 	case TRANSMITTER_UNIPHY_D:
2630 		return PHYLD_3;
2631 	case TRANSMITTER_UNIPHY_E:
2632 		return PHYLD_4;
2633 	case TRANSMITTER_UNIPHY_F:
2634 		return PHYLD_5;
2635 	case TRANSMITTER_NUTMEG_CRT:
2636 		return PHYLD_6;
2637 	case TRANSMITTER_TRAVIS_CRT:
2638 		return PHYLD_7;
2639 	case TRANSMITTER_TRAVIS_LCD:
2640 		return PHYLD_8;
2641 	case TRANSMITTER_UNIPHY_G:
2642 		return PHYLD_9;
2643 	case TRANSMITTER_COUNT:
2644 		return PHYLD_COUNT;
2645 	case TRANSMITTER_UNKNOWN:
2646 		return PHYLD_UNKNOWN;
2647 	default:
2648 		WARN_ONCE(1, "Unknown transmitter value %d\n",
2649 			  transmitter_value);
2650 		return PHYLD_UNKNOWN;
2651 	}
2652 }
2653 
dc_link_setup_psr(struct dc_link * link,const struct dc_stream_state * stream,struct psr_config * psr_config,struct psr_context * psr_context)2654 bool dc_link_setup_psr(struct dc_link *link,
2655 		const struct dc_stream_state *stream, struct psr_config *psr_config,
2656 		struct psr_context *psr_context)
2657 {
2658 	struct dc *dc;
2659 	struct dmcu *dmcu;
2660 	struct dmub_psr *psr;
2661 	int i;
2662 	/* updateSinkPsrDpcdConfig*/
2663 	union dpcd_psr_configuration psr_configuration;
2664 
2665 	psr_context->controllerId = CONTROLLER_ID_UNDEFINED;
2666 
2667 	if (!link)
2668 		return false;
2669 
2670 	dc = link->ctx->dc;
2671 	dmcu = dc->res_pool->dmcu;
2672 	psr = dc->res_pool->psr;
2673 
2674 	if (!dmcu && !psr)
2675 		return false;
2676 
2677 
2678 	memset(&psr_configuration, 0, sizeof(psr_configuration));
2679 
2680 	psr_configuration.bits.ENABLE                    = 1;
2681 	psr_configuration.bits.CRC_VERIFICATION          = 1;
2682 	psr_configuration.bits.FRAME_CAPTURE_INDICATION  =
2683 			psr_config->psr_frame_capture_indication_req;
2684 
2685 	/* Check for PSR v2*/
2686 	if (psr_config->psr_version == 0x2) {
2687 		/* For PSR v2 selective update.
2688 		 * Indicates whether sink should start capturing
2689 		 * immediately following active scan line,
2690 		 * or starting with the 2nd active scan line.
2691 		 */
2692 		psr_configuration.bits.LINE_CAPTURE_INDICATION = 0;
2693 		/*For PSR v2, determines whether Sink should generate
2694 		 * IRQ_HPD when CRC mismatch is detected.
2695 		 */
2696 		psr_configuration.bits.IRQ_HPD_WITH_CRC_ERROR    = 1;
2697 	}
2698 
2699 	dm_helpers_dp_write_dpcd(
2700 		link->ctx,
2701 		link,
2702 		368,
2703 		&psr_configuration.raw,
2704 		sizeof(psr_configuration.raw));
2705 
2706 	psr_context->channel = link->ddc->ddc_pin->hw_info.ddc_channel;
2707 	psr_context->transmitterId = link->link_enc->transmitter;
2708 	psr_context->engineId = link->link_enc->preferred_engine;
2709 
2710 	for (i = 0; i < MAX_PIPES; i++) {
2711 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
2712 				== stream) {
2713 			/* dmcu -1 for all controller id values,
2714 			 * therefore +1 here
2715 			 */
2716 			psr_context->controllerId =
2717 				dc->current_state->res_ctx.
2718 				pipe_ctx[i].stream_res.tg->inst + 1;
2719 			break;
2720 		}
2721 	}
2722 
2723 	/* Hardcoded for now.  Can be Pcie or Uniphy (or Unknown)*/
2724 	psr_context->phyType = PHY_TYPE_UNIPHY;
2725 	/*PhyId is associated with the transmitter id*/
2726 	psr_context->smuPhyId =
2727 		transmitter_to_phy_id(link->link_enc->transmitter);
2728 
2729 	psr_context->crtcTimingVerticalTotal = stream->timing.v_total;
2730 	psr_context->vsync_rate_hz = div64_u64(div64_u64((stream->
2731 					timing.pix_clk_100hz * 100),
2732 					stream->timing.v_total),
2733 					stream->timing.h_total);
2734 
2735 	psr_context->psrSupportedDisplayConfig = true;
2736 	psr_context->psrExitLinkTrainingRequired =
2737 		psr_config->psr_exit_link_training_required;
2738 	psr_context->sdpTransmitLineNumDeadline =
2739 		psr_config->psr_sdp_transmit_line_num_deadline;
2740 	psr_context->psrFrameCaptureIndicationReq =
2741 		psr_config->psr_frame_capture_indication_req;
2742 
2743 	psr_context->skipPsrWaitForPllLock = 0; /* only = 1 in KV */
2744 
2745 	psr_context->numberOfControllers =
2746 			link->dc->res_pool->timing_generator_count;
2747 
2748 	psr_context->rfb_update_auto_en = true;
2749 
2750 	/* 2 frames before enter PSR. */
2751 	psr_context->timehyst_frames = 2;
2752 	/* half a frame
2753 	 * (units in 100 lines, i.e. a value of 1 represents 100 lines)
2754 	 */
2755 	psr_context->hyst_lines = stream->timing.v_total / 2 / 100;
2756 	psr_context->aux_repeats = 10;
2757 
2758 	psr_context->psr_level.u32all = 0;
2759 
2760 #if defined(CONFIG_DRM_AMD_DC_DCN)
2761 	/*skip power down the single pipe since it blocks the cstate*/
2762 	if (ASICREV_IS_RAVEN(link->ctx->asic_id.hw_internal_rev))
2763 		psr_context->psr_level.bits.SKIP_CRTC_DISABLE = true;
2764 #endif
2765 
2766 	/* SMU will perform additional powerdown sequence.
2767 	 * For unsupported ASICs, set psr_level flag to skip PSR
2768 	 *  static screen notification to SMU.
2769 	 *  (Always set for DAL2, did not check ASIC)
2770 	 */
2771 	psr_context->allow_smu_optimizations = psr_config->allow_smu_optimizations;
2772 
2773 	/* Complete PSR entry before aborting to prevent intermittent
2774 	 * freezes on certain eDPs
2775 	 */
2776 	psr_context->psr_level.bits.DISABLE_PSR_ENTRY_ABORT = 1;
2777 
2778 	/* Controls additional delay after remote frame capture before
2779 	 * continuing power down, default = 0
2780 	 */
2781 	psr_context->frame_delay = 0;
2782 
2783 	if (psr)
2784 		link->psr_settings.psr_feature_enabled = psr->funcs->psr_copy_settings(psr, link, psr_context);
2785 	else
2786 		link->psr_settings.psr_feature_enabled = dmcu->funcs->setup_psr(dmcu, link, psr_context);
2787 
2788 	/* psr_enabled == 0 indicates setup_psr did not succeed, but this
2789 	 * should not happen since firmware should be running at this point
2790 	 */
2791 	if (link->psr_settings.psr_feature_enabled == 0)
2792 		ASSERT(0);
2793 
2794 	return true;
2795 
2796 }
2797 
dc_link_get_status(const struct dc_link * link)2798 const struct dc_link_status *dc_link_get_status(const struct dc_link *link)
2799 {
2800 	return &link->link_status;
2801 }
2802 
core_link_resume(struct dc_link * link)2803 void core_link_resume(struct dc_link *link)
2804 {
2805 	if (link->connector_signal != SIGNAL_TYPE_VIRTUAL)
2806 		program_hpd_filter(link);
2807 }
2808 
get_pbn_per_slot(struct dc_stream_state * stream)2809 static struct fixed31_32 get_pbn_per_slot(struct dc_stream_state *stream)
2810 {
2811 	struct fixed31_32 mbytes_per_sec;
2812 	uint32_t link_rate_in_mbytes_per_sec = dc_link_bandwidth_kbps(stream->link,
2813 			&stream->link->cur_link_settings);
2814 	link_rate_in_mbytes_per_sec /= 8000; /* Kbits to MBytes */
2815 
2816 	mbytes_per_sec = dc_fixpt_from_int(link_rate_in_mbytes_per_sec);
2817 
2818 	return dc_fixpt_div_int(mbytes_per_sec, 54);
2819 }
2820 
get_pbn_from_timing(struct pipe_ctx * pipe_ctx)2821 static struct fixed31_32 get_pbn_from_timing(struct pipe_ctx *pipe_ctx)
2822 {
2823 	uint64_t kbps;
2824 	struct fixed31_32 peak_kbps;
2825 	uint32_t numerator;
2826 	uint32_t denominator;
2827 
2828 	kbps = dc_bandwidth_in_kbps_from_timing(&pipe_ctx->stream->timing);
2829 
2830 	/*
2831 	 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2832 	 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2833 	 * common multiplier to render an integer PBN for all link rate/lane
2834 	 * counts combinations
2835 	 * calculate
2836 	 * peak_kbps *= (1006/1000)
2837 	 * peak_kbps *= (64/54)
2838 	 * peak_kbps *= 8    convert to bytes
2839 	 */
2840 
2841 	numerator = 64 * PEAK_FACTOR_X1000;
2842 	denominator = 54 * 8 * 1000 * 1000;
2843 	kbps *= numerator;
2844 	peak_kbps = dc_fixpt_from_fraction(kbps, denominator);
2845 
2846 	return peak_kbps;
2847 }
2848 
update_mst_stream_alloc_table(struct dc_link * link,struct stream_encoder * stream_enc,const struct dp_mst_stream_allocation_table * proposed_table)2849 static void update_mst_stream_alloc_table(
2850 	struct dc_link *link,
2851 	struct stream_encoder *stream_enc,
2852 	const struct dp_mst_stream_allocation_table *proposed_table)
2853 {
2854 	struct link_mst_stream_allocation work_table[MAX_CONTROLLER_NUM] = {
2855 			{ 0 } };
2856 	struct link_mst_stream_allocation *dc_alloc;
2857 
2858 	int i;
2859 	int j;
2860 
2861 	/* if DRM proposed_table has more than one new payload */
2862 	ASSERT(proposed_table->stream_count -
2863 			link->mst_stream_alloc_table.stream_count < 2);
2864 
2865 	/* copy proposed_table to link, add stream encoder */
2866 	for (i = 0; i < proposed_table->stream_count; i++) {
2867 
2868 		for (j = 0; j < link->mst_stream_alloc_table.stream_count; j++) {
2869 			dc_alloc =
2870 			&link->mst_stream_alloc_table.stream_allocations[j];
2871 
2872 			if (dc_alloc->vcp_id ==
2873 				proposed_table->stream_allocations[i].vcp_id) {
2874 
2875 				work_table[i] = *dc_alloc;
2876 				break; /* exit j loop */
2877 			}
2878 		}
2879 
2880 		/* new vcp_id */
2881 		if (j == link->mst_stream_alloc_table.stream_count) {
2882 			work_table[i].vcp_id =
2883 				proposed_table->stream_allocations[i].vcp_id;
2884 			work_table[i].slot_count =
2885 				proposed_table->stream_allocations[i].slot_count;
2886 			work_table[i].stream_enc = stream_enc;
2887 		}
2888 	}
2889 
2890 	/* update link->mst_stream_alloc_table with work_table */
2891 	link->mst_stream_alloc_table.stream_count =
2892 			proposed_table->stream_count;
2893 	for (i = 0; i < MAX_CONTROLLER_NUM; i++)
2894 		link->mst_stream_alloc_table.stream_allocations[i] =
2895 				work_table[i];
2896 }
2897 
2898 /* convert link_mst_stream_alloc_table to dm dp_mst_stream_alloc_table
2899  * because stream_encoder is not exposed to dm
2900  */
dc_link_allocate_mst_payload(struct pipe_ctx * pipe_ctx)2901 enum dc_status dc_link_allocate_mst_payload(struct pipe_ctx *pipe_ctx)
2902 {
2903 	struct dc_stream_state *stream = pipe_ctx->stream;
2904 	struct dc_link *link = stream->link;
2905 	struct link_encoder *link_encoder = link->link_enc;
2906 	struct stream_encoder *stream_encoder = pipe_ctx->stream_res.stream_enc;
2907 	struct dp_mst_stream_allocation_table proposed_table = {0};
2908 	struct fixed31_32 avg_time_slots_per_mtp;
2909 	struct fixed31_32 pbn;
2910 	struct fixed31_32 pbn_per_slot;
2911 	uint8_t i;
2912 	enum act_return_status ret;
2913 	DC_LOGGER_INIT(link->ctx->logger);
2914 
2915 	/* enable_link_dp_mst already check link->enabled_stream_count
2916 	 * and stream is in link->stream[]. This is called during set mode,
2917 	 * stream_enc is available.
2918 	 */
2919 
2920 	/* get calculate VC payload for stream: stream_alloc */
2921 	if (dm_helpers_dp_mst_write_payload_allocation_table(
2922 		stream->ctx,
2923 		stream,
2924 		&proposed_table,
2925 		true)) {
2926 		update_mst_stream_alloc_table(
2927 					link, pipe_ctx->stream_res.stream_enc, &proposed_table);
2928 	}
2929 	else
2930 		DC_LOG_WARNING("Failed to update"
2931 				"MST allocation table for"
2932 				"pipe idx:%d\n",
2933 				pipe_ctx->pipe_idx);
2934 
2935 	DC_LOG_MST("%s  "
2936 			"stream_count: %d: \n ",
2937 			__func__,
2938 			link->mst_stream_alloc_table.stream_count);
2939 
2940 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
2941 		DC_LOG_MST("stream_enc[%d]: %p      "
2942 		"stream[%d].vcp_id: %d      "
2943 		"stream[%d].slot_count: %d\n",
2944 		i,
2945 		(void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc,
2946 		i,
2947 		link->mst_stream_alloc_table.stream_allocations[i].vcp_id,
2948 		i,
2949 		link->mst_stream_alloc_table.stream_allocations[i].slot_count);
2950 	}
2951 
2952 	ASSERT(proposed_table.stream_count > 0);
2953 
2954 	/* program DP source TX for payload */
2955 	link_encoder->funcs->update_mst_stream_allocation_table(
2956 		link_encoder,
2957 		&link->mst_stream_alloc_table);
2958 
2959 	/* send down message */
2960 	ret = dm_helpers_dp_mst_poll_for_allocation_change_trigger(
2961 			stream->ctx,
2962 			stream);
2963 
2964 	if (ret != ACT_LINK_LOST) {
2965 		dm_helpers_dp_mst_send_payload_allocation(
2966 				stream->ctx,
2967 				stream,
2968 				true);
2969 	}
2970 
2971 	/* slot X.Y for only current stream */
2972 	pbn_per_slot = get_pbn_per_slot(stream);
2973 	pbn = get_pbn_from_timing(pipe_ctx);
2974 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
2975 
2976 	stream_encoder->funcs->set_throttled_vcp_size(
2977 		stream_encoder,
2978 		avg_time_slots_per_mtp);
2979 
2980 	return DC_OK;
2981 
2982 }
2983 
deallocate_mst_payload(struct pipe_ctx * pipe_ctx)2984 static enum dc_status deallocate_mst_payload(struct pipe_ctx *pipe_ctx)
2985 {
2986 	struct dc_stream_state *stream = pipe_ctx->stream;
2987 	struct dc_link *link = stream->link;
2988 	struct link_encoder *link_encoder = link->link_enc;
2989 	struct stream_encoder *stream_encoder = pipe_ctx->stream_res.stream_enc;
2990 	struct dp_mst_stream_allocation_table proposed_table = {0};
2991 	struct fixed31_32 avg_time_slots_per_mtp = dc_fixpt_from_int(0);
2992 	uint8_t i;
2993 	bool mst_mode = (link->type == dc_connection_mst_branch);
2994 	DC_LOGGER_INIT(link->ctx->logger);
2995 
2996 	/* deallocate_mst_payload is called before disable link. When mode or
2997 	 * disable/enable monitor, new stream is created which is not in link
2998 	 * stream[] yet. For this, payload is not allocated yet, so de-alloc
2999 	 * should not done. For new mode set, map_resources will get engine
3000 	 * for new stream, so stream_enc->id should be validated until here.
3001 	 */
3002 
3003 	/* slot X.Y */
3004 	stream_encoder->funcs->set_throttled_vcp_size(
3005 		stream_encoder,
3006 		avg_time_slots_per_mtp);
3007 
3008 	/* TODO: which component is responsible for remove payload table? */
3009 	if (mst_mode) {
3010 		if (dm_helpers_dp_mst_write_payload_allocation_table(
3011 				stream->ctx,
3012 				stream,
3013 				&proposed_table,
3014 				false)) {
3015 
3016 			update_mst_stream_alloc_table(
3017 				link, pipe_ctx->stream_res.stream_enc, &proposed_table);
3018 		}
3019 		else {
3020 				DC_LOG_WARNING("Failed to update"
3021 						"MST allocation table for"
3022 						"pipe idx:%d\n",
3023 						pipe_ctx->pipe_idx);
3024 		}
3025 	}
3026 
3027 	DC_LOG_MST("%s"
3028 			"stream_count: %d: ",
3029 			__func__,
3030 			link->mst_stream_alloc_table.stream_count);
3031 
3032 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
3033 		DC_LOG_MST("stream_enc[%d]: %p      "
3034 		"stream[%d].vcp_id: %d      "
3035 		"stream[%d].slot_count: %d\n",
3036 		i,
3037 		(void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc,
3038 		i,
3039 		link->mst_stream_alloc_table.stream_allocations[i].vcp_id,
3040 		i,
3041 		link->mst_stream_alloc_table.stream_allocations[i].slot_count);
3042 	}
3043 
3044 	link_encoder->funcs->update_mst_stream_allocation_table(
3045 		link_encoder,
3046 		&link->mst_stream_alloc_table);
3047 
3048 	if (mst_mode) {
3049 		dm_helpers_dp_mst_poll_for_allocation_change_trigger(
3050 			stream->ctx,
3051 			stream);
3052 
3053 		dm_helpers_dp_mst_send_payload_allocation(
3054 			stream->ctx,
3055 			stream,
3056 			false);
3057 	}
3058 
3059 	return DC_OK;
3060 }
3061 
dc_link_reallocate_mst_payload(struct dc_link * link)3062 enum dc_status dc_link_reallocate_mst_payload(struct dc_link *link)
3063 {
3064 	int i;
3065 	struct pipe_ctx *pipe_ctx;
3066 
3067 	// Clear all of MST payload then reallocate
3068 	for (i = 0; i < MAX_PIPES; i++) {
3069 		pipe_ctx = &link->dc->current_state->res_ctx.pipe_ctx[i];
3070 
3071 		/* driver enable split pipe for external monitors
3072 		 * we have to check pipe_ctx is split pipe or not
3073 		 * If it's split pipe, driver using top pipe to
3074 		 * reaallocate.
3075 		 */
3076 		if (!pipe_ctx || pipe_ctx->top_pipe)
3077 			continue;
3078 
3079 		if (pipe_ctx->stream && pipe_ctx->stream->link == link &&
3080 				pipe_ctx->stream->dpms_off == false &&
3081 				pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3082 			deallocate_mst_payload(pipe_ctx);
3083 		}
3084 	}
3085 
3086 	for (i = 0; i < MAX_PIPES; i++) {
3087 		pipe_ctx = &link->dc->current_state->res_ctx.pipe_ctx[i];
3088 
3089 		if (!pipe_ctx || pipe_ctx->top_pipe)
3090 			continue;
3091 
3092 		if (pipe_ctx->stream && pipe_ctx->stream->link == link &&
3093 				pipe_ctx->stream->dpms_off == false &&
3094 				pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3095 			/* enable/disable PHY will clear connection between BE and FE
3096 			 * need to restore it.
3097 			 */
3098 			link->link_enc->funcs->connect_dig_be_to_fe(link->link_enc,
3099 									pipe_ctx->stream_res.stream_enc->id, true);
3100 			dc_link_allocate_mst_payload(pipe_ctx);
3101 		}
3102 	}
3103 
3104 	return DC_OK;
3105 }
3106 
3107 #if defined(CONFIG_DRM_AMD_DC_HDCP)
update_psp_stream_config(struct pipe_ctx * pipe_ctx,bool dpms_off)3108 static void update_psp_stream_config(struct pipe_ctx *pipe_ctx, bool dpms_off)
3109 {
3110 	struct cp_psp *cp_psp = &pipe_ctx->stream->ctx->cp_psp;
3111 	if (cp_psp && cp_psp->funcs.update_stream_config) {
3112 		struct cp_psp_stream_config config;
3113 
3114 		memset(&config, 0, sizeof(config));
3115 
3116 		config.otg_inst = (uint8_t) pipe_ctx->stream_res.tg->inst;
3117 		/*stream_enc_inst*/
3118 		config.stream_enc_inst = (uint8_t) pipe_ctx->stream_res.stream_enc->stream_enc_inst;
3119 		config.link_enc_inst = pipe_ctx->stream->link->link_enc_hw_inst;
3120 		config.dpms_off = dpms_off;
3121 		config.dm_stream_ctx = pipe_ctx->stream->dm_stream_context;
3122 		config.mst_supported = (pipe_ctx->stream->signal ==
3123 				SIGNAL_TYPE_DISPLAY_PORT_MST);
3124 		cp_psp->funcs.update_stream_config(cp_psp->handle, &config);
3125 	}
3126 }
3127 #endif
3128 
core_link_enable_stream(struct dc_state * state,struct pipe_ctx * pipe_ctx)3129 void core_link_enable_stream(
3130 		struct dc_state *state,
3131 		struct pipe_ctx *pipe_ctx)
3132 {
3133 	struct dc *dc = pipe_ctx->stream->ctx->dc;
3134 	struct dc_stream_state *stream = pipe_ctx->stream;
3135 	enum dc_status status;
3136 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
3137 	enum otg_out_mux_dest otg_out_dest = OUT_MUX_DIO;
3138 #endif
3139 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
3140 
3141 	if (!IS_DIAG_DC(dc->ctx->dce_environment) &&
3142 			dc_is_virtual_signal(pipe_ctx->stream->signal))
3143 		return;
3144 
3145 	if (!dc_is_virtual_signal(pipe_ctx->stream->signal)) {
3146 		stream->link->link_enc->funcs->setup(
3147 			stream->link->link_enc,
3148 			pipe_ctx->stream->signal);
3149 		pipe_ctx->stream_res.stream_enc->funcs->setup_stereo_sync(
3150 			pipe_ctx->stream_res.stream_enc,
3151 			pipe_ctx->stream_res.tg->inst,
3152 			stream->timing.timing_3d_format != TIMING_3D_FORMAT_NONE);
3153 	}
3154 
3155 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
3156 		pipe_ctx->stream_res.stream_enc->funcs->dp_set_stream_attribute(
3157 			pipe_ctx->stream_res.stream_enc,
3158 			&stream->timing,
3159 			stream->output_color_space,
3160 			stream->use_vsc_sdp_for_colorimetry,
3161 			stream->link->dpcd_caps.dprx_feature.bits.SST_SPLIT_SDP_CAP);
3162 
3163 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal))
3164 		pipe_ctx->stream_res.stream_enc->funcs->hdmi_set_stream_attribute(
3165 			pipe_ctx->stream_res.stream_enc,
3166 			&stream->timing,
3167 			stream->phy_pix_clk,
3168 			pipe_ctx->stream_res.audio != NULL);
3169 
3170 	pipe_ctx->stream->link->link_state_valid = true;
3171 
3172 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
3173 	if (pipe_ctx->stream_res.tg->funcs->set_out_mux)
3174 		pipe_ctx->stream_res.tg->funcs->set_out_mux(pipe_ctx->stream_res.tg, otg_out_dest);
3175 #endif
3176 
3177 	if (dc_is_dvi_signal(pipe_ctx->stream->signal))
3178 		pipe_ctx->stream_res.stream_enc->funcs->dvi_set_stream_attribute(
3179 			pipe_ctx->stream_res.stream_enc,
3180 			&stream->timing,
3181 			(pipe_ctx->stream->signal == SIGNAL_TYPE_DVI_DUAL_LINK) ?
3182 			true : false);
3183 
3184 	if (dc_is_lvds_signal(pipe_ctx->stream->signal))
3185 		pipe_ctx->stream_res.stream_enc->funcs->lvds_set_stream_attribute(
3186 			pipe_ctx->stream_res.stream_enc,
3187 			&stream->timing);
3188 
3189 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
3190 		bool apply_edp_fast_boot_optimization =
3191 			pipe_ctx->stream->apply_edp_fast_boot_optimization;
3192 
3193 		pipe_ctx->stream->apply_edp_fast_boot_optimization = false;
3194 
3195 		resource_build_info_frame(pipe_ctx);
3196 		dc->hwss.update_info_frame(pipe_ctx);
3197 
3198 		/* Do not touch link on seamless boot optimization. */
3199 		if (pipe_ctx->stream->apply_seamless_boot_optimization) {
3200 			pipe_ctx->stream->dpms_off = false;
3201 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3202 			update_psp_stream_config(pipe_ctx, false);
3203 #endif
3204 			return;
3205 		}
3206 
3207 		/* eDP lit up by bios already, no need to enable again. */
3208 		if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP &&
3209 					apply_edp_fast_boot_optimization) {
3210 			pipe_ctx->stream->dpms_off = false;
3211 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3212 			update_psp_stream_config(pipe_ctx, false);
3213 #endif
3214 			return;
3215 		}
3216 
3217 		if (pipe_ctx->stream->dpms_off)
3218 			return;
3219 
3220 		/* Have to setup DSC before DIG FE and BE are connected (which happens before the
3221 		 * link training). This is to make sure the bandwidth sent to DIG BE won't be
3222 		 * bigger than what the link and/or DIG BE can handle. VBID[6]/CompressedStream_flag
3223 		 * will be automatically set at a later time when the video is enabled
3224 		 * (DP_VID_STREAM_EN = 1).
3225 		 */
3226 		if (pipe_ctx->stream->timing.flags.DSC) {
3227 			if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3228 					dc_is_virtual_signal(pipe_ctx->stream->signal))
3229 				dp_set_dsc_enable(pipe_ctx, true);
3230 		}
3231 
3232 		status = enable_link(state, pipe_ctx);
3233 
3234 		if (status != DC_OK) {
3235 			DC_LOG_WARNING("enabling link %u failed: %d\n",
3236 			pipe_ctx->stream->link->link_index,
3237 			status);
3238 
3239 			/* Abort stream enable *unless* the failure was due to
3240 			 * DP link training - some DP monitors will recover and
3241 			 * show the stream anyway. But MST displays can't proceed
3242 			 * without link training.
3243 			 */
3244 			if (status != DC_FAIL_DP_LINK_TRAINING ||
3245 					pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3246 				BREAK_TO_DEBUGGER();
3247 				return;
3248 			}
3249 		}
3250 
3251 		dc->hwss.enable_audio_stream(pipe_ctx);
3252 
3253 		/* turn off otg test pattern if enable */
3254 		if (pipe_ctx->stream_res.tg->funcs->set_test_pattern)
3255 			pipe_ctx->stream_res.tg->funcs->set_test_pattern(pipe_ctx->stream_res.tg,
3256 					CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
3257 					COLOR_DEPTH_UNDEFINED);
3258 
3259 		/* This second call is needed to reconfigure the DIG
3260 		 * as a workaround for the incorrect value being applied
3261 		 * from transmitter control.
3262 		 */
3263 		if (!dc_is_virtual_signal(pipe_ctx->stream->signal))
3264 			stream->link->link_enc->funcs->setup(
3265 				stream->link->link_enc,
3266 				pipe_ctx->stream->signal);
3267 
3268 		dc->hwss.enable_stream(pipe_ctx);
3269 
3270 		/* Set DPS PPS SDP (AKA "info frames") */
3271 		if (pipe_ctx->stream->timing.flags.DSC) {
3272 			if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3273 					dc_is_virtual_signal(pipe_ctx->stream->signal))
3274 				dp_set_dsc_pps_sdp(pipe_ctx, true);
3275 		}
3276 
3277 		if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3278 			dc_link_allocate_mst_payload(pipe_ctx);
3279 
3280 		dc->hwss.unblank_stream(pipe_ctx,
3281 			&pipe_ctx->stream->link->cur_link_settings);
3282 
3283 		if (stream->sink_patches.delay_ignore_msa > 0)
3284 			msleep(stream->sink_patches.delay_ignore_msa);
3285 
3286 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
3287 			enable_stream_features(pipe_ctx);
3288 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3289 		update_psp_stream_config(pipe_ctx, false);
3290 #endif
3291 	} else { // if (IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment))
3292 		if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3293 				dc_is_virtual_signal(pipe_ctx->stream->signal))
3294 			dp_set_dsc_enable(pipe_ctx, true);
3295 
3296 	}
3297 
3298 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
3299 		core_link_set_avmute(pipe_ctx, false);
3300 	}
3301 }
3302 
core_link_disable_stream(struct pipe_ctx * pipe_ctx)3303 void core_link_disable_stream(struct pipe_ctx *pipe_ctx)
3304 {
3305 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
3306 	struct dc_stream_state *stream = pipe_ctx->stream;
3307 	struct dc_link *link = stream->sink->link;
3308 
3309 	if (!IS_DIAG_DC(dc->ctx->dce_environment) &&
3310 			dc_is_virtual_signal(pipe_ctx->stream->signal))
3311 		return;
3312 
3313 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
3314 		core_link_set_avmute(pipe_ctx, true);
3315 	}
3316 
3317 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3318 	update_psp_stream_config(pipe_ctx, true);
3319 #endif
3320 	dc->hwss.blank_stream(pipe_ctx);
3321 
3322 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3323 		deallocate_mst_payload(pipe_ctx);
3324 
3325 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
3326 		struct ext_hdmi_settings settings = {0};
3327 		enum engine_id eng_id = pipe_ctx->stream_res.stream_enc->id;
3328 
3329 		unsigned short masked_chip_caps = link->chip_caps &
3330 				EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
3331 		//Need to inform that sink is going to use legacy HDMI mode.
3332 		dal_ddc_service_write_scdc_data(
3333 			link->ddc,
3334 			165000,//vbios only handles 165Mhz.
3335 			false);
3336 		if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
3337 			/* DP159, Retimer settings */
3338 			if (get_ext_hdmi_settings(pipe_ctx, eng_id, &settings))
3339 				write_i2c_retimer_setting(pipe_ctx,
3340 						false, false, &settings);
3341 			else
3342 				write_i2c_default_retimer_setting(pipe_ctx,
3343 						false, false);
3344 		} else if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
3345 			/* PI3EQX1204, Redriver settings */
3346 			write_i2c_redriver_setting(pipe_ctx, false);
3347 		}
3348 	}
3349 
3350 	disable_link(pipe_ctx->stream->link, pipe_ctx->stream->signal);
3351 
3352 	dc->hwss.disable_stream(pipe_ctx);
3353 
3354 	if (pipe_ctx->stream->timing.flags.DSC) {
3355 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
3356 			dp_set_dsc_enable(pipe_ctx, false);
3357 	}
3358 }
3359 
core_link_set_avmute(struct pipe_ctx * pipe_ctx,bool enable)3360 void core_link_set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
3361 {
3362 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
3363 
3364 	if (!dc_is_hdmi_signal(pipe_ctx->stream->signal))
3365 		return;
3366 
3367 	dc->hwss.set_avmute(pipe_ctx, enable);
3368 }
3369 
3370 /**
3371  *****************************************************************************
3372  *  Function: dc_link_enable_hpd_filter
3373  *
3374  *  @brief
3375  *     If enable is true, programs HPD filter on associated HPD line using
3376  *     delay_on_disconnect/delay_on_connect values dependent on
3377  *     link->connector_signal
3378  *
3379  *     If enable is false, programs HPD filter on associated HPD line with no
3380  *     delays on connect or disconnect
3381  *
3382  *  @param [in] link: pointer to the dc link
3383  *  @param [in] enable: boolean specifying whether to enable hbd
3384  *****************************************************************************
3385  */
dc_link_enable_hpd_filter(struct dc_link * link,bool enable)3386 void dc_link_enable_hpd_filter(struct dc_link *link, bool enable)
3387 {
3388 	struct gpio *hpd;
3389 
3390 	if (enable) {
3391 		link->is_hpd_filter_disabled = false;
3392 		program_hpd_filter(link);
3393 	} else {
3394 		link->is_hpd_filter_disabled = true;
3395 		/* Obtain HPD handle */
3396 		hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id, link->ctx->gpio_service);
3397 
3398 		if (!hpd)
3399 			return;
3400 
3401 		/* Setup HPD filtering */
3402 		if (dal_gpio_open(hpd, GPIO_MODE_INTERRUPT) == GPIO_RESULT_OK) {
3403 			struct gpio_hpd_config config;
3404 
3405 			config.delay_on_connect = 0;
3406 			config.delay_on_disconnect = 0;
3407 
3408 			dal_irq_setup_hpd_filter(hpd, &config);
3409 
3410 			dal_gpio_close(hpd);
3411 		} else {
3412 			ASSERT_CRITICAL(false);
3413 		}
3414 		/* Release HPD handle */
3415 		dal_gpio_destroy_irq(&hpd);
3416 	}
3417 }
3418 
dc_bandwidth_in_kbps_from_timing(const struct dc_crtc_timing * timing)3419 uint32_t dc_bandwidth_in_kbps_from_timing(
3420 	const struct dc_crtc_timing *timing)
3421 {
3422 	uint32_t bits_per_channel = 0;
3423 	uint32_t kbps;
3424 	struct fixed31_32 link_bw_kbps;
3425 
3426 	if (timing->flags.DSC) {
3427 		link_bw_kbps = dc_fixpt_from_int(timing->pix_clk_100hz);
3428 		link_bw_kbps = dc_fixpt_div_int(link_bw_kbps, 160);
3429 		link_bw_kbps = dc_fixpt_mul_int(link_bw_kbps, timing->dsc_cfg.bits_per_pixel);
3430 		kbps = dc_fixpt_ceil(link_bw_kbps);
3431 		return kbps;
3432 	}
3433 
3434 	switch (timing->display_color_depth) {
3435 	case COLOR_DEPTH_666:
3436 		bits_per_channel = 6;
3437 		break;
3438 	case COLOR_DEPTH_888:
3439 		bits_per_channel = 8;
3440 		break;
3441 	case COLOR_DEPTH_101010:
3442 		bits_per_channel = 10;
3443 		break;
3444 	case COLOR_DEPTH_121212:
3445 		bits_per_channel = 12;
3446 		break;
3447 	case COLOR_DEPTH_141414:
3448 		bits_per_channel = 14;
3449 		break;
3450 	case COLOR_DEPTH_161616:
3451 		bits_per_channel = 16;
3452 		break;
3453 	default:
3454 		break;
3455 	}
3456 
3457 	ASSERT(bits_per_channel != 0);
3458 
3459 	kbps = timing->pix_clk_100hz / 10;
3460 	kbps *= bits_per_channel;
3461 
3462 	if (timing->flags.Y_ONLY != 1) {
3463 		/*Only YOnly make reduce bandwidth by 1/3 compares to RGB*/
3464 		kbps *= 3;
3465 		if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
3466 			kbps /= 2;
3467 		else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
3468 			kbps = kbps * 2 / 3;
3469 	}
3470 
3471 	return kbps;
3472 
3473 }
3474 
dc_link_set_drive_settings(struct dc * dc,struct link_training_settings * lt_settings,const struct dc_link * link)3475 void dc_link_set_drive_settings(struct dc *dc,
3476 				struct link_training_settings *lt_settings,
3477 				const struct dc_link *link)
3478 {
3479 
3480 	int i;
3481 
3482 	for (i = 0; i < dc->link_count; i++) {
3483 		if (dc->links[i] == link)
3484 			break;
3485 	}
3486 
3487 	if (i >= dc->link_count)
3488 		ASSERT_CRITICAL(false);
3489 
3490 	dc_link_dp_set_drive_settings(dc->links[i], lt_settings);
3491 }
3492 
dc_link_perform_link_training(struct dc * dc,struct dc_link_settings * link_setting,bool skip_video_pattern)3493 void dc_link_perform_link_training(struct dc *dc,
3494 				   struct dc_link_settings *link_setting,
3495 				   bool skip_video_pattern)
3496 {
3497 	int i;
3498 
3499 	for (i = 0; i < dc->link_count; i++)
3500 		dc_link_dp_perform_link_training(
3501 			dc->links[i],
3502 			link_setting,
3503 			skip_video_pattern);
3504 }
3505 
dc_link_set_preferred_link_settings(struct dc * dc,struct dc_link_settings * link_setting,struct dc_link * link)3506 void dc_link_set_preferred_link_settings(struct dc *dc,
3507 					 struct dc_link_settings *link_setting,
3508 					 struct dc_link *link)
3509 {
3510 	int i;
3511 	struct pipe_ctx *pipe;
3512 	struct dc_stream_state *link_stream;
3513 	struct dc_link_settings store_settings = *link_setting;
3514 
3515 	link->preferred_link_setting = store_settings;
3516 
3517 	/* Retrain with preferred link settings only relevant for
3518 	 * DP signal type
3519 	 * Check for non-DP signal or if passive dongle present
3520 	 */
3521 	if (!dc_is_dp_signal(link->connector_signal) ||
3522 		link->dongle_max_pix_clk > 0)
3523 		return;
3524 
3525 	for (i = 0; i < MAX_PIPES; i++) {
3526 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3527 		if (pipe->stream && pipe->stream->link) {
3528 			if (pipe->stream->link == link) {
3529 				link_stream = pipe->stream;
3530 				break;
3531 			}
3532 		}
3533 	}
3534 
3535 	/* Stream not found */
3536 	if (i == MAX_PIPES)
3537 		return;
3538 
3539 	/* Cannot retrain link if backend is off */
3540 	if (link_stream->dpms_off)
3541 		return;
3542 
3543 	decide_link_settings(link_stream, &store_settings);
3544 
3545 	if ((store_settings.lane_count != LANE_COUNT_UNKNOWN) &&
3546 		(store_settings.link_rate != LINK_RATE_UNKNOWN))
3547 		dp_retrain_link_dp_test(link, &store_settings, false);
3548 }
3549 
dc_link_set_preferred_training_settings(struct dc * dc,struct dc_link_settings * link_setting,struct dc_link_training_overrides * lt_overrides,struct dc_link * link,bool skip_immediate_retrain)3550 void dc_link_set_preferred_training_settings(struct dc *dc,
3551 						 struct dc_link_settings *link_setting,
3552 						 struct dc_link_training_overrides *lt_overrides,
3553 						 struct dc_link *link,
3554 						 bool skip_immediate_retrain)
3555 {
3556 	if (lt_overrides != NULL)
3557 		link->preferred_training_settings = *lt_overrides;
3558 	else
3559 		memset(&link->preferred_training_settings, 0, sizeof(link->preferred_training_settings));
3560 
3561 	if (link_setting != NULL) {
3562 		link->preferred_link_setting = *link_setting;
3563 	} else {
3564 		link->preferred_link_setting.lane_count = LANE_COUNT_UNKNOWN;
3565 		link->preferred_link_setting.link_rate = LINK_RATE_UNKNOWN;
3566 	}
3567 
3568 	/* Retrain now, or wait until next stream update to apply */
3569 	if (skip_immediate_retrain == false)
3570 		dc_link_set_preferred_link_settings(dc, &link->preferred_link_setting, link);
3571 }
3572 
dc_link_enable_hpd(const struct dc_link * link)3573 void dc_link_enable_hpd(const struct dc_link *link)
3574 {
3575 	dc_link_dp_enable_hpd(link);
3576 }
3577 
dc_link_disable_hpd(const struct dc_link * link)3578 void dc_link_disable_hpd(const struct dc_link *link)
3579 {
3580 	dc_link_dp_disable_hpd(link);
3581 }
3582 
dc_link_set_test_pattern(struct dc_link * link,enum dp_test_pattern test_pattern,enum dp_test_pattern_color_space test_pattern_color_space,const struct link_training_settings * p_link_settings,const unsigned char * p_custom_pattern,unsigned int cust_pattern_size)3583 void dc_link_set_test_pattern(struct dc_link *link,
3584 			      enum dp_test_pattern test_pattern,
3585 			      enum dp_test_pattern_color_space test_pattern_color_space,
3586 			      const struct link_training_settings *p_link_settings,
3587 			      const unsigned char *p_custom_pattern,
3588 			      unsigned int cust_pattern_size)
3589 {
3590 	if (link != NULL)
3591 		dc_link_dp_set_test_pattern(
3592 			link,
3593 			test_pattern,
3594 			test_pattern_color_space,
3595 			p_link_settings,
3596 			p_custom_pattern,
3597 			cust_pattern_size);
3598 }
3599 
dc_link_bandwidth_kbps(const struct dc_link * link,const struct dc_link_settings * link_setting)3600 uint32_t dc_link_bandwidth_kbps(
3601 	const struct dc_link *link,
3602 	const struct dc_link_settings *link_setting)
3603 {
3604 	uint32_t link_bw_kbps =
3605 		link_setting->link_rate * LINK_RATE_REF_FREQ_IN_KHZ; /* bytes per sec */
3606 
3607 	link_bw_kbps *= 8;   /* 8 bits per byte*/
3608 	link_bw_kbps *= link_setting->lane_count;
3609 
3610 	if (dc_link_is_fec_supported(link) && !link->dc->debug.disable_fec) {
3611 		/* Account for FEC overhead.
3612 		 * We have to do it based on caps,
3613 		 * and not based on FEC being set ready,
3614 		 * because FEC is set ready too late in
3615 		 * the process to correctly be picked up
3616 		 * by mode enumeration.
3617 		 *
3618 		 * There's enough zeros at the end of 'kbps'
3619 		 * that make the below operation 100% precise
3620 		 * for our purposes.
3621 		 * 'long long' makes it work even for HDMI 2.1
3622 		 * max bandwidth (and much, much bigger bandwidths
3623 		 * than that, actually).
3624 		 *
3625 		 * NOTE: Reducing link BW by 3% may not be precise
3626 		 * because it may be a stream BT that increases by 3%, and so
3627 		 * 1/1.03 = 0.970873 factor should have been used instead,
3628 		 * but the difference is minimal and is in a safe direction,
3629 		 * which all works well around potential ambiguity of DP 1.4a spec.
3630 		 */
3631 		link_bw_kbps = mul_u64_u32_shr(BIT_ULL(32) * 970LL / 1000,
3632 					       link_bw_kbps, 32);
3633 	}
3634 
3635 	return link_bw_kbps;
3636 
3637 }
3638 
dc_link_get_link_cap(const struct dc_link * link)3639 const struct dc_link_settings *dc_link_get_link_cap(
3640 		const struct dc_link *link)
3641 {
3642 	if (link->preferred_link_setting.lane_count != LANE_COUNT_UNKNOWN &&
3643 			link->preferred_link_setting.link_rate != LINK_RATE_UNKNOWN)
3644 		return &link->preferred_link_setting;
3645 	return &link->verified_link_cap;
3646 }
3647 
dc_link_overwrite_extended_receiver_cap(struct dc_link * link)3648 void dc_link_overwrite_extended_receiver_cap(
3649 		struct dc_link *link)
3650 {
3651 	dp_overwrite_extended_receiver_cap(link);
3652 }
3653 
dc_link_is_fec_supported(const struct dc_link * link)3654 bool dc_link_is_fec_supported(const struct dc_link *link)
3655 {
3656 	return (dc_is_dp_signal(link->connector_signal) &&
3657 			link->link_enc->features.fec_supported &&
3658 			link->dpcd_caps.fec_cap.bits.FEC_CAPABLE &&
3659 			!IS_FPGA_MAXIMUS_DC(link->ctx->dce_environment));
3660 }
3661 
3662