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 
1053 				return false;
1054 			}
1055 		default:
1056 			break;
1057 		}
1058 
1059 		if (link->local_sink->edid_caps.panel_patch.disable_fec)
1060 			link->ctx->dc->debug.disable_fec = true;
1061 
1062 		// Check if edid is the same
1063 		if ((prev_sink) &&
1064 		    (edid_status == EDID_THE_SAME || edid_status == EDID_OK))
1065 			same_edid = is_same_edid(&prev_sink->dc_edid,
1066 						 &sink->dc_edid);
1067 
1068 		if (sink->edid_caps.panel_patch.skip_scdc_overwrite)
1069 			link->ctx->dc->debug.hdmi20_disable = true;
1070 
1071 		if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
1072 		    sink_caps.transaction_type ==
1073 		    DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
1074 			/*
1075 			 * TODO debug why Dell 2413 doesn't like
1076 			 *  two link trainings
1077 			 */
1078 #if defined(CONFIG_DRM_AMD_DC_HDCP)
1079 			query_hdcp_capability(sink->sink_signal, link);
1080 #endif
1081 
1082 			// verify link cap for SST non-seamless boot
1083 			if (!perform_dp_seamless_boot)
1084 				dp_verify_link_cap_with_retries(link,
1085 								&link->reported_link_cap,
1086 								LINK_TRAINING_MAX_VERIFY_RETRY);
1087 		} else {
1088 			// If edid is the same, then discard new sink and revert back to original sink
1089 			if (same_edid) {
1090 				link_disconnect_remap(prev_sink, link);
1091 				sink = prev_sink;
1092 				prev_sink = NULL;
1093 			}
1094 #if defined(CONFIG_DRM_AMD_DC_HDCP)
1095 			query_hdcp_capability(sink->sink_signal, link);
1096 #endif
1097 		}
1098 
1099 		/* HDMI-DVI Dongle */
1100 		if (sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A &&
1101 		    !sink->edid_caps.edid_hdmi)
1102 			sink->sink_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1103 
1104 		/* Connectivity log: detection */
1105 		for (i = 0; i < sink->dc_edid.length / DC_EDID_BLOCK_SIZE; i++) {
1106 			CONN_DATA_DETECT(link,
1107 					 &sink->dc_edid.raw_edid[i * DC_EDID_BLOCK_SIZE],
1108 					 DC_EDID_BLOCK_SIZE,
1109 					 "%s: [Block %d] ", sink->edid_caps.display_name, i);
1110 		}
1111 
1112 		DC_LOG_DETECTION_EDID_PARSER("%s: "
1113 			"manufacturer_id = %X, "
1114 			"product_id = %X, "
1115 			"serial_number = %X, "
1116 			"manufacture_week = %d, "
1117 			"manufacture_year = %d, "
1118 			"display_name = %s, "
1119 			"speaker_flag = %d, "
1120 			"audio_mode_count = %d\n",
1121 			__func__,
1122 			sink->edid_caps.manufacturer_id,
1123 			sink->edid_caps.product_id,
1124 			sink->edid_caps.serial_number,
1125 			sink->edid_caps.manufacture_week,
1126 			sink->edid_caps.manufacture_year,
1127 			sink->edid_caps.display_name,
1128 			sink->edid_caps.speaker_flags,
1129 			sink->edid_caps.audio_mode_count);
1130 
1131 		for (i = 0; i < sink->edid_caps.audio_mode_count; i++) {
1132 			DC_LOG_DETECTION_EDID_PARSER("%s: mode number = %d, "
1133 				"format_code = %d, "
1134 				"channel_count = %d, "
1135 				"sample_rate = %d, "
1136 				"sample_size = %d\n",
1137 				__func__,
1138 				i,
1139 				sink->edid_caps.audio_modes[i].format_code,
1140 				sink->edid_caps.audio_modes[i].channel_count,
1141 				sink->edid_caps.audio_modes[i].sample_rate,
1142 				sink->edid_caps.audio_modes[i].sample_size);
1143 		}
1144 	} else {
1145 		/* From Connected-to-Disconnected. */
1146 		if (link->type == dc_connection_mst_branch) {
1147 			LINK_INFO("link=%d, mst branch is now Disconnected\n",
1148 				  link->link_index);
1149 
1150 			dm_helpers_dp_mst_stop_top_mgr(link->ctx, link);
1151 
1152 			link->mst_stream_alloc_table.stream_count = 0;
1153 			memset(link->mst_stream_alloc_table.stream_allocations,
1154 			       0,
1155 			       sizeof(link->mst_stream_alloc_table.stream_allocations));
1156 		}
1157 
1158 		link->type = dc_connection_none;
1159 		sink_caps.signal = SIGNAL_TYPE_NONE;
1160 		/* When we unplug a passive DP-HDMI dongle connection, dongle_max_pix_clk
1161 		 *  is not cleared. If we emulate a DP signal on this connection, it thinks
1162 		 *  the dongle is still there and limits the number of modes we can emulate.
1163 		 *  Clear dongle_max_pix_clk on disconnect to fix this
1164 		 */
1165 		link->dongle_max_pix_clk = 0;
1166 	}
1167 
1168 	LINK_INFO("link=%d, dc_sink_in=%p is now %s prev_sink=%p dpcd same=%d edid same=%d\n",
1169 		  link->link_index, sink,
1170 		  (sink_caps.signal ==
1171 		   SIGNAL_TYPE_NONE ? "Disconnected" : "Connected"),
1172 		  prev_sink, same_dpcd, same_edid);
1173 
1174 	if (prev_sink)
1175 		dc_sink_release(prev_sink);
1176 
1177 	return true;
1178 }
1179 
dc_link_detect(struct dc_link * link,enum dc_detect_reason reason)1180 bool dc_link_detect(struct dc_link *link, enum dc_detect_reason reason)
1181 {
1182 	const struct dc *dc = link->dc;
1183 	bool ret;
1184 
1185 	/* get out of low power state */
1186 	clk_mgr_exit_optimized_pwr_state(dc, dc->clk_mgr);
1187 
1188 	ret = dc_link_detect_helper(link, reason);
1189 
1190 	/* Go back to power optimized state */
1191 	clk_mgr_optimize_pwr_state(dc, dc->clk_mgr);
1192 
1193 	return ret;
1194 }
1195 
dc_link_get_hpd_state(struct dc_link * dc_link)1196 bool dc_link_get_hpd_state(struct dc_link *dc_link)
1197 {
1198 	uint32_t state;
1199 
1200 	dal_gpio_lock_pin(dc_link->hpd_gpio);
1201 	dal_gpio_get_value(dc_link->hpd_gpio, &state);
1202 	dal_gpio_unlock_pin(dc_link->hpd_gpio);
1203 
1204 	return state;
1205 }
1206 
get_hpd_line(struct dc_link * link)1207 static enum hpd_source_id get_hpd_line(struct dc_link *link)
1208 {
1209 	struct gpio *hpd;
1210 	enum hpd_source_id hpd_id = HPD_SOURCEID_UNKNOWN;
1211 
1212 	hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
1213 			   link->ctx->gpio_service);
1214 
1215 	if (hpd) {
1216 		switch (dal_irq_get_source(hpd)) {
1217 		case DC_IRQ_SOURCE_HPD1:
1218 			hpd_id = HPD_SOURCEID1;
1219 		break;
1220 		case DC_IRQ_SOURCE_HPD2:
1221 			hpd_id = HPD_SOURCEID2;
1222 		break;
1223 		case DC_IRQ_SOURCE_HPD3:
1224 			hpd_id = HPD_SOURCEID3;
1225 		break;
1226 		case DC_IRQ_SOURCE_HPD4:
1227 			hpd_id = HPD_SOURCEID4;
1228 		break;
1229 		case DC_IRQ_SOURCE_HPD5:
1230 			hpd_id = HPD_SOURCEID5;
1231 		break;
1232 		case DC_IRQ_SOURCE_HPD6:
1233 			hpd_id = HPD_SOURCEID6;
1234 		break;
1235 		default:
1236 			BREAK_TO_DEBUGGER();
1237 		break;
1238 		}
1239 
1240 		dal_gpio_destroy_irq(&hpd);
1241 	}
1242 
1243 	return hpd_id;
1244 }
1245 
get_ddc_line(struct dc_link * link)1246 static enum channel_id get_ddc_line(struct dc_link *link)
1247 {
1248 	struct ddc *ddc;
1249 	enum channel_id channel = CHANNEL_ID_UNKNOWN;
1250 
1251 	ddc = dal_ddc_service_get_ddc_pin(link->ddc);
1252 
1253 	if (ddc) {
1254 		switch (dal_ddc_get_line(ddc)) {
1255 		case GPIO_DDC_LINE_DDC1:
1256 			channel = CHANNEL_ID_DDC1;
1257 			break;
1258 		case GPIO_DDC_LINE_DDC2:
1259 			channel = CHANNEL_ID_DDC2;
1260 			break;
1261 		case GPIO_DDC_LINE_DDC3:
1262 			channel = CHANNEL_ID_DDC3;
1263 			break;
1264 		case GPIO_DDC_LINE_DDC4:
1265 			channel = CHANNEL_ID_DDC4;
1266 			break;
1267 		case GPIO_DDC_LINE_DDC5:
1268 			channel = CHANNEL_ID_DDC5;
1269 			break;
1270 		case GPIO_DDC_LINE_DDC6:
1271 			channel = CHANNEL_ID_DDC6;
1272 			break;
1273 		case GPIO_DDC_LINE_DDC_VGA:
1274 			channel = CHANNEL_ID_DDC_VGA;
1275 			break;
1276 		case GPIO_DDC_LINE_I2C_PAD:
1277 			channel = CHANNEL_ID_I2C_PAD;
1278 			break;
1279 		default:
1280 			BREAK_TO_DEBUGGER();
1281 			break;
1282 		}
1283 	}
1284 
1285 	return channel;
1286 }
1287 
translate_encoder_to_transmitter(struct graphics_object_id encoder)1288 static enum transmitter translate_encoder_to_transmitter(struct graphics_object_id encoder)
1289 {
1290 	switch (encoder.id) {
1291 	case ENCODER_ID_INTERNAL_UNIPHY:
1292 		switch (encoder.enum_id) {
1293 		case ENUM_ID_1:
1294 			return TRANSMITTER_UNIPHY_A;
1295 		case ENUM_ID_2:
1296 			return TRANSMITTER_UNIPHY_B;
1297 		default:
1298 			return TRANSMITTER_UNKNOWN;
1299 		}
1300 	break;
1301 	case ENCODER_ID_INTERNAL_UNIPHY1:
1302 		switch (encoder.enum_id) {
1303 		case ENUM_ID_1:
1304 			return TRANSMITTER_UNIPHY_C;
1305 		case ENUM_ID_2:
1306 			return TRANSMITTER_UNIPHY_D;
1307 		default:
1308 			return TRANSMITTER_UNKNOWN;
1309 		}
1310 	break;
1311 	case ENCODER_ID_INTERNAL_UNIPHY2:
1312 		switch (encoder.enum_id) {
1313 		case ENUM_ID_1:
1314 			return TRANSMITTER_UNIPHY_E;
1315 		case ENUM_ID_2:
1316 			return TRANSMITTER_UNIPHY_F;
1317 		default:
1318 			return TRANSMITTER_UNKNOWN;
1319 		}
1320 	break;
1321 	case ENCODER_ID_INTERNAL_UNIPHY3:
1322 		switch (encoder.enum_id) {
1323 		case ENUM_ID_1:
1324 			return TRANSMITTER_UNIPHY_G;
1325 		default:
1326 			return TRANSMITTER_UNKNOWN;
1327 		}
1328 	break;
1329 	case ENCODER_ID_EXTERNAL_NUTMEG:
1330 		switch (encoder.enum_id) {
1331 		case ENUM_ID_1:
1332 			return TRANSMITTER_NUTMEG_CRT;
1333 		default:
1334 			return TRANSMITTER_UNKNOWN;
1335 		}
1336 	break;
1337 	case ENCODER_ID_EXTERNAL_TRAVIS:
1338 		switch (encoder.enum_id) {
1339 		case ENUM_ID_1:
1340 			return TRANSMITTER_TRAVIS_CRT;
1341 		case ENUM_ID_2:
1342 			return TRANSMITTER_TRAVIS_LCD;
1343 		default:
1344 			return TRANSMITTER_UNKNOWN;
1345 		}
1346 	break;
1347 	default:
1348 		return TRANSMITTER_UNKNOWN;
1349 	}
1350 }
1351 
dc_link_construct(struct dc_link * link,const struct link_init_data * init_params)1352 static bool dc_link_construct(struct dc_link *link,
1353 			      const struct link_init_data *init_params)
1354 {
1355 	uint8_t i;
1356 	struct ddc_service_init_data ddc_service_init_data = { { 0 } };
1357 	struct dc_context *dc_ctx = init_params->ctx;
1358 	struct encoder_init_data enc_init_data = { 0 };
1359 	struct panel_cntl_init_data panel_cntl_init_data = { 0 };
1360 	struct integrated_info info = {{{ 0 }}};
1361 	struct dc_bios *bios = init_params->dc->ctx->dc_bios;
1362 	const struct dc_vbios_funcs *bp_funcs = bios->funcs;
1363 
1364 	DC_LOGGER_INIT(dc_ctx->logger);
1365 
1366 	link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
1367 	link->irq_source_hpd_rx = DC_IRQ_SOURCE_INVALID;
1368 
1369 	link->link_status.dpcd_caps = &link->dpcd_caps;
1370 
1371 	link->dc = init_params->dc;
1372 	link->ctx = dc_ctx;
1373 	link->link_index = init_params->link_index;
1374 
1375 	memset(&link->preferred_training_settings, 0,
1376 	       sizeof(struct dc_link_training_overrides));
1377 	memset(&link->preferred_link_setting, 0,
1378 	       sizeof(struct dc_link_settings));
1379 
1380 	link->link_id =
1381 		bios->funcs->get_connector_id(bios, init_params->connector_index);
1382 
1383 	if (link->link_id.type != OBJECT_TYPE_CONNECTOR) {
1384 		dm_output_to_console("%s: Invalid Connector ObjectID from Adapter Service for connector index:%d! type %d expected %d\n",
1385 				     __func__, init_params->connector_index,
1386 				     link->link_id.type, OBJECT_TYPE_CONNECTOR);
1387 		goto create_fail;
1388 	}
1389 
1390 	if (link->dc->res_pool->funcs->link_init)
1391 		link->dc->res_pool->funcs->link_init(link);
1392 
1393 	link->hpd_gpio = get_hpd_gpio(link->ctx->dc_bios, link->link_id,
1394 				      link->ctx->gpio_service);
1395 	if (link->hpd_gpio) {
1396 		dal_gpio_open(link->hpd_gpio, GPIO_MODE_INTERRUPT);
1397 		dal_gpio_unlock_pin(link->hpd_gpio);
1398 		link->irq_source_hpd = dal_irq_get_source(link->hpd_gpio);
1399 	}
1400 
1401 	switch (link->link_id.id) {
1402 	case CONNECTOR_ID_HDMI_TYPE_A:
1403 		link->connector_signal = SIGNAL_TYPE_HDMI_TYPE_A;
1404 
1405 		break;
1406 	case CONNECTOR_ID_SINGLE_LINK_DVID:
1407 	case CONNECTOR_ID_SINGLE_LINK_DVII:
1408 		link->connector_signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1409 		break;
1410 	case CONNECTOR_ID_DUAL_LINK_DVID:
1411 	case CONNECTOR_ID_DUAL_LINK_DVII:
1412 		link->connector_signal = SIGNAL_TYPE_DVI_DUAL_LINK;
1413 		break;
1414 	case CONNECTOR_ID_DISPLAY_PORT:
1415 		link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
1416 
1417 		if (link->hpd_gpio)
1418 			link->irq_source_hpd_rx =
1419 					dal_irq_get_rx_source(link->hpd_gpio);
1420 
1421 		break;
1422 	case CONNECTOR_ID_EDP:
1423 		link->connector_signal = SIGNAL_TYPE_EDP;
1424 
1425 		if (link->hpd_gpio) {
1426 			link->irq_source_hpd = DC_IRQ_SOURCE_INVALID;
1427 			link->irq_source_hpd_rx =
1428 					dal_irq_get_rx_source(link->hpd_gpio);
1429 		}
1430 
1431 		break;
1432 	case CONNECTOR_ID_LVDS:
1433 		link->connector_signal = SIGNAL_TYPE_LVDS;
1434 		break;
1435 	default:
1436 		DC_LOG_WARNING("Unsupported Connector type:%d!\n",
1437 			       link->link_id.id);
1438 		goto create_fail;
1439 	}
1440 
1441 	/* TODO: #DAL3 Implement id to str function.*/
1442 	LINK_INFO("Connector[%d] description:"
1443 		  "signal %d\n",
1444 		  init_params->connector_index,
1445 		  link->connector_signal);
1446 
1447 	ddc_service_init_data.ctx = link->ctx;
1448 	ddc_service_init_data.id = link->link_id;
1449 	ddc_service_init_data.link = link;
1450 	link->ddc = dal_ddc_service_create(&ddc_service_init_data);
1451 
1452 	if (!link->ddc) {
1453 		DC_ERROR("Failed to create ddc_service!\n");
1454 		goto ddc_create_fail;
1455 	}
1456 
1457 	link->ddc_hw_inst =
1458 		dal_ddc_get_line(dal_ddc_service_get_ddc_pin(link->ddc));
1459 
1460 
1461 	if (link->dc->res_pool->funcs->panel_cntl_create &&
1462 		(link->link_id.id == CONNECTOR_ID_EDP ||
1463 			link->link_id.id == CONNECTOR_ID_LVDS)) {
1464 		panel_cntl_init_data.ctx = dc_ctx;
1465 		panel_cntl_init_data.inst = 0;
1466 		link->panel_cntl =
1467 			link->dc->res_pool->funcs->panel_cntl_create(
1468 								&panel_cntl_init_data);
1469 
1470 		if (link->panel_cntl == NULL) {
1471 			DC_ERROR("Failed to create link panel_cntl!\n");
1472 			goto panel_cntl_create_fail;
1473 		}
1474 	}
1475 
1476 	enc_init_data.ctx = dc_ctx;
1477 	bp_funcs->get_src_obj(dc_ctx->dc_bios, link->link_id, 0,
1478 			      &enc_init_data.encoder);
1479 	enc_init_data.connector = link->link_id;
1480 	enc_init_data.channel = get_ddc_line(link);
1481 	enc_init_data.hpd_source = get_hpd_line(link);
1482 
1483 	link->hpd_src = enc_init_data.hpd_source;
1484 
1485 	enc_init_data.transmitter =
1486 		translate_encoder_to_transmitter(enc_init_data.encoder);
1487 	link->link_enc =
1488 		link->dc->res_pool->funcs->link_enc_create(&enc_init_data);
1489 
1490 	if (!link->link_enc) {
1491 		DC_ERROR("Failed to create link encoder!\n");
1492 		goto link_enc_create_fail;
1493 	}
1494 
1495 	link->link_enc_hw_inst = link->link_enc->transmitter;
1496 
1497 	for (i = 0; i < 4; i++) {
1498 		if (bp_funcs->get_device_tag(dc_ctx->dc_bios,
1499 					     link->link_id, i,
1500 					     &link->device_tag) != BP_RESULT_OK) {
1501 			DC_ERROR("Failed to find device tag!\n");
1502 			goto device_tag_fail;
1503 		}
1504 
1505 		/* Look for device tag that matches connector signal,
1506 		 * CRT for rgb, LCD for other supported signal tyes
1507 		 */
1508 		if (!bp_funcs->is_device_id_supported(dc_ctx->dc_bios,
1509 						      link->device_tag.dev_id))
1510 			continue;
1511 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_CRT &&
1512 		    link->connector_signal != SIGNAL_TYPE_RGB)
1513 			continue;
1514 		if (link->device_tag.dev_id.device_type == DEVICE_TYPE_LCD &&
1515 		    link->connector_signal == SIGNAL_TYPE_RGB)
1516 			continue;
1517 		break;
1518 	}
1519 
1520 	if (bios->integrated_info)
1521 		info = *bios->integrated_info;
1522 
1523 	/* Look for channel mapping corresponding to connector and device tag */
1524 	for (i = 0; i < MAX_NUMBER_OF_EXT_DISPLAY_PATH; i++) {
1525 		struct external_display_path *path =
1526 			&info.ext_disp_conn_info.path[i];
1527 
1528 		if (path->device_connector_id.enum_id == link->link_id.enum_id &&
1529 		    path->device_connector_id.id == link->link_id.id &&
1530 		    path->device_connector_id.type == link->link_id.type) {
1531 			if (link->device_tag.acpi_device != 0 &&
1532 			    path->device_acpi_enum == link->device_tag.acpi_device) {
1533 				link->ddi_channel_mapping = path->channel_mapping;
1534 				link->chip_caps = path->caps;
1535 			} else if (path->device_tag ==
1536 				   link->device_tag.dev_id.raw_device_tag) {
1537 				link->ddi_channel_mapping = path->channel_mapping;
1538 				link->chip_caps = path->caps;
1539 			}
1540 			break;
1541 		}
1542 	}
1543 
1544 	if (bios->funcs->get_atom_dc_golden_table)
1545 		bios->funcs->get_atom_dc_golden_table(bios);
1546 
1547 	/*
1548 	 * TODO check if GPIO programmed correctly
1549 	 *
1550 	 * If GPIO isn't programmed correctly HPD might not rise or drain
1551 	 * fast enough, leading to bounces.
1552 	 */
1553 	program_hpd_filter(link);
1554 
1555 	link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
1556 
1557 	return true;
1558 device_tag_fail:
1559 	link->link_enc->funcs->destroy(&link->link_enc);
1560 link_enc_create_fail:
1561 	if (link->panel_cntl != NULL)
1562 		link->panel_cntl->funcs->destroy(&link->panel_cntl);
1563 panel_cntl_create_fail:
1564 	dal_ddc_service_destroy(&link->ddc);
1565 ddc_create_fail:
1566 create_fail:
1567 
1568 	if (link->hpd_gpio) {
1569 		dal_gpio_destroy_irq(&link->hpd_gpio);
1570 		link->hpd_gpio = NULL;
1571 	}
1572 
1573 	return false;
1574 }
1575 
1576 /*******************************************************************************
1577  * Public functions
1578  ******************************************************************************/
link_create(const struct link_init_data * init_params)1579 struct dc_link *link_create(const struct link_init_data *init_params)
1580 {
1581 	struct dc_link *link =
1582 			kzalloc(sizeof(*link), GFP_KERNEL);
1583 
1584 	if (NULL == link)
1585 		goto alloc_fail;
1586 
1587 	if (false == dc_link_construct(link, init_params))
1588 		goto construct_fail;
1589 
1590 	return link;
1591 
1592 construct_fail:
1593 	kfree(link);
1594 
1595 alloc_fail:
1596 	return NULL;
1597 }
1598 
link_destroy(struct dc_link ** link)1599 void link_destroy(struct dc_link **link)
1600 {
1601 	dc_link_destruct(*link);
1602 	kfree(*link);
1603 	*link = NULL;
1604 }
1605 
enable_stream_features(struct pipe_ctx * pipe_ctx)1606 static void enable_stream_features(struct pipe_ctx *pipe_ctx)
1607 {
1608 	struct dc_stream_state *stream = pipe_ctx->stream;
1609 	struct dc_link *link = stream->link;
1610 	union down_spread_ctrl old_downspread;
1611 	union down_spread_ctrl new_downspread;
1612 
1613 	core_link_read_dpcd(link, DP_DOWNSPREAD_CTRL,
1614 			&old_downspread.raw, sizeof(old_downspread));
1615 
1616 	new_downspread.raw = old_downspread.raw;
1617 
1618 	new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
1619 			(stream->ignore_msa_timing_param) ? 1 : 0;
1620 
1621 	if (new_downspread.raw != old_downspread.raw) {
1622 		core_link_write_dpcd(link, DP_DOWNSPREAD_CTRL,
1623 			&new_downspread.raw, sizeof(new_downspread));
1624 	}
1625 }
1626 
enable_link_dp(struct dc_state * state,struct pipe_ctx * pipe_ctx)1627 static enum dc_status enable_link_dp(struct dc_state *state,
1628 				     struct pipe_ctx *pipe_ctx)
1629 {
1630 	struct dc_stream_state *stream = pipe_ctx->stream;
1631 	enum dc_status status;
1632 	bool skip_video_pattern;
1633 	struct dc_link *link = stream->link;
1634 	struct dc_link_settings link_settings = {0};
1635 	bool fec_enable;
1636 	int i;
1637 	bool apply_seamless_boot_optimization = false;
1638 	uint32_t bl_oled_enable_delay = 50; // in ms
1639 	const uint32_t post_oui_delay = 30; // 30ms
1640 
1641 	// check for seamless boot
1642 	for (i = 0; i < state->stream_count; i++) {
1643 		if (state->streams[i]->apply_seamless_boot_optimization) {
1644 			apply_seamless_boot_optimization = true;
1645 			break;
1646 		}
1647 	}
1648 
1649 	/* get link settings for video mode timing */
1650 	decide_link_settings(stream, &link_settings);
1651 
1652 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP) {
1653 		/*in case it is not on*/
1654 		link->dc->hwss.edp_power_control(link, true);
1655 		link->dc->hwss.edp_wait_for_hpd_ready(link, true);
1656 	}
1657 
1658 	pipe_ctx->stream_res.pix_clk_params.requested_sym_clk =
1659 			link_settings.link_rate * LINK_RATE_REF_FREQ_IN_KHZ;
1660 	if (state->clk_mgr && !apply_seamless_boot_optimization)
1661 		state->clk_mgr->funcs->update_clocks(state->clk_mgr,
1662 						     state, false);
1663 
1664 	// during mode switch we do DP_SET_POWER off then on, and OUI is lost
1665 	dpcd_set_source_specific_data(link);
1666 	if (link->dpcd_sink_ext_caps.raw != 0)
1667 		msleep(post_oui_delay);
1668 
1669 	skip_video_pattern = true;
1670 
1671 	if (link_settings.link_rate == LINK_RATE_LOW)
1672 		skip_video_pattern = false;
1673 
1674 	if (perform_link_training_with_retries(&link_settings,
1675 					       skip_video_pattern,
1676 					       LINK_TRAINING_ATTEMPTS,
1677 					       pipe_ctx,
1678 					       pipe_ctx->stream->signal)) {
1679 		link->cur_link_settings = link_settings;
1680 		status = DC_OK;
1681 	} else {
1682 		status = DC_FAIL_DP_LINK_TRAINING;
1683 	}
1684 
1685 	if (link->preferred_training_settings.fec_enable)
1686 		fec_enable = *link->preferred_training_settings.fec_enable;
1687 	else
1688 		fec_enable = true;
1689 
1690 	dp_set_fec_enable(link, fec_enable);
1691 
1692 	// during mode set we do DP_SET_POWER off then on, aux writes are lost
1693 	if (link->dpcd_sink_ext_caps.bits.oled == 1 ||
1694 		link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1 ||
1695 		link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1) {
1696 		dc_link_set_default_brightness_aux(link); // TODO: use cached if known
1697 		if (link->dpcd_sink_ext_caps.bits.oled == 1)
1698 			msleep(bl_oled_enable_delay);
1699 		dc_link_backlight_enable_aux(link, true);
1700 	}
1701 
1702 	return status;
1703 }
1704 
enable_link_edp(struct dc_state * state,struct pipe_ctx * pipe_ctx)1705 static enum dc_status enable_link_edp(
1706 		struct dc_state *state,
1707 		struct pipe_ctx *pipe_ctx)
1708 {
1709 	enum dc_status status;
1710 
1711 	status = enable_link_dp(state, pipe_ctx);
1712 
1713 	return status;
1714 }
1715 
enable_link_dp_mst(struct dc_state * state,struct pipe_ctx * pipe_ctx)1716 static enum dc_status enable_link_dp_mst(
1717 		struct dc_state *state,
1718 		struct pipe_ctx *pipe_ctx)
1719 {
1720 	struct dc_link *link = pipe_ctx->stream->link;
1721 
1722 	/* sink signal type after MST branch is MST. Multiple MST sinks
1723 	 * share one link. Link DP PHY is enable or training only once.
1724 	 */
1725 	if (link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN)
1726 		return DC_OK;
1727 
1728 	/* clear payload table */
1729 	dm_helpers_dp_mst_clear_payload_allocation_table(link->ctx, link);
1730 
1731 	/* to make sure the pending down rep can be processed
1732 	 * before enabling the link
1733 	 */
1734 	dm_helpers_dp_mst_poll_pending_down_reply(link->ctx, link);
1735 
1736 	/* set the sink to MST mode before enabling the link */
1737 	dp_enable_mst_on_sink(link, true);
1738 
1739 	return enable_link_dp(state, pipe_ctx);
1740 }
1741 
get_ext_hdmi_settings(struct pipe_ctx * pipe_ctx,enum engine_id eng_id,struct ext_hdmi_settings * settings)1742 static bool get_ext_hdmi_settings(struct pipe_ctx *pipe_ctx,
1743 		enum engine_id eng_id,
1744 		struct ext_hdmi_settings *settings)
1745 {
1746 	bool result = false;
1747 	int i = 0;
1748 	struct integrated_info *integrated_info =
1749 			pipe_ctx->stream->ctx->dc_bios->integrated_info;
1750 
1751 	if (integrated_info == NULL)
1752 		return false;
1753 
1754 	/*
1755 	 * Get retimer settings from sbios for passing SI eye test for DCE11
1756 	 * The setting values are varied based on board revision and port id
1757 	 * Therefore the setting values of each ports is passed by sbios.
1758 	 */
1759 
1760 	// Check if current bios contains ext Hdmi settings
1761 	if (integrated_info->gpu_cap_info & 0x20) {
1762 		switch (eng_id) {
1763 		case ENGINE_ID_DIGA:
1764 			settings->slv_addr = integrated_info->dp0_ext_hdmi_slv_addr;
1765 			settings->reg_num = integrated_info->dp0_ext_hdmi_6g_reg_num;
1766 			settings->reg_num_6g = integrated_info->dp0_ext_hdmi_6g_reg_num;
1767 			memmove(settings->reg_settings,
1768 					integrated_info->dp0_ext_hdmi_reg_settings,
1769 					sizeof(integrated_info->dp0_ext_hdmi_reg_settings));
1770 			memmove(settings->reg_settings_6g,
1771 					integrated_info->dp0_ext_hdmi_6g_reg_settings,
1772 					sizeof(integrated_info->dp0_ext_hdmi_6g_reg_settings));
1773 			result = true;
1774 			break;
1775 		case ENGINE_ID_DIGB:
1776 			settings->slv_addr = integrated_info->dp1_ext_hdmi_slv_addr;
1777 			settings->reg_num = integrated_info->dp1_ext_hdmi_6g_reg_num;
1778 			settings->reg_num_6g = integrated_info->dp1_ext_hdmi_6g_reg_num;
1779 			memmove(settings->reg_settings,
1780 					integrated_info->dp1_ext_hdmi_reg_settings,
1781 					sizeof(integrated_info->dp1_ext_hdmi_reg_settings));
1782 			memmove(settings->reg_settings_6g,
1783 					integrated_info->dp1_ext_hdmi_6g_reg_settings,
1784 					sizeof(integrated_info->dp1_ext_hdmi_6g_reg_settings));
1785 			result = true;
1786 			break;
1787 		case ENGINE_ID_DIGC:
1788 			settings->slv_addr = integrated_info->dp2_ext_hdmi_slv_addr;
1789 			settings->reg_num = integrated_info->dp2_ext_hdmi_6g_reg_num;
1790 			settings->reg_num_6g = integrated_info->dp2_ext_hdmi_6g_reg_num;
1791 			memmove(settings->reg_settings,
1792 					integrated_info->dp2_ext_hdmi_reg_settings,
1793 					sizeof(integrated_info->dp2_ext_hdmi_reg_settings));
1794 			memmove(settings->reg_settings_6g,
1795 					integrated_info->dp2_ext_hdmi_6g_reg_settings,
1796 					sizeof(integrated_info->dp2_ext_hdmi_6g_reg_settings));
1797 			result = true;
1798 			break;
1799 		case ENGINE_ID_DIGD:
1800 			settings->slv_addr = integrated_info->dp3_ext_hdmi_slv_addr;
1801 			settings->reg_num = integrated_info->dp3_ext_hdmi_6g_reg_num;
1802 			settings->reg_num_6g = integrated_info->dp3_ext_hdmi_6g_reg_num;
1803 			memmove(settings->reg_settings,
1804 					integrated_info->dp3_ext_hdmi_reg_settings,
1805 					sizeof(integrated_info->dp3_ext_hdmi_reg_settings));
1806 			memmove(settings->reg_settings_6g,
1807 					integrated_info->dp3_ext_hdmi_6g_reg_settings,
1808 					sizeof(integrated_info->dp3_ext_hdmi_6g_reg_settings));
1809 			result = true;
1810 			break;
1811 		default:
1812 			break;
1813 		}
1814 
1815 		if (result == true) {
1816 			// Validate settings from bios integrated info table
1817 			if (settings->slv_addr == 0)
1818 				return false;
1819 			if (settings->reg_num > 9)
1820 				return false;
1821 			if (settings->reg_num_6g > 3)
1822 				return false;
1823 
1824 			for (i = 0; i < settings->reg_num; i++) {
1825 				if (settings->reg_settings[i].i2c_reg_index > 0x20)
1826 					return false;
1827 			}
1828 
1829 			for (i = 0; i < settings->reg_num_6g; i++) {
1830 				if (settings->reg_settings_6g[i].i2c_reg_index > 0x20)
1831 					return false;
1832 			}
1833 		}
1834 	}
1835 
1836 	return result;
1837 }
1838 
i2c_write(struct pipe_ctx * pipe_ctx,uint8_t address,uint8_t * buffer,uint32_t length)1839 static bool i2c_write(struct pipe_ctx *pipe_ctx,
1840 		uint8_t address, uint8_t *buffer, uint32_t length)
1841 {
1842 	struct i2c_command cmd = {0};
1843 	struct i2c_payload payload = {0};
1844 
1845 	memset(&payload, 0, sizeof(payload));
1846 	memset(&cmd, 0, sizeof(cmd));
1847 
1848 	cmd.number_of_payloads = 1;
1849 	cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
1850 	cmd.speed = pipe_ctx->stream->ctx->dc->caps.i2c_speed_in_khz;
1851 
1852 	payload.address = address;
1853 	payload.data = buffer;
1854 	payload.length = length;
1855 	payload.write = true;
1856 	cmd.payloads = &payload;
1857 
1858 	if (dm_helpers_submit_i2c(pipe_ctx->stream->ctx,
1859 			pipe_ctx->stream->link, &cmd))
1860 		return true;
1861 
1862 	return false;
1863 }
1864 
write_i2c_retimer_setting(struct pipe_ctx * pipe_ctx,bool is_vga_mode,bool is_over_340mhz,struct ext_hdmi_settings * settings)1865 static void write_i2c_retimer_setting(
1866 		struct pipe_ctx *pipe_ctx,
1867 		bool is_vga_mode,
1868 		bool is_over_340mhz,
1869 		struct ext_hdmi_settings *settings)
1870 {
1871 	uint8_t slave_address = (settings->slv_addr >> 1);
1872 	uint8_t buffer[2];
1873 	const uint8_t apply_rx_tx_change = 0x4;
1874 	uint8_t offset = 0xA;
1875 	uint8_t value = 0;
1876 	int i = 0;
1877 	bool i2c_success = false;
1878 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
1879 
1880 	memset(&buffer, 0, sizeof(buffer));
1881 
1882 	/* Start Ext-Hdmi programming*/
1883 
1884 	for (i = 0; i < settings->reg_num; i++) {
1885 		/* Apply 3G settings */
1886 		if (settings->reg_settings[i].i2c_reg_index <= 0x20) {
1887 
1888 			buffer[0] = settings->reg_settings[i].i2c_reg_index;
1889 			buffer[1] = settings->reg_settings[i].i2c_reg_val;
1890 			i2c_success = i2c_write(pipe_ctx, slave_address,
1891 						buffer, sizeof(buffer));
1892 			RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1893 				offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
1894 				slave_address, buffer[0], buffer[1], i2c_success?1:0);
1895 
1896 			if (!i2c_success)
1897 				goto i2c_write_fail;
1898 
1899 			/* Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
1900 			 * needs to be set to 1 on every 0xA-0xC write.
1901 			 */
1902 			if (settings->reg_settings[i].i2c_reg_index == 0xA ||
1903 				settings->reg_settings[i].i2c_reg_index == 0xB ||
1904 				settings->reg_settings[i].i2c_reg_index == 0xC) {
1905 
1906 				/* Query current value from offset 0xA */
1907 				if (settings->reg_settings[i].i2c_reg_index == 0xA)
1908 					value = settings->reg_settings[i].i2c_reg_val;
1909 				else {
1910 					i2c_success =
1911 						dal_ddc_service_query_ddc_data(
1912 						pipe_ctx->stream->link->ddc,
1913 						slave_address, &offset, 1, &value, 1);
1914 					if (!i2c_success)
1915 						goto i2c_write_fail;
1916 				}
1917 
1918 				buffer[0] = offset;
1919 				/* Set APPLY_RX_TX_CHANGE bit to 1 */
1920 				buffer[1] = value | apply_rx_tx_change;
1921 				i2c_success = i2c_write(pipe_ctx, slave_address,
1922 						buffer, sizeof(buffer));
1923 				RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1924 					offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1925 					slave_address, buffer[0], buffer[1], i2c_success?1:0);
1926 				if (!i2c_success)
1927 					goto i2c_write_fail;
1928 			}
1929 		}
1930 	}
1931 
1932 	/* Apply 3G settings */
1933 	if (is_over_340mhz) {
1934 		for (i = 0; i < settings->reg_num_6g; i++) {
1935 			/* Apply 3G settings */
1936 			if (settings->reg_settings[i].i2c_reg_index <= 0x20) {
1937 
1938 				buffer[0] = settings->reg_settings_6g[i].i2c_reg_index;
1939 				buffer[1] = settings->reg_settings_6g[i].i2c_reg_val;
1940 				i2c_success = i2c_write(pipe_ctx, slave_address,
1941 							buffer, sizeof(buffer));
1942 				RETIMER_REDRIVER_INFO("above 340Mhz: retimer write to slave_address = 0x%x,\
1943 					offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1944 					slave_address, buffer[0], buffer[1], i2c_success?1:0);
1945 
1946 				if (!i2c_success)
1947 					goto i2c_write_fail;
1948 
1949 				/* Based on DP159 specs, APPLY_RX_TX_CHANGE bit in 0x0A
1950 				 * needs to be set to 1 on every 0xA-0xC write.
1951 				 */
1952 				if (settings->reg_settings_6g[i].i2c_reg_index == 0xA ||
1953 					settings->reg_settings_6g[i].i2c_reg_index == 0xB ||
1954 					settings->reg_settings_6g[i].i2c_reg_index == 0xC) {
1955 
1956 					/* Query current value from offset 0xA */
1957 					if (settings->reg_settings_6g[i].i2c_reg_index == 0xA)
1958 						value = settings->reg_settings_6g[i].i2c_reg_val;
1959 					else {
1960 						i2c_success =
1961 								dal_ddc_service_query_ddc_data(
1962 								pipe_ctx->stream->link->ddc,
1963 								slave_address, &offset, 1, &value, 1);
1964 						if (!i2c_success)
1965 							goto i2c_write_fail;
1966 					}
1967 
1968 					buffer[0] = offset;
1969 					/* Set APPLY_RX_TX_CHANGE bit to 1 */
1970 					buffer[1] = value | apply_rx_tx_change;
1971 					i2c_success = i2c_write(pipe_ctx, slave_address,
1972 							buffer, sizeof(buffer));
1973 					RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1974 						offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1975 						slave_address, buffer[0], buffer[1], i2c_success?1:0);
1976 					if (!i2c_success)
1977 						goto i2c_write_fail;
1978 				}
1979 			}
1980 		}
1981 	}
1982 
1983 	if (is_vga_mode) {
1984 		/* Program additional settings if using 640x480 resolution */
1985 
1986 		/* Write offset 0xFF to 0x01 */
1987 		buffer[0] = 0xff;
1988 		buffer[1] = 0x01;
1989 		i2c_success = i2c_write(pipe_ctx, slave_address,
1990 				buffer, sizeof(buffer));
1991 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
1992 				offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
1993 				slave_address, buffer[0], buffer[1], i2c_success?1:0);
1994 		if (!i2c_success)
1995 			goto i2c_write_fail;
1996 
1997 		/* Write offset 0x00 to 0x23 */
1998 		buffer[0] = 0x00;
1999 		buffer[1] = 0x23;
2000 		i2c_success = i2c_write(pipe_ctx, slave_address,
2001 				buffer, sizeof(buffer));
2002 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2003 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2004 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2005 		if (!i2c_success)
2006 			goto i2c_write_fail;
2007 
2008 		/* Write offset 0xff to 0x00 */
2009 		buffer[0] = 0xff;
2010 		buffer[1] = 0x00;
2011 		i2c_success = i2c_write(pipe_ctx, slave_address,
2012 				buffer, sizeof(buffer));
2013 		RETIMER_REDRIVER_INFO("retimer write to slave_address = 0x%x,\
2014 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2015 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2016 		if (!i2c_success)
2017 			goto i2c_write_fail;
2018 
2019 	}
2020 
2021 	return;
2022 
2023 i2c_write_fail:
2024 	DC_LOG_DEBUG("Set retimer failed");
2025 }
2026 
write_i2c_default_retimer_setting(struct pipe_ctx * pipe_ctx,bool is_vga_mode,bool is_over_340mhz)2027 static void write_i2c_default_retimer_setting(
2028 		struct pipe_ctx *pipe_ctx,
2029 		bool is_vga_mode,
2030 		bool is_over_340mhz)
2031 {
2032 	uint8_t slave_address = (0xBA >> 1);
2033 	uint8_t buffer[2];
2034 	bool i2c_success = false;
2035 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2036 
2037 	memset(&buffer, 0, sizeof(buffer));
2038 
2039 	/* Program Slave Address for tuning single integrity */
2040 	/* Write offset 0x0A to 0x13 */
2041 	buffer[0] = 0x0A;
2042 	buffer[1] = 0x13;
2043 	i2c_success = i2c_write(pipe_ctx, slave_address,
2044 			buffer, sizeof(buffer));
2045 	RETIMER_REDRIVER_INFO("retimer writes default setting to slave_address = 0x%x,\
2046 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2047 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2048 	if (!i2c_success)
2049 		goto i2c_write_fail;
2050 
2051 	/* Write offset 0x0A to 0x17 */
2052 	buffer[0] = 0x0A;
2053 	buffer[1] = 0x17;
2054 	i2c_success = i2c_write(pipe_ctx, slave_address,
2055 			buffer, sizeof(buffer));
2056 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2057 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2058 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2059 	if (!i2c_success)
2060 		goto i2c_write_fail;
2061 
2062 	/* Write offset 0x0B to 0xDA or 0xD8 */
2063 	buffer[0] = 0x0B;
2064 	buffer[1] = is_over_340mhz ? 0xDA : 0xD8;
2065 	i2c_success = i2c_write(pipe_ctx, slave_address,
2066 			buffer, sizeof(buffer));
2067 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2068 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2069 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2070 	if (!i2c_success)
2071 		goto i2c_write_fail;
2072 
2073 	/* Write offset 0x0A to 0x17 */
2074 	buffer[0] = 0x0A;
2075 	buffer[1] = 0x17;
2076 	i2c_success = i2c_write(pipe_ctx, slave_address,
2077 			buffer, sizeof(buffer));
2078 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2079 		offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
2080 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2081 	if (!i2c_success)
2082 		goto i2c_write_fail;
2083 
2084 	/* Write offset 0x0C to 0x1D or 0x91 */
2085 	buffer[0] = 0x0C;
2086 	buffer[1] = is_over_340mhz ? 0x1D : 0x91;
2087 	i2c_success = i2c_write(pipe_ctx, slave_address,
2088 			buffer, sizeof(buffer));
2089 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2090 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2091 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2092 	if (!i2c_success)
2093 		goto i2c_write_fail;
2094 
2095 	/* Write offset 0x0A to 0x17 */
2096 	buffer[0] = 0x0A;
2097 	buffer[1] = 0x17;
2098 	i2c_success = i2c_write(pipe_ctx, slave_address,
2099 			buffer, sizeof(buffer));
2100 	RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2101 		offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2102 		slave_address, buffer[0], buffer[1], i2c_success?1:0);
2103 	if (!i2c_success)
2104 		goto i2c_write_fail;
2105 
2106 
2107 	if (is_vga_mode) {
2108 		/* Program additional settings if using 640x480 resolution */
2109 
2110 		/* Write offset 0xFF to 0x01 */
2111 		buffer[0] = 0xff;
2112 		buffer[1] = 0x01;
2113 		i2c_success = i2c_write(pipe_ctx, slave_address,
2114 				buffer, sizeof(buffer));
2115 		RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2116 			offset = 0x%x, reg_val = 0x%x, i2c_success = %d\n",
2117 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2118 		if (!i2c_success)
2119 			goto i2c_write_fail;
2120 
2121 		/* Write offset 0x00 to 0x23 */
2122 		buffer[0] = 0x00;
2123 		buffer[1] = 0x23;
2124 		i2c_success = i2c_write(pipe_ctx, slave_address,
2125 				buffer, sizeof(buffer));
2126 		RETIMER_REDRIVER_INFO("retimer write to slave_addr = 0x%x,\
2127 			offset = 0x%x, reg_val= 0x%x, i2c_success = %d\n",
2128 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2129 		if (!i2c_success)
2130 			goto i2c_write_fail;
2131 
2132 		/* Write offset 0xff to 0x00 */
2133 		buffer[0] = 0xff;
2134 		buffer[1] = 0x00;
2135 		i2c_success = i2c_write(pipe_ctx, slave_address,
2136 				buffer, sizeof(buffer));
2137 		RETIMER_REDRIVER_INFO("retimer write default setting to slave_addr = 0x%x,\
2138 			offset = 0x%x, reg_val= 0x%x, i2c_success = %d end here\n",
2139 			slave_address, buffer[0], buffer[1], i2c_success?1:0);
2140 		if (!i2c_success)
2141 			goto i2c_write_fail;
2142 	}
2143 
2144 	return;
2145 
2146 i2c_write_fail:
2147 	DC_LOG_DEBUG("Set default retimer failed");
2148 }
2149 
write_i2c_redriver_setting(struct pipe_ctx * pipe_ctx,bool is_over_340mhz)2150 static void write_i2c_redriver_setting(
2151 		struct pipe_ctx *pipe_ctx,
2152 		bool is_over_340mhz)
2153 {
2154 	uint8_t slave_address = (0xF0 >> 1);
2155 	uint8_t buffer[16];
2156 	bool i2c_success = false;
2157 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
2158 
2159 	memset(&buffer, 0, sizeof(buffer));
2160 
2161 	// Program Slave Address for tuning single integrity
2162 	buffer[3] = 0x4E;
2163 	buffer[4] = 0x4E;
2164 	buffer[5] = 0x4E;
2165 	buffer[6] = is_over_340mhz ? 0x4E : 0x4A;
2166 
2167 	i2c_success = i2c_write(pipe_ctx, slave_address,
2168 					buffer, sizeof(buffer));
2169 	RETIMER_REDRIVER_INFO("redriver write 0 to all 16 reg offset expect following:\n\
2170 		\t slave_addr = 0x%x, offset[3] = 0x%x, offset[4] = 0x%x,\
2171 		offset[5] = 0x%x,offset[6] is_over_340mhz = 0x%x,\
2172 		i2c_success = %d\n",
2173 		slave_address, buffer[3], buffer[4], buffer[5], buffer[6], i2c_success?1:0);
2174 
2175 	if (!i2c_success)
2176 		DC_LOG_DEBUG("Set redriver failed");
2177 }
2178 
disable_link(struct dc_link * link,enum signal_type signal)2179 static void disable_link(struct dc_link *link, enum signal_type signal)
2180 {
2181 	/*
2182 	 * TODO: implement call for dp_set_hw_test_pattern
2183 	 * it is needed for compliance testing
2184 	 */
2185 
2186 	/* Here we need to specify that encoder output settings
2187 	 * need to be calculated as for the set mode,
2188 	 * it will lead to querying dynamic link capabilities
2189 	 * which should be done before enable output
2190 	 */
2191 
2192 	if (dc_is_dp_signal(signal)) {
2193 		/* SST DP, eDP */
2194 		if (dc_is_dp_sst_signal(signal))
2195 			dp_disable_link_phy(link, signal);
2196 		else
2197 			dp_disable_link_phy_mst(link, signal);
2198 
2199 		if (dc_is_dp_sst_signal(signal) ||
2200 				link->mst_stream_alloc_table.stream_count == 0) {
2201 			dp_set_fec_enable(link, false);
2202 			dp_set_fec_ready(link, false);
2203 		}
2204 	} else {
2205 		if (signal != SIGNAL_TYPE_VIRTUAL)
2206 			link->link_enc->funcs->disable_output(link->link_enc, signal);
2207 	}
2208 
2209 	if (signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2210 		/* MST disable link only when no stream use the link */
2211 		if (link->mst_stream_alloc_table.stream_count <= 0)
2212 			link->link_status.link_active = false;
2213 	} else {
2214 		link->link_status.link_active = false;
2215 	}
2216 }
2217 
enable_link_hdmi(struct pipe_ctx * pipe_ctx)2218 static void enable_link_hdmi(struct pipe_ctx *pipe_ctx)
2219 {
2220 	struct dc_stream_state *stream = pipe_ctx->stream;
2221 	struct dc_link *link = stream->link;
2222 	enum dc_color_depth display_color_depth;
2223 	enum engine_id eng_id;
2224 	struct ext_hdmi_settings settings = {0};
2225 	bool is_over_340mhz = false;
2226 	bool is_vga_mode = (stream->timing.h_addressable == 640)
2227 			&& (stream->timing.v_addressable == 480);
2228 
2229 	if (stream->phy_pix_clk == 0)
2230 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2231 	if (stream->phy_pix_clk > 340000)
2232 		is_over_340mhz = true;
2233 
2234 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
2235 		unsigned short masked_chip_caps = pipe_ctx->stream->link->chip_caps &
2236 				EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
2237 		if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
2238 			/* DP159, Retimer settings */
2239 			eng_id = pipe_ctx->stream_res.stream_enc->id;
2240 
2241 			if (get_ext_hdmi_settings(pipe_ctx, eng_id, &settings)) {
2242 				write_i2c_retimer_setting(pipe_ctx,
2243 						is_vga_mode, is_over_340mhz, &settings);
2244 			} else {
2245 				write_i2c_default_retimer_setting(pipe_ctx,
2246 						is_vga_mode, is_over_340mhz);
2247 			}
2248 		} else if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
2249 			/* PI3EQX1204, Redriver settings */
2250 			write_i2c_redriver_setting(pipe_ctx, is_over_340mhz);
2251 		}
2252 	}
2253 
2254 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
2255 		dal_ddc_service_write_scdc_data(
2256 			stream->link->ddc,
2257 			stream->phy_pix_clk,
2258 			stream->timing.flags.LTE_340MCSC_SCRAMBLE);
2259 
2260 	memset(&stream->link->cur_link_settings, 0,
2261 			sizeof(struct dc_link_settings));
2262 
2263 	display_color_depth = stream->timing.display_color_depth;
2264 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR422)
2265 		display_color_depth = COLOR_DEPTH_888;
2266 
2267 	link->link_enc->funcs->enable_tmds_output(
2268 			link->link_enc,
2269 			pipe_ctx->clock_source->id,
2270 			display_color_depth,
2271 			pipe_ctx->stream->signal,
2272 			stream->phy_pix_clk);
2273 
2274 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal))
2275 		dal_ddc_service_read_scdc_data(link->ddc);
2276 }
2277 
enable_link_lvds(struct pipe_ctx * pipe_ctx)2278 static void enable_link_lvds(struct pipe_ctx *pipe_ctx)
2279 {
2280 	struct dc_stream_state *stream = pipe_ctx->stream;
2281 	struct dc_link *link = stream->link;
2282 
2283 	if (stream->phy_pix_clk == 0)
2284 		stream->phy_pix_clk = stream->timing.pix_clk_100hz / 10;
2285 
2286 	memset(&stream->link->cur_link_settings, 0,
2287 			sizeof(struct dc_link_settings));
2288 
2289 	link->link_enc->funcs->enable_lvds_output(
2290 			link->link_enc,
2291 			pipe_ctx->clock_source->id,
2292 			stream->phy_pix_clk);
2293 
2294 }
2295 
2296 /****************************enable_link***********************************/
enable_link(struct dc_state * state,struct pipe_ctx * pipe_ctx)2297 static enum dc_status enable_link(
2298 		struct dc_state *state,
2299 		struct pipe_ctx *pipe_ctx)
2300 {
2301 	enum dc_status status = DC_ERROR_UNEXPECTED;
2302 	struct dc_stream_state *stream = pipe_ctx->stream;
2303 	struct dc_link *link = stream->link;
2304 
2305 	/* There's some scenarios where driver is unloaded with display
2306 	 * still enabled. When driver is reloaded, it may cause a display
2307 	 * to not light up if there is a mismatch between old and new
2308 	 * link settings. Need to call disable first before enabling at
2309 	 * new link settings.
2310 	 */
2311 	if (link->link_status.link_active) {
2312 		disable_link(link, pipe_ctx->stream->signal);
2313 	}
2314 
2315 	switch (pipe_ctx->stream->signal) {
2316 	case SIGNAL_TYPE_DISPLAY_PORT:
2317 		status = enable_link_dp(state, pipe_ctx);
2318 		break;
2319 	case SIGNAL_TYPE_EDP:
2320 		status = enable_link_edp(state, pipe_ctx);
2321 		break;
2322 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
2323 		status = enable_link_dp_mst(state, pipe_ctx);
2324 		msleep(200);
2325 		break;
2326 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
2327 	case SIGNAL_TYPE_DVI_DUAL_LINK:
2328 	case SIGNAL_TYPE_HDMI_TYPE_A:
2329 		enable_link_hdmi(pipe_ctx);
2330 		status = DC_OK;
2331 		break;
2332 	case SIGNAL_TYPE_LVDS:
2333 		enable_link_lvds(pipe_ctx);
2334 		status = DC_OK;
2335 		break;
2336 	case SIGNAL_TYPE_VIRTUAL:
2337 		status = DC_OK;
2338 		break;
2339 	default:
2340 		break;
2341 	}
2342 
2343 	if (status == DC_OK)
2344 		pipe_ctx->stream->link->link_status.link_active = true;
2345 
2346 	return status;
2347 }
2348 
get_timing_pixel_clock_100hz(const struct dc_crtc_timing * timing)2349 static uint32_t get_timing_pixel_clock_100hz(const struct dc_crtc_timing *timing)
2350 {
2351 
2352 	uint32_t pxl_clk = timing->pix_clk_100hz;
2353 
2354 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2355 		pxl_clk /= 2;
2356 	else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
2357 		pxl_clk = pxl_clk * 2 / 3;
2358 
2359 	if (timing->display_color_depth == COLOR_DEPTH_101010)
2360 		pxl_clk = pxl_clk * 10 / 8;
2361 	else if (timing->display_color_depth == COLOR_DEPTH_121212)
2362 		pxl_clk = pxl_clk * 12 / 8;
2363 
2364 	return pxl_clk;
2365 }
2366 
dp_active_dongle_validate_timing(const struct dc_crtc_timing * timing,const struct dpcd_caps * dpcd_caps)2367 static bool dp_active_dongle_validate_timing(
2368 		const struct dc_crtc_timing *timing,
2369 		const struct dpcd_caps *dpcd_caps)
2370 {
2371 	const struct dc_dongle_caps *dongle_caps = &dpcd_caps->dongle_caps;
2372 
2373 	switch (dpcd_caps->dongle_type) {
2374 	case DISPLAY_DONGLE_DP_VGA_CONVERTER:
2375 	case DISPLAY_DONGLE_DP_DVI_CONVERTER:
2376 	case DISPLAY_DONGLE_DP_DVI_DONGLE:
2377 		if (timing->pixel_encoding == PIXEL_ENCODING_RGB)
2378 			return true;
2379 		else
2380 			return false;
2381 	default:
2382 		break;
2383 	}
2384 
2385 	if (dpcd_caps->dongle_type != DISPLAY_DONGLE_DP_HDMI_CONVERTER ||
2386 		dongle_caps->extendedCapValid == false)
2387 		return true;
2388 
2389 	/* Check Pixel Encoding */
2390 	switch (timing->pixel_encoding) {
2391 	case PIXEL_ENCODING_RGB:
2392 	case PIXEL_ENCODING_YCBCR444:
2393 		break;
2394 	case PIXEL_ENCODING_YCBCR422:
2395 		if (!dongle_caps->is_dp_hdmi_ycbcr422_pass_through)
2396 			return false;
2397 		break;
2398 	case PIXEL_ENCODING_YCBCR420:
2399 		if (!dongle_caps->is_dp_hdmi_ycbcr420_pass_through)
2400 			return false;
2401 		break;
2402 	default:
2403 		/* Invalid Pixel Encoding*/
2404 		return false;
2405 	}
2406 
2407 	switch (timing->display_color_depth) {
2408 	case COLOR_DEPTH_666:
2409 	case COLOR_DEPTH_888:
2410 		/*888 and 666 should always be supported*/
2411 		break;
2412 	case COLOR_DEPTH_101010:
2413 		if (dongle_caps->dp_hdmi_max_bpc < 10)
2414 			return false;
2415 		break;
2416 	case COLOR_DEPTH_121212:
2417 		if (dongle_caps->dp_hdmi_max_bpc < 12)
2418 			return false;
2419 		break;
2420 	case COLOR_DEPTH_141414:
2421 	case COLOR_DEPTH_161616:
2422 	default:
2423 		/* These color depths are currently not supported */
2424 		return false;
2425 	}
2426 
2427 	if (get_timing_pixel_clock_100hz(timing) > (dongle_caps->dp_hdmi_max_pixel_clk_in_khz * 10))
2428 		return false;
2429 
2430 	return true;
2431 }
2432 
dc_link_validate_mode_timing(const struct dc_stream_state * stream,struct dc_link * link,const struct dc_crtc_timing * timing)2433 enum dc_status dc_link_validate_mode_timing(
2434 		const struct dc_stream_state *stream,
2435 		struct dc_link *link,
2436 		const struct dc_crtc_timing *timing)
2437 {
2438 	uint32_t max_pix_clk = stream->link->dongle_max_pix_clk * 10;
2439 	struct dpcd_caps *dpcd_caps = &link->dpcd_caps;
2440 
2441 	/* A hack to avoid failing any modes for EDID override feature on
2442 	 * topology change such as lower quality cable for DP or different dongle
2443 	 */
2444 	if (link->remote_sinks[0] && link->remote_sinks[0]->sink_signal == SIGNAL_TYPE_VIRTUAL)
2445 		return DC_OK;
2446 
2447 	/* Passive Dongle */
2448 	if (max_pix_clk != 0 && get_timing_pixel_clock_100hz(timing) > max_pix_clk)
2449 		return DC_EXCEED_DONGLE_CAP;
2450 
2451 	/* Active Dongle*/
2452 	if (!dp_active_dongle_validate_timing(timing, dpcd_caps))
2453 		return DC_EXCEED_DONGLE_CAP;
2454 
2455 	switch (stream->signal) {
2456 	case SIGNAL_TYPE_EDP:
2457 	case SIGNAL_TYPE_DISPLAY_PORT:
2458 		if (!dp_validate_mode_timing(
2459 				link,
2460 				timing))
2461 			return DC_NO_DP_LINK_BANDWIDTH;
2462 		break;
2463 
2464 	default:
2465 		break;
2466 	}
2467 
2468 	return DC_OK;
2469 }
2470 
get_abm_from_stream_res(const struct dc_link * link)2471 static struct abm *get_abm_from_stream_res(const struct dc_link *link)
2472 {
2473 	int i;
2474 	struct dc *dc = link->ctx->dc;
2475 	struct abm *abm = NULL;
2476 
2477 	for (i = 0; i < MAX_PIPES; i++) {
2478 		struct pipe_ctx pipe_ctx = dc->current_state->res_ctx.pipe_ctx[i];
2479 		struct dc_stream_state *stream = pipe_ctx.stream;
2480 
2481 		if (stream && stream->link == link) {
2482 			abm = pipe_ctx.stream_res.abm;
2483 			break;
2484 		}
2485 	}
2486 	return abm;
2487 }
2488 
dc_link_get_backlight_level(const struct dc_link * link)2489 int dc_link_get_backlight_level(const struct dc_link *link)
2490 {
2491 
2492 	struct abm *abm = get_abm_from_stream_res(link);
2493 
2494 	if (abm == NULL || abm->funcs->get_current_backlight == NULL)
2495 		return DC_ERROR_UNEXPECTED;
2496 
2497 	return (int) abm->funcs->get_current_backlight(abm);
2498 }
2499 
dc_link_get_target_backlight_pwm(const struct dc_link * link)2500 int dc_link_get_target_backlight_pwm(const struct dc_link *link)
2501 {
2502 	struct abm *abm = get_abm_from_stream_res(link);
2503 
2504 	if (abm == NULL || abm->funcs->get_target_backlight == NULL)
2505 		return DC_ERROR_UNEXPECTED;
2506 
2507 	return (int) abm->funcs->get_target_backlight(abm);
2508 }
2509 
get_pipe_from_link(const struct dc_link * link)2510 static struct pipe_ctx *get_pipe_from_link(const struct dc_link *link)
2511 {
2512 	int i;
2513 	struct dc *dc = link->ctx->dc;
2514 	struct pipe_ctx *pipe_ctx = NULL;
2515 
2516 	for (i = 0; i < MAX_PIPES; i++) {
2517 		if (dc->current_state->res_ctx.pipe_ctx[i].stream) {
2518 			if (dc->current_state->res_ctx.pipe_ctx[i].stream->link == link) {
2519 				pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
2520 				break;
2521 			}
2522 		}
2523 	}
2524 
2525 	return pipe_ctx;
2526 }
2527 
dc_link_set_backlight_level(const struct dc_link * link,uint32_t backlight_pwm_u16_16,uint32_t frame_ramp)2528 bool dc_link_set_backlight_level(const struct dc_link *link,
2529 		uint32_t backlight_pwm_u16_16,
2530 		uint32_t frame_ramp)
2531 {
2532 	struct dc  *dc = link->ctx->dc;
2533 
2534 	DC_LOGGER_INIT(link->ctx->logger);
2535 	DC_LOG_BACKLIGHT("New Backlight level: %d (0x%X)\n",
2536 			backlight_pwm_u16_16, backlight_pwm_u16_16);
2537 
2538 	if (dc_is_embedded_signal(link->connector_signal)) {
2539 		struct pipe_ctx *pipe_ctx = get_pipe_from_link(link);
2540 
2541 		if (pipe_ctx) {
2542 			/* Disable brightness ramping when the display is blanked
2543 			 * as it can hang the DMCU
2544 			 */
2545 			if (pipe_ctx->plane_state == NULL)
2546 				frame_ramp = 0;
2547 		} else {
2548 			ASSERT(false);
2549 			return false;
2550 		}
2551 
2552 		dc->hwss.set_backlight_level(
2553 				pipe_ctx,
2554 				backlight_pwm_u16_16,
2555 				frame_ramp);
2556 	}
2557 	return true;
2558 }
2559 
dc_link_set_psr_allow_active(struct dc_link * link,bool allow_active,bool wait)2560 bool dc_link_set_psr_allow_active(struct dc_link *link, bool allow_active, bool wait)
2561 {
2562 	struct dc  *dc = link->ctx->dc;
2563 	struct dmcu *dmcu = dc->res_pool->dmcu;
2564 	struct dmub_psr *psr = dc->res_pool->psr;
2565 
2566 	link->psr_settings.psr_allow_active = allow_active;
2567 
2568 	if (psr != NULL && link->psr_settings.psr_feature_enabled)
2569 		psr->funcs->psr_enable(psr, allow_active, wait);
2570 	else if ((dmcu != NULL && dmcu->funcs->is_dmcu_initialized(dmcu)) && link->psr_settings.psr_feature_enabled)
2571 		dmcu->funcs->set_psr_enable(dmcu, allow_active, wait);
2572 	else
2573 		return false;
2574 
2575 	return true;
2576 }
2577 
dc_link_get_psr_state(const struct dc_link * link,uint32_t * psr_state)2578 bool dc_link_get_psr_state(const struct dc_link *link, uint32_t *psr_state)
2579 {
2580 	struct dc  *dc = link->ctx->dc;
2581 	struct dmcu *dmcu = dc->res_pool->dmcu;
2582 	struct dmub_psr *psr = dc->res_pool->psr;
2583 
2584 	if (psr != NULL && link->psr_settings.psr_feature_enabled)
2585 		psr->funcs->psr_get_state(psr, psr_state);
2586 	else if (dmcu != NULL && link->psr_settings.psr_feature_enabled)
2587 		dmcu->funcs->get_psr_state(dmcu, psr_state);
2588 
2589 	return true;
2590 }
2591 
2592 static inline enum physical_phy_id
transmitter_to_phy_id(enum transmitter transmitter_value)2593 transmitter_to_phy_id(enum transmitter transmitter_value)
2594 {
2595 	switch (transmitter_value) {
2596 	case TRANSMITTER_UNIPHY_A:
2597 		return PHYLD_0;
2598 	case TRANSMITTER_UNIPHY_B:
2599 		return PHYLD_1;
2600 	case TRANSMITTER_UNIPHY_C:
2601 		return PHYLD_2;
2602 	case TRANSMITTER_UNIPHY_D:
2603 		return PHYLD_3;
2604 	case TRANSMITTER_UNIPHY_E:
2605 		return PHYLD_4;
2606 	case TRANSMITTER_UNIPHY_F:
2607 		return PHYLD_5;
2608 	case TRANSMITTER_NUTMEG_CRT:
2609 		return PHYLD_6;
2610 	case TRANSMITTER_TRAVIS_CRT:
2611 		return PHYLD_7;
2612 	case TRANSMITTER_TRAVIS_LCD:
2613 		return PHYLD_8;
2614 	case TRANSMITTER_UNIPHY_G:
2615 		return PHYLD_9;
2616 	case TRANSMITTER_COUNT:
2617 		return PHYLD_COUNT;
2618 	case TRANSMITTER_UNKNOWN:
2619 		return PHYLD_UNKNOWN;
2620 	default:
2621 		WARN_ONCE(1, "Unknown transmitter value %d\n",
2622 			  transmitter_value);
2623 		return PHYLD_UNKNOWN;
2624 	}
2625 }
2626 
dc_link_setup_psr(struct dc_link * link,const struct dc_stream_state * stream,struct psr_config * psr_config,struct psr_context * psr_context)2627 bool dc_link_setup_psr(struct dc_link *link,
2628 		const struct dc_stream_state *stream, struct psr_config *psr_config,
2629 		struct psr_context *psr_context)
2630 {
2631 	struct dc *dc;
2632 	struct dmcu *dmcu;
2633 	struct dmub_psr *psr;
2634 	int i;
2635 	/* updateSinkPsrDpcdConfig*/
2636 	union dpcd_psr_configuration psr_configuration;
2637 
2638 	psr_context->controllerId = CONTROLLER_ID_UNDEFINED;
2639 
2640 	if (!link)
2641 		return false;
2642 
2643 	dc = link->ctx->dc;
2644 	dmcu = dc->res_pool->dmcu;
2645 	psr = dc->res_pool->psr;
2646 
2647 	if (!dmcu && !psr)
2648 		return false;
2649 
2650 
2651 	memset(&psr_configuration, 0, sizeof(psr_configuration));
2652 
2653 	psr_configuration.bits.ENABLE                    = 1;
2654 	psr_configuration.bits.CRC_VERIFICATION          = 1;
2655 	psr_configuration.bits.FRAME_CAPTURE_INDICATION  =
2656 			psr_config->psr_frame_capture_indication_req;
2657 
2658 	/* Check for PSR v2*/
2659 	if (psr_config->psr_version == 0x2) {
2660 		/* For PSR v2 selective update.
2661 		 * Indicates whether sink should start capturing
2662 		 * immediately following active scan line,
2663 		 * or starting with the 2nd active scan line.
2664 		 */
2665 		psr_configuration.bits.LINE_CAPTURE_INDICATION = 0;
2666 		/*For PSR v2, determines whether Sink should generate
2667 		 * IRQ_HPD when CRC mismatch is detected.
2668 		 */
2669 		psr_configuration.bits.IRQ_HPD_WITH_CRC_ERROR    = 1;
2670 	}
2671 
2672 	dm_helpers_dp_write_dpcd(
2673 		link->ctx,
2674 		link,
2675 		368,
2676 		&psr_configuration.raw,
2677 		sizeof(psr_configuration.raw));
2678 
2679 	psr_context->channel = link->ddc->ddc_pin->hw_info.ddc_channel;
2680 	psr_context->transmitterId = link->link_enc->transmitter;
2681 	psr_context->engineId = link->link_enc->preferred_engine;
2682 
2683 	for (i = 0; i < MAX_PIPES; i++) {
2684 		if (dc->current_state->res_ctx.pipe_ctx[i].stream
2685 				== stream) {
2686 			/* dmcu -1 for all controller id values,
2687 			 * therefore +1 here
2688 			 */
2689 			psr_context->controllerId =
2690 				dc->current_state->res_ctx.
2691 				pipe_ctx[i].stream_res.tg->inst + 1;
2692 			break;
2693 		}
2694 	}
2695 
2696 	/* Hardcoded for now.  Can be Pcie or Uniphy (or Unknown)*/
2697 	psr_context->phyType = PHY_TYPE_UNIPHY;
2698 	/*PhyId is associated with the transmitter id*/
2699 	psr_context->smuPhyId =
2700 		transmitter_to_phy_id(link->link_enc->transmitter);
2701 
2702 	psr_context->crtcTimingVerticalTotal = stream->timing.v_total;
2703 	psr_context->vsync_rate_hz = div64_u64(div64_u64((stream->
2704 					timing.pix_clk_100hz * 100),
2705 					stream->timing.v_total),
2706 					stream->timing.h_total);
2707 
2708 	psr_context->psrSupportedDisplayConfig = true;
2709 	psr_context->psrExitLinkTrainingRequired =
2710 		psr_config->psr_exit_link_training_required;
2711 	psr_context->sdpTransmitLineNumDeadline =
2712 		psr_config->psr_sdp_transmit_line_num_deadline;
2713 	psr_context->psrFrameCaptureIndicationReq =
2714 		psr_config->psr_frame_capture_indication_req;
2715 
2716 	psr_context->skipPsrWaitForPllLock = 0; /* only = 1 in KV */
2717 
2718 	psr_context->numberOfControllers =
2719 			link->dc->res_pool->timing_generator_count;
2720 
2721 	psr_context->rfb_update_auto_en = true;
2722 
2723 	/* 2 frames before enter PSR. */
2724 	psr_context->timehyst_frames = 2;
2725 	/* half a frame
2726 	 * (units in 100 lines, i.e. a value of 1 represents 100 lines)
2727 	 */
2728 	psr_context->hyst_lines = stream->timing.v_total / 2 / 100;
2729 	psr_context->aux_repeats = 10;
2730 
2731 	psr_context->psr_level.u32all = 0;
2732 
2733 #if defined(CONFIG_DRM_AMD_DC_DCN)
2734 	/*skip power down the single pipe since it blocks the cstate*/
2735 	if (ASICREV_IS_RAVEN(link->ctx->asic_id.hw_internal_rev))
2736 		psr_context->psr_level.bits.SKIP_CRTC_DISABLE = true;
2737 #endif
2738 
2739 	/* SMU will perform additional powerdown sequence.
2740 	 * For unsupported ASICs, set psr_level flag to skip PSR
2741 	 *  static screen notification to SMU.
2742 	 *  (Always set for DAL2, did not check ASIC)
2743 	 */
2744 	psr_context->allow_smu_optimizations = psr_config->allow_smu_optimizations;
2745 
2746 	/* Complete PSR entry before aborting to prevent intermittent
2747 	 * freezes on certain eDPs
2748 	 */
2749 	psr_context->psr_level.bits.DISABLE_PSR_ENTRY_ABORT = 1;
2750 
2751 	/* Controls additional delay after remote frame capture before
2752 	 * continuing power down, default = 0
2753 	 */
2754 	psr_context->frame_delay = 0;
2755 
2756 	if (psr)
2757 		link->psr_settings.psr_feature_enabled = psr->funcs->psr_copy_settings(psr, link, psr_context);
2758 	else
2759 		link->psr_settings.psr_feature_enabled = dmcu->funcs->setup_psr(dmcu, link, psr_context);
2760 
2761 	/* psr_enabled == 0 indicates setup_psr did not succeed, but this
2762 	 * should not happen since firmware should be running at this point
2763 	 */
2764 	if (link->psr_settings.psr_feature_enabled == 0)
2765 		ASSERT(0);
2766 
2767 	return true;
2768 
2769 }
2770 
dc_link_get_status(const struct dc_link * link)2771 const struct dc_link_status *dc_link_get_status(const struct dc_link *link)
2772 {
2773 	return &link->link_status;
2774 }
2775 
core_link_resume(struct dc_link * link)2776 void core_link_resume(struct dc_link *link)
2777 {
2778 	if (link->connector_signal != SIGNAL_TYPE_VIRTUAL)
2779 		program_hpd_filter(link);
2780 }
2781 
get_pbn_per_slot(struct dc_stream_state * stream)2782 static struct fixed31_32 get_pbn_per_slot(struct dc_stream_state *stream)
2783 {
2784 	struct fixed31_32 mbytes_per_sec;
2785 	uint32_t link_rate_in_mbytes_per_sec = dc_link_bandwidth_kbps(stream->link,
2786 			&stream->link->cur_link_settings);
2787 	link_rate_in_mbytes_per_sec /= 8000; /* Kbits to MBytes */
2788 
2789 	mbytes_per_sec = dc_fixpt_from_int(link_rate_in_mbytes_per_sec);
2790 
2791 	return dc_fixpt_div_int(mbytes_per_sec, 54);
2792 }
2793 
get_pbn_from_timing(struct pipe_ctx * pipe_ctx)2794 static struct fixed31_32 get_pbn_from_timing(struct pipe_ctx *pipe_ctx)
2795 {
2796 	uint64_t kbps;
2797 	struct fixed31_32 peak_kbps;
2798 	uint32_t numerator;
2799 	uint32_t denominator;
2800 
2801 	kbps = dc_bandwidth_in_kbps_from_timing(&pipe_ctx->stream->timing);
2802 
2803 	/*
2804 	 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2805 	 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2806 	 * common multiplier to render an integer PBN for all link rate/lane
2807 	 * counts combinations
2808 	 * calculate
2809 	 * peak_kbps *= (1006/1000)
2810 	 * peak_kbps *= (64/54)
2811 	 * peak_kbps *= 8    convert to bytes
2812 	 */
2813 
2814 	numerator = 64 * PEAK_FACTOR_X1000;
2815 	denominator = 54 * 8 * 1000 * 1000;
2816 	kbps *= numerator;
2817 	peak_kbps = dc_fixpt_from_fraction(kbps, denominator);
2818 
2819 	return peak_kbps;
2820 }
2821 
update_mst_stream_alloc_table(struct dc_link * link,struct stream_encoder * stream_enc,const struct dp_mst_stream_allocation_table * proposed_table)2822 static void update_mst_stream_alloc_table(
2823 	struct dc_link *link,
2824 	struct stream_encoder *stream_enc,
2825 	const struct dp_mst_stream_allocation_table *proposed_table)
2826 {
2827 	struct link_mst_stream_allocation work_table[MAX_CONTROLLER_NUM] = {
2828 			{ 0 } };
2829 	struct link_mst_stream_allocation *dc_alloc;
2830 
2831 	int i;
2832 	int j;
2833 
2834 	/* if DRM proposed_table has more than one new payload */
2835 	ASSERT(proposed_table->stream_count -
2836 			link->mst_stream_alloc_table.stream_count < 2);
2837 
2838 	/* copy proposed_table to link, add stream encoder */
2839 	for (i = 0; i < proposed_table->stream_count; i++) {
2840 
2841 		for (j = 0; j < link->mst_stream_alloc_table.stream_count; j++) {
2842 			dc_alloc =
2843 			&link->mst_stream_alloc_table.stream_allocations[j];
2844 
2845 			if (dc_alloc->vcp_id ==
2846 				proposed_table->stream_allocations[i].vcp_id) {
2847 
2848 				work_table[i] = *dc_alloc;
2849 				break; /* exit j loop */
2850 			}
2851 		}
2852 
2853 		/* new vcp_id */
2854 		if (j == link->mst_stream_alloc_table.stream_count) {
2855 			work_table[i].vcp_id =
2856 				proposed_table->stream_allocations[i].vcp_id;
2857 			work_table[i].slot_count =
2858 				proposed_table->stream_allocations[i].slot_count;
2859 			work_table[i].stream_enc = stream_enc;
2860 		}
2861 	}
2862 
2863 	/* update link->mst_stream_alloc_table with work_table */
2864 	link->mst_stream_alloc_table.stream_count =
2865 			proposed_table->stream_count;
2866 	for (i = 0; i < MAX_CONTROLLER_NUM; i++)
2867 		link->mst_stream_alloc_table.stream_allocations[i] =
2868 				work_table[i];
2869 }
2870 
2871 /* convert link_mst_stream_alloc_table to dm dp_mst_stream_alloc_table
2872  * because stream_encoder is not exposed to dm
2873  */
dc_link_allocate_mst_payload(struct pipe_ctx * pipe_ctx)2874 enum dc_status dc_link_allocate_mst_payload(struct pipe_ctx *pipe_ctx)
2875 {
2876 	struct dc_stream_state *stream = pipe_ctx->stream;
2877 	struct dc_link *link = stream->link;
2878 	struct link_encoder *link_encoder = link->link_enc;
2879 	struct stream_encoder *stream_encoder = pipe_ctx->stream_res.stream_enc;
2880 	struct dp_mst_stream_allocation_table proposed_table = {0};
2881 	struct fixed31_32 avg_time_slots_per_mtp;
2882 	struct fixed31_32 pbn;
2883 	struct fixed31_32 pbn_per_slot;
2884 	uint8_t i;
2885 	enum act_return_status ret;
2886 	DC_LOGGER_INIT(link->ctx->logger);
2887 
2888 	/* enable_link_dp_mst already check link->enabled_stream_count
2889 	 * and stream is in link->stream[]. This is called during set mode,
2890 	 * stream_enc is available.
2891 	 */
2892 
2893 	/* get calculate VC payload for stream: stream_alloc */
2894 	if (dm_helpers_dp_mst_write_payload_allocation_table(
2895 		stream->ctx,
2896 		stream,
2897 		&proposed_table,
2898 		true)) {
2899 		update_mst_stream_alloc_table(
2900 					link, pipe_ctx->stream_res.stream_enc, &proposed_table);
2901 	}
2902 	else
2903 		DC_LOG_WARNING("Failed to update"
2904 				"MST allocation table for"
2905 				"pipe idx:%d\n",
2906 				pipe_ctx->pipe_idx);
2907 
2908 	DC_LOG_MST("%s  "
2909 			"stream_count: %d: \n ",
2910 			__func__,
2911 			link->mst_stream_alloc_table.stream_count);
2912 
2913 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
2914 		DC_LOG_MST("stream_enc[%d]: %p      "
2915 		"stream[%d].vcp_id: %d      "
2916 		"stream[%d].slot_count: %d\n",
2917 		i,
2918 		(void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc,
2919 		i,
2920 		link->mst_stream_alloc_table.stream_allocations[i].vcp_id,
2921 		i,
2922 		link->mst_stream_alloc_table.stream_allocations[i].slot_count);
2923 	}
2924 
2925 	ASSERT(proposed_table.stream_count > 0);
2926 
2927 	/* program DP source TX for payload */
2928 	link_encoder->funcs->update_mst_stream_allocation_table(
2929 		link_encoder,
2930 		&link->mst_stream_alloc_table);
2931 
2932 	/* send down message */
2933 	ret = dm_helpers_dp_mst_poll_for_allocation_change_trigger(
2934 			stream->ctx,
2935 			stream);
2936 
2937 	if (ret != ACT_LINK_LOST) {
2938 		dm_helpers_dp_mst_send_payload_allocation(
2939 				stream->ctx,
2940 				stream,
2941 				true);
2942 	}
2943 
2944 	/* slot X.Y for only current stream */
2945 	pbn_per_slot = get_pbn_per_slot(stream);
2946 	pbn = get_pbn_from_timing(pipe_ctx);
2947 	avg_time_slots_per_mtp = dc_fixpt_div(pbn, pbn_per_slot);
2948 
2949 	stream_encoder->funcs->set_throttled_vcp_size(
2950 		stream_encoder,
2951 		avg_time_slots_per_mtp);
2952 
2953 	return DC_OK;
2954 
2955 }
2956 
deallocate_mst_payload(struct pipe_ctx * pipe_ctx)2957 static enum dc_status deallocate_mst_payload(struct pipe_ctx *pipe_ctx)
2958 {
2959 	struct dc_stream_state *stream = pipe_ctx->stream;
2960 	struct dc_link *link = stream->link;
2961 	struct link_encoder *link_encoder = link->link_enc;
2962 	struct stream_encoder *stream_encoder = pipe_ctx->stream_res.stream_enc;
2963 	struct dp_mst_stream_allocation_table proposed_table = {0};
2964 	struct fixed31_32 avg_time_slots_per_mtp = dc_fixpt_from_int(0);
2965 	uint8_t i;
2966 	bool mst_mode = (link->type == dc_connection_mst_branch);
2967 	DC_LOGGER_INIT(link->ctx->logger);
2968 
2969 	/* deallocate_mst_payload is called before disable link. When mode or
2970 	 * disable/enable monitor, new stream is created which is not in link
2971 	 * stream[] yet. For this, payload is not allocated yet, so de-alloc
2972 	 * should not done. For new mode set, map_resources will get engine
2973 	 * for new stream, so stream_enc->id should be validated until here.
2974 	 */
2975 
2976 	/* slot X.Y */
2977 	stream_encoder->funcs->set_throttled_vcp_size(
2978 		stream_encoder,
2979 		avg_time_slots_per_mtp);
2980 
2981 	/* TODO: which component is responsible for remove payload table? */
2982 	if (mst_mode) {
2983 		if (dm_helpers_dp_mst_write_payload_allocation_table(
2984 				stream->ctx,
2985 				stream,
2986 				&proposed_table,
2987 				false)) {
2988 
2989 			update_mst_stream_alloc_table(
2990 				link, pipe_ctx->stream_res.stream_enc, &proposed_table);
2991 		}
2992 		else {
2993 				DC_LOG_WARNING("Failed to update"
2994 						"MST allocation table for"
2995 						"pipe idx:%d\n",
2996 						pipe_ctx->pipe_idx);
2997 		}
2998 	}
2999 
3000 	DC_LOG_MST("%s"
3001 			"stream_count: %d: ",
3002 			__func__,
3003 			link->mst_stream_alloc_table.stream_count);
3004 
3005 	for (i = 0; i < MAX_CONTROLLER_NUM; i++) {
3006 		DC_LOG_MST("stream_enc[%d]: %p      "
3007 		"stream[%d].vcp_id: %d      "
3008 		"stream[%d].slot_count: %d\n",
3009 		i,
3010 		(void *) link->mst_stream_alloc_table.stream_allocations[i].stream_enc,
3011 		i,
3012 		link->mst_stream_alloc_table.stream_allocations[i].vcp_id,
3013 		i,
3014 		link->mst_stream_alloc_table.stream_allocations[i].slot_count);
3015 	}
3016 
3017 	link_encoder->funcs->update_mst_stream_allocation_table(
3018 		link_encoder,
3019 		&link->mst_stream_alloc_table);
3020 
3021 	if (mst_mode) {
3022 		dm_helpers_dp_mst_poll_for_allocation_change_trigger(
3023 			stream->ctx,
3024 			stream);
3025 
3026 		dm_helpers_dp_mst_send_payload_allocation(
3027 			stream->ctx,
3028 			stream,
3029 			false);
3030 	}
3031 
3032 	return DC_OK;
3033 }
3034 
dc_link_reallocate_mst_payload(struct dc_link * link)3035 enum dc_status dc_link_reallocate_mst_payload(struct dc_link *link)
3036 {
3037 	int i;
3038 	struct pipe_ctx *pipe_ctx;
3039 
3040 	// Clear all of MST payload then reallocate
3041 	for (i = 0; i < MAX_PIPES; i++) {
3042 		pipe_ctx = &link->dc->current_state->res_ctx.pipe_ctx[i];
3043 
3044 		/* driver enable split pipe for external monitors
3045 		 * we have to check pipe_ctx is split pipe or not
3046 		 * If it's split pipe, driver using top pipe to
3047 		 * reaallocate.
3048 		 */
3049 		if (!pipe_ctx || pipe_ctx->top_pipe)
3050 			continue;
3051 
3052 		if (pipe_ctx->stream && pipe_ctx->stream->link == link &&
3053 				pipe_ctx->stream->dpms_off == false &&
3054 				pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3055 			deallocate_mst_payload(pipe_ctx);
3056 		}
3057 	}
3058 
3059 	for (i = 0; i < MAX_PIPES; i++) {
3060 		pipe_ctx = &link->dc->current_state->res_ctx.pipe_ctx[i];
3061 
3062 		if (!pipe_ctx || pipe_ctx->top_pipe)
3063 			continue;
3064 
3065 		if (pipe_ctx->stream && pipe_ctx->stream->link == link &&
3066 				pipe_ctx->stream->dpms_off == false &&
3067 				pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3068 			/* enable/disable PHY will clear connection between BE and FE
3069 			 * need to restore it.
3070 			 */
3071 			link->link_enc->funcs->connect_dig_be_to_fe(link->link_enc,
3072 									pipe_ctx->stream_res.stream_enc->id, true);
3073 			dc_link_allocate_mst_payload(pipe_ctx);
3074 		}
3075 	}
3076 
3077 	return DC_OK;
3078 }
3079 
3080 #if defined(CONFIG_DRM_AMD_DC_HDCP)
update_psp_stream_config(struct pipe_ctx * pipe_ctx,bool dpms_off)3081 static void update_psp_stream_config(struct pipe_ctx *pipe_ctx, bool dpms_off)
3082 {
3083 	struct cp_psp *cp_psp = &pipe_ctx->stream->ctx->cp_psp;
3084 	if (cp_psp && cp_psp->funcs.update_stream_config) {
3085 		struct cp_psp_stream_config config;
3086 
3087 		memset(&config, 0, sizeof(config));
3088 
3089 		config.otg_inst = (uint8_t) pipe_ctx->stream_res.tg->inst;
3090 		/*stream_enc_inst*/
3091 		config.stream_enc_inst = (uint8_t) pipe_ctx->stream_res.stream_enc->stream_enc_inst;
3092 		config.link_enc_inst = pipe_ctx->stream->link->link_enc_hw_inst;
3093 		config.dpms_off = dpms_off;
3094 		config.dm_stream_ctx = pipe_ctx->stream->dm_stream_context;
3095 		config.mst_supported = (pipe_ctx->stream->signal ==
3096 				SIGNAL_TYPE_DISPLAY_PORT_MST);
3097 		cp_psp->funcs.update_stream_config(cp_psp->handle, &config);
3098 	}
3099 }
3100 #endif
3101 
core_link_enable_stream(struct dc_state * state,struct pipe_ctx * pipe_ctx)3102 void core_link_enable_stream(
3103 		struct dc_state *state,
3104 		struct pipe_ctx *pipe_ctx)
3105 {
3106 	struct dc *dc = pipe_ctx->stream->ctx->dc;
3107 	struct dc_stream_state *stream = pipe_ctx->stream;
3108 	enum dc_status status;
3109 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
3110 	enum otg_out_mux_dest otg_out_dest = OUT_MUX_DIO;
3111 #endif
3112 	DC_LOGGER_INIT(pipe_ctx->stream->ctx->logger);
3113 
3114 	if (!IS_DIAG_DC(dc->ctx->dce_environment) &&
3115 			dc_is_virtual_signal(pipe_ctx->stream->signal))
3116 		return;
3117 
3118 	if (!dc_is_virtual_signal(pipe_ctx->stream->signal)) {
3119 		stream->link->link_enc->funcs->setup(
3120 			stream->link->link_enc,
3121 			pipe_ctx->stream->signal);
3122 		pipe_ctx->stream_res.stream_enc->funcs->setup_stereo_sync(
3123 			pipe_ctx->stream_res.stream_enc,
3124 			pipe_ctx->stream_res.tg->inst,
3125 			stream->timing.timing_3d_format != TIMING_3D_FORMAT_NONE);
3126 	}
3127 
3128 	if (dc_is_dp_signal(pipe_ctx->stream->signal))
3129 		pipe_ctx->stream_res.stream_enc->funcs->dp_set_stream_attribute(
3130 			pipe_ctx->stream_res.stream_enc,
3131 			&stream->timing,
3132 			stream->output_color_space,
3133 			stream->use_vsc_sdp_for_colorimetry,
3134 			stream->link->dpcd_caps.dprx_feature.bits.SST_SPLIT_SDP_CAP);
3135 
3136 	if (dc_is_hdmi_tmds_signal(pipe_ctx->stream->signal))
3137 		pipe_ctx->stream_res.stream_enc->funcs->hdmi_set_stream_attribute(
3138 			pipe_ctx->stream_res.stream_enc,
3139 			&stream->timing,
3140 			stream->phy_pix_clk,
3141 			pipe_ctx->stream_res.audio != NULL);
3142 
3143 	pipe_ctx->stream->link->link_state_valid = true;
3144 
3145 #if defined(CONFIG_DRM_AMD_DC_DCN3_0)
3146 	if (pipe_ctx->stream_res.tg->funcs->set_out_mux)
3147 		pipe_ctx->stream_res.tg->funcs->set_out_mux(pipe_ctx->stream_res.tg, otg_out_dest);
3148 #endif
3149 
3150 	if (dc_is_dvi_signal(pipe_ctx->stream->signal))
3151 		pipe_ctx->stream_res.stream_enc->funcs->dvi_set_stream_attribute(
3152 			pipe_ctx->stream_res.stream_enc,
3153 			&stream->timing,
3154 			(pipe_ctx->stream->signal == SIGNAL_TYPE_DVI_DUAL_LINK) ?
3155 			true : false);
3156 
3157 	if (dc_is_lvds_signal(pipe_ctx->stream->signal))
3158 		pipe_ctx->stream_res.stream_enc->funcs->lvds_set_stream_attribute(
3159 			pipe_ctx->stream_res.stream_enc,
3160 			&stream->timing);
3161 
3162 	if (!IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment)) {
3163 		bool apply_edp_fast_boot_optimization =
3164 			pipe_ctx->stream->apply_edp_fast_boot_optimization;
3165 
3166 		pipe_ctx->stream->apply_edp_fast_boot_optimization = false;
3167 
3168 		resource_build_info_frame(pipe_ctx);
3169 		dc->hwss.update_info_frame(pipe_ctx);
3170 
3171 		/* Do not touch link on seamless boot optimization. */
3172 		if (pipe_ctx->stream->apply_seamless_boot_optimization) {
3173 			pipe_ctx->stream->dpms_off = false;
3174 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3175 			update_psp_stream_config(pipe_ctx, false);
3176 #endif
3177 			return;
3178 		}
3179 
3180 		/* eDP lit up by bios already, no need to enable again. */
3181 		if (pipe_ctx->stream->signal == SIGNAL_TYPE_EDP &&
3182 					apply_edp_fast_boot_optimization) {
3183 			pipe_ctx->stream->dpms_off = false;
3184 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3185 			update_psp_stream_config(pipe_ctx, false);
3186 #endif
3187 			return;
3188 		}
3189 
3190 		if (pipe_ctx->stream->dpms_off)
3191 			return;
3192 
3193 		/* Have to setup DSC before DIG FE and BE are connected (which happens before the
3194 		 * link training). This is to make sure the bandwidth sent to DIG BE won't be
3195 		 * bigger than what the link and/or DIG BE can handle. VBID[6]/CompressedStream_flag
3196 		 * will be automatically set at a later time when the video is enabled
3197 		 * (DP_VID_STREAM_EN = 1).
3198 		 */
3199 		if (pipe_ctx->stream->timing.flags.DSC) {
3200 			if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3201 					dc_is_virtual_signal(pipe_ctx->stream->signal))
3202 				dp_set_dsc_enable(pipe_ctx, true);
3203 		}
3204 
3205 		status = enable_link(state, pipe_ctx);
3206 
3207 		if (status != DC_OK) {
3208 			DC_LOG_WARNING("enabling link %u failed: %d\n",
3209 			pipe_ctx->stream->link->link_index,
3210 			status);
3211 
3212 			/* Abort stream enable *unless* the failure was due to
3213 			 * DP link training - some DP monitors will recover and
3214 			 * show the stream anyway. But MST displays can't proceed
3215 			 * without link training.
3216 			 */
3217 			if (status != DC_FAIL_DP_LINK_TRAINING ||
3218 					pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
3219 				BREAK_TO_DEBUGGER();
3220 				return;
3221 			}
3222 		}
3223 
3224 		dc->hwss.enable_audio_stream(pipe_ctx);
3225 
3226 		/* turn off otg test pattern if enable */
3227 		if (pipe_ctx->stream_res.tg->funcs->set_test_pattern)
3228 			pipe_ctx->stream_res.tg->funcs->set_test_pattern(pipe_ctx->stream_res.tg,
3229 					CONTROLLER_DP_TEST_PATTERN_VIDEOMODE,
3230 					COLOR_DEPTH_UNDEFINED);
3231 
3232 		/* This second call is needed to reconfigure the DIG
3233 		 * as a workaround for the incorrect value being applied
3234 		 * from transmitter control.
3235 		 */
3236 		if (!dc_is_virtual_signal(pipe_ctx->stream->signal))
3237 			stream->link->link_enc->funcs->setup(
3238 				stream->link->link_enc,
3239 				pipe_ctx->stream->signal);
3240 
3241 		dc->hwss.enable_stream(pipe_ctx);
3242 
3243 		/* Set DPS PPS SDP (AKA "info frames") */
3244 		if (pipe_ctx->stream->timing.flags.DSC) {
3245 			if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3246 					dc_is_virtual_signal(pipe_ctx->stream->signal))
3247 				dp_set_dsc_pps_sdp(pipe_ctx, true);
3248 		}
3249 
3250 		if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3251 			dc_link_allocate_mst_payload(pipe_ctx);
3252 
3253 		dc->hwss.unblank_stream(pipe_ctx,
3254 			&pipe_ctx->stream->link->cur_link_settings);
3255 
3256 		if (stream->sink_patches.delay_ignore_msa > 0)
3257 			msleep(stream->sink_patches.delay_ignore_msa);
3258 
3259 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
3260 			enable_stream_features(pipe_ctx);
3261 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3262 		update_psp_stream_config(pipe_ctx, false);
3263 #endif
3264 	} else { // if (IS_FPGA_MAXIMUS_DC(dc->ctx->dce_environment))
3265 		if (dc_is_dp_signal(pipe_ctx->stream->signal) ||
3266 				dc_is_virtual_signal(pipe_ctx->stream->signal))
3267 			dp_set_dsc_enable(pipe_ctx, true);
3268 
3269 	}
3270 
3271 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
3272 		core_link_set_avmute(pipe_ctx, false);
3273 	}
3274 }
3275 
core_link_disable_stream(struct pipe_ctx * pipe_ctx)3276 void core_link_disable_stream(struct pipe_ctx *pipe_ctx)
3277 {
3278 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
3279 	struct dc_stream_state *stream = pipe_ctx->stream;
3280 	struct dc_link *link = stream->sink->link;
3281 
3282 	if (!IS_DIAG_DC(dc->ctx->dce_environment) &&
3283 			dc_is_virtual_signal(pipe_ctx->stream->signal))
3284 		return;
3285 
3286 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
3287 		core_link_set_avmute(pipe_ctx, true);
3288 	}
3289 
3290 #if defined(CONFIG_DRM_AMD_DC_HDCP)
3291 	update_psp_stream_config(pipe_ctx, true);
3292 #endif
3293 	dc->hwss.blank_stream(pipe_ctx);
3294 
3295 	if (pipe_ctx->stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
3296 		deallocate_mst_payload(pipe_ctx);
3297 
3298 	if (dc_is_hdmi_signal(pipe_ctx->stream->signal)) {
3299 		struct ext_hdmi_settings settings = {0};
3300 		enum engine_id eng_id = pipe_ctx->stream_res.stream_enc->id;
3301 
3302 		unsigned short masked_chip_caps = link->chip_caps &
3303 				EXT_DISPLAY_PATH_CAPS__EXT_CHIP_MASK;
3304 		//Need to inform that sink is going to use legacy HDMI mode.
3305 		dal_ddc_service_write_scdc_data(
3306 			link->ddc,
3307 			165000,//vbios only handles 165Mhz.
3308 			false);
3309 		if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_TISN65DP159RSBT) {
3310 			/* DP159, Retimer settings */
3311 			if (get_ext_hdmi_settings(pipe_ctx, eng_id, &settings))
3312 				write_i2c_retimer_setting(pipe_ctx,
3313 						false, false, &settings);
3314 			else
3315 				write_i2c_default_retimer_setting(pipe_ctx,
3316 						false, false);
3317 		} else if (masked_chip_caps == EXT_DISPLAY_PATH_CAPS__HDMI20_PI3EQX1204) {
3318 			/* PI3EQX1204, Redriver settings */
3319 			write_i2c_redriver_setting(pipe_ctx, false);
3320 		}
3321 	}
3322 
3323 	disable_link(pipe_ctx->stream->link, pipe_ctx->stream->signal);
3324 
3325 	dc->hwss.disable_stream(pipe_ctx);
3326 
3327 	if (pipe_ctx->stream->timing.flags.DSC) {
3328 		if (dc_is_dp_signal(pipe_ctx->stream->signal))
3329 			dp_set_dsc_enable(pipe_ctx, false);
3330 	}
3331 }
3332 
core_link_set_avmute(struct pipe_ctx * pipe_ctx,bool enable)3333 void core_link_set_avmute(struct pipe_ctx *pipe_ctx, bool enable)
3334 {
3335 	struct dc  *dc = pipe_ctx->stream->ctx->dc;
3336 
3337 	if (!dc_is_hdmi_signal(pipe_ctx->stream->signal))
3338 		return;
3339 
3340 	dc->hwss.set_avmute(pipe_ctx, enable);
3341 }
3342 
3343 /**
3344  *****************************************************************************
3345  *  Function: dc_link_enable_hpd_filter
3346  *
3347  *  @brief
3348  *     If enable is true, programs HPD filter on associated HPD line using
3349  *     delay_on_disconnect/delay_on_connect values dependent on
3350  *     link->connector_signal
3351  *
3352  *     If enable is false, programs HPD filter on associated HPD line with no
3353  *     delays on connect or disconnect
3354  *
3355  *  @param [in] link: pointer to the dc link
3356  *  @param [in] enable: boolean specifying whether to enable hbd
3357  *****************************************************************************
3358  */
dc_link_enable_hpd_filter(struct dc_link * link,bool enable)3359 void dc_link_enable_hpd_filter(struct dc_link *link, bool enable)
3360 {
3361 	struct gpio *hpd;
3362 
3363 	if (enable) {
3364 		link->is_hpd_filter_disabled = false;
3365 		program_hpd_filter(link);
3366 	} else {
3367 		link->is_hpd_filter_disabled = true;
3368 		/* Obtain HPD handle */
3369 		hpd = get_hpd_gpio(link->ctx->dc_bios, link->link_id, link->ctx->gpio_service);
3370 
3371 		if (!hpd)
3372 			return;
3373 
3374 		/* Setup HPD filtering */
3375 		if (dal_gpio_open(hpd, GPIO_MODE_INTERRUPT) == GPIO_RESULT_OK) {
3376 			struct gpio_hpd_config config;
3377 
3378 			config.delay_on_connect = 0;
3379 			config.delay_on_disconnect = 0;
3380 
3381 			dal_irq_setup_hpd_filter(hpd, &config);
3382 
3383 			dal_gpio_close(hpd);
3384 		} else {
3385 			ASSERT_CRITICAL(false);
3386 		}
3387 		/* Release HPD handle */
3388 		dal_gpio_destroy_irq(&hpd);
3389 	}
3390 }
3391 
dc_bandwidth_in_kbps_from_timing(const struct dc_crtc_timing * timing)3392 uint32_t dc_bandwidth_in_kbps_from_timing(
3393 	const struct dc_crtc_timing *timing)
3394 {
3395 	uint32_t bits_per_channel = 0;
3396 	uint32_t kbps;
3397 	struct fixed31_32 link_bw_kbps;
3398 
3399 	if (timing->flags.DSC) {
3400 		link_bw_kbps = dc_fixpt_from_int(timing->pix_clk_100hz);
3401 		link_bw_kbps = dc_fixpt_div_int(link_bw_kbps, 160);
3402 		link_bw_kbps = dc_fixpt_mul_int(link_bw_kbps, timing->dsc_cfg.bits_per_pixel);
3403 		kbps = dc_fixpt_ceil(link_bw_kbps);
3404 		return kbps;
3405 	}
3406 
3407 	switch (timing->display_color_depth) {
3408 	case COLOR_DEPTH_666:
3409 		bits_per_channel = 6;
3410 		break;
3411 	case COLOR_DEPTH_888:
3412 		bits_per_channel = 8;
3413 		break;
3414 	case COLOR_DEPTH_101010:
3415 		bits_per_channel = 10;
3416 		break;
3417 	case COLOR_DEPTH_121212:
3418 		bits_per_channel = 12;
3419 		break;
3420 	case COLOR_DEPTH_141414:
3421 		bits_per_channel = 14;
3422 		break;
3423 	case COLOR_DEPTH_161616:
3424 		bits_per_channel = 16;
3425 		break;
3426 	default:
3427 		break;
3428 	}
3429 
3430 	ASSERT(bits_per_channel != 0);
3431 
3432 	kbps = timing->pix_clk_100hz / 10;
3433 	kbps *= bits_per_channel;
3434 
3435 	if (timing->flags.Y_ONLY != 1) {
3436 		/*Only YOnly make reduce bandwidth by 1/3 compares to RGB*/
3437 		kbps *= 3;
3438 		if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
3439 			kbps /= 2;
3440 		else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
3441 			kbps = kbps * 2 / 3;
3442 	}
3443 
3444 	return kbps;
3445 
3446 }
3447 
dc_link_set_drive_settings(struct dc * dc,struct link_training_settings * lt_settings,const struct dc_link * link)3448 void dc_link_set_drive_settings(struct dc *dc,
3449 				struct link_training_settings *lt_settings,
3450 				const struct dc_link *link)
3451 {
3452 
3453 	int i;
3454 
3455 	for (i = 0; i < dc->link_count; i++) {
3456 		if (dc->links[i] == link)
3457 			break;
3458 	}
3459 
3460 	if (i >= dc->link_count)
3461 		ASSERT_CRITICAL(false);
3462 
3463 	dc_link_dp_set_drive_settings(dc->links[i], lt_settings);
3464 }
3465 
dc_link_perform_link_training(struct dc * dc,struct dc_link_settings * link_setting,bool skip_video_pattern)3466 void dc_link_perform_link_training(struct dc *dc,
3467 				   struct dc_link_settings *link_setting,
3468 				   bool skip_video_pattern)
3469 {
3470 	int i;
3471 
3472 	for (i = 0; i < dc->link_count; i++)
3473 		dc_link_dp_perform_link_training(
3474 			dc->links[i],
3475 			link_setting,
3476 			skip_video_pattern);
3477 }
3478 
dc_link_set_preferred_link_settings(struct dc * dc,struct dc_link_settings * link_setting,struct dc_link * link)3479 void dc_link_set_preferred_link_settings(struct dc *dc,
3480 					 struct dc_link_settings *link_setting,
3481 					 struct dc_link *link)
3482 {
3483 	int i;
3484 	struct pipe_ctx *pipe;
3485 	struct dc_stream_state *link_stream;
3486 	struct dc_link_settings store_settings = *link_setting;
3487 
3488 	link->preferred_link_setting = store_settings;
3489 
3490 	/* Retrain with preferred link settings only relevant for
3491 	 * DP signal type
3492 	 * Check for non-DP signal or if passive dongle present
3493 	 */
3494 	if (!dc_is_dp_signal(link->connector_signal) ||
3495 		link->dongle_max_pix_clk > 0)
3496 		return;
3497 
3498 	for (i = 0; i < MAX_PIPES; i++) {
3499 		pipe = &dc->current_state->res_ctx.pipe_ctx[i];
3500 		if (pipe->stream && pipe->stream->link) {
3501 			if (pipe->stream->link == link) {
3502 				link_stream = pipe->stream;
3503 				break;
3504 			}
3505 		}
3506 	}
3507 
3508 	/* Stream not found */
3509 	if (i == MAX_PIPES)
3510 		return;
3511 
3512 	/* Cannot retrain link if backend is off */
3513 	if (link_stream->dpms_off)
3514 		return;
3515 
3516 	decide_link_settings(link_stream, &store_settings);
3517 
3518 	if ((store_settings.lane_count != LANE_COUNT_UNKNOWN) &&
3519 		(store_settings.link_rate != LINK_RATE_UNKNOWN))
3520 		dp_retrain_link_dp_test(link, &store_settings, false);
3521 }
3522 
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)3523 void dc_link_set_preferred_training_settings(struct dc *dc,
3524 						 struct dc_link_settings *link_setting,
3525 						 struct dc_link_training_overrides *lt_overrides,
3526 						 struct dc_link *link,
3527 						 bool skip_immediate_retrain)
3528 {
3529 	if (lt_overrides != NULL)
3530 		link->preferred_training_settings = *lt_overrides;
3531 	else
3532 		memset(&link->preferred_training_settings, 0, sizeof(link->preferred_training_settings));
3533 
3534 	if (link_setting != NULL) {
3535 		link->preferred_link_setting = *link_setting;
3536 	} else {
3537 		link->preferred_link_setting.lane_count = LANE_COUNT_UNKNOWN;
3538 		link->preferred_link_setting.link_rate = LINK_RATE_UNKNOWN;
3539 	}
3540 
3541 	/* Retrain now, or wait until next stream update to apply */
3542 	if (skip_immediate_retrain == false)
3543 		dc_link_set_preferred_link_settings(dc, &link->preferred_link_setting, link);
3544 }
3545 
dc_link_enable_hpd(const struct dc_link * link)3546 void dc_link_enable_hpd(const struct dc_link *link)
3547 {
3548 	dc_link_dp_enable_hpd(link);
3549 }
3550 
dc_link_disable_hpd(const struct dc_link * link)3551 void dc_link_disable_hpd(const struct dc_link *link)
3552 {
3553 	dc_link_dp_disable_hpd(link);
3554 }
3555 
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)3556 void dc_link_set_test_pattern(struct dc_link *link,
3557 			      enum dp_test_pattern test_pattern,
3558 			      enum dp_test_pattern_color_space test_pattern_color_space,
3559 			      const struct link_training_settings *p_link_settings,
3560 			      const unsigned char *p_custom_pattern,
3561 			      unsigned int cust_pattern_size)
3562 {
3563 	if (link != NULL)
3564 		dc_link_dp_set_test_pattern(
3565 			link,
3566 			test_pattern,
3567 			test_pattern_color_space,
3568 			p_link_settings,
3569 			p_custom_pattern,
3570 			cust_pattern_size);
3571 }
3572 
dc_link_bandwidth_kbps(const struct dc_link * link,const struct dc_link_settings * link_setting)3573 uint32_t dc_link_bandwidth_kbps(
3574 	const struct dc_link *link,
3575 	const struct dc_link_settings *link_setting)
3576 {
3577 	uint32_t link_bw_kbps =
3578 		link_setting->link_rate * LINK_RATE_REF_FREQ_IN_KHZ; /* bytes per sec */
3579 
3580 	link_bw_kbps *= 8;   /* 8 bits per byte*/
3581 	link_bw_kbps *= link_setting->lane_count;
3582 
3583 	if (dc_link_is_fec_supported(link) && !link->dc->debug.disable_fec) {
3584 		/* Account for FEC overhead.
3585 		 * We have to do it based on caps,
3586 		 * and not based on FEC being set ready,
3587 		 * because FEC is set ready too late in
3588 		 * the process to correctly be picked up
3589 		 * by mode enumeration.
3590 		 *
3591 		 * There's enough zeros at the end of 'kbps'
3592 		 * that make the below operation 100% precise
3593 		 * for our purposes.
3594 		 * 'long long' makes it work even for HDMI 2.1
3595 		 * max bandwidth (and much, much bigger bandwidths
3596 		 * than that, actually).
3597 		 *
3598 		 * NOTE: Reducing link BW by 3% may not be precise
3599 		 * because it may be a stream BT that increases by 3%, and so
3600 		 * 1/1.03 = 0.970873 factor should have been used instead,
3601 		 * but the difference is minimal and is in a safe direction,
3602 		 * which all works well around potential ambiguity of DP 1.4a spec.
3603 		 */
3604 		link_bw_kbps = mul_u64_u32_shr(BIT_ULL(32) * 970LL / 1000,
3605 					       link_bw_kbps, 32);
3606 	}
3607 
3608 	return link_bw_kbps;
3609 
3610 }
3611 
dc_link_get_link_cap(const struct dc_link * link)3612 const struct dc_link_settings *dc_link_get_link_cap(
3613 		const struct dc_link *link)
3614 {
3615 	if (link->preferred_link_setting.lane_count != LANE_COUNT_UNKNOWN &&
3616 			link->preferred_link_setting.link_rate != LINK_RATE_UNKNOWN)
3617 		return &link->preferred_link_setting;
3618 	return &link->verified_link_cap;
3619 }
3620 
dc_link_overwrite_extended_receiver_cap(struct dc_link * link)3621 void dc_link_overwrite_extended_receiver_cap(
3622 		struct dc_link *link)
3623 {
3624 	dp_overwrite_extended_receiver_cap(link);
3625 }
3626 
dc_link_is_fec_supported(const struct dc_link * link)3627 bool dc_link_is_fec_supported(const struct dc_link *link)
3628 {
3629 	return (dc_is_dp_signal(link->connector_signal) &&
3630 			link->link_enc->features.fec_supported &&
3631 			link->dpcd_caps.fec_cap.bits.FEC_CAPABLE &&
3632 			!IS_FPGA_MAXIMUS_DC(link->ctx->dce_environment));
3633 }
3634 
3635